Take the 2-minute tour ×
Web Applications Stack Exchange is a question and answer site for power users of web applications. It's 100% free, no registration required.

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

2 Answers 2

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

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()
    .getActiveSheet().getActiveCell();
  cell.setFormula('=importXml("http://www.google.com", "//a/@href")');  
}

Reference: setFormula

share|improve this answer

Your Answer

 
discard

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.