Web Applications Stack Exchange is a question and answer site for power users of web applications. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I use a function such as =importXml("http://www.google.com", "//a/@href") in a Google Spreadsheet cell, it works just fine. However, when I try to use the same function in a script, it gives an error message.

My script has the following code:

function myFunction() {
  return importXml("http://www.google.com", "//a/@href");

When I call the function from a cell by using =myFunction(), I get the following error:

error: ReferenceError: "importXml" is not defined.

How can I use the importXml function in a script?

share|improve this question

In addition to the remark made by Barry, here's a link that explains the usage of XML in combination with Google Apps Script (GAS): XML with GAS

You can use the importXML formula in GAS, but you have to address it as a formula:

function myXML() {
  var cell = SpreadsheetApp.getActiveSpreadsheet()
  cell.setFormula('=importXml("http://www.google.com", "//a/@href")');  

Reference: setFormula

share|improve this answer

This isn't possible because ImportXML is a spreadsheet function and is not available in the Script Editor.

You would have to write a script to get the XML from the URL and then parse it manually.

What exactly are you trying achieve? (I am presuming that the Google Url and href elements are an example?)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.