1
votes

I am pretty new to Google Apps Scripts. I am using a script that pulls the URLs from an XML sitemap into Google Spreadsheets. I need it to be time-based so that it will pull in the new URLs when they are added.

I have added a trigger to my code via project triggers. The trigger seems to run but the spreadsheet is not updated with the new URLs.

I am using the following code

function sitemap(sitemapUrl,namespace) {
 try {
    var xml = UrlFetchApp.fetch(sitemapUrl).getContentText();
    var document = XmlService.parse(xml);
    var root = document.getRootElement()
    var sitemapNameSpace = XmlService.getNamespace(namespace);

    var urls = root.getChildren('url', sitemapNameSpace)
    var locs = []

    for (var i=0;i <urls.length;i++) {
      locs.push(urls[i].getChild('loc', sitemapNameSpace).getText()) 
    }

    return locs  
  } catch (e) {
    return e 
    }
}

The script is working fine but the problem is that it does not update the information according to the trigger I have created. Does anyone have a suggestion on how to solve this?

1
...did you forget to setValues()?sinaraheneba

1 Answers

0
votes

The function isn't updating the spreadsheet because there isn't any statement that do that.

There are several ways to do this but it's very likely, assuming that locs is a 2D array object, that you should use range.setValues(locs) where range is the destination range.

Please read https://developers.google.com/apps-script/guides/sheets for further details or search this site for questions about passing values to a sheet (there are a lot).