简体   繁体   中英

Adding Multiple Arguments to a Custom Function in Google Sheets

I've searched high and wide for an answer but can't seem to find it. I am trying to alter my custom function that looks up sitemap URL's and the date they were updated to accept a range of inputs.

Here is the current function that works:

function sitemap(sitemapUrl, namespace) {
  var array = [];
  var xml = UrlFetchApp.fetch(sitemapUrl).getContentText();
  var document = XmlService.parse(xml);
  var root = document.getRootElement();
  var sitemapNameSpace = XmlService.getNamespace("http://www.sitemaps.org/schemas/sitemap/0.9");

  var urls = root.getChildren('url', sitemapNameSpace);

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

  return array;
}

I've tried using Google's example below but doesn't seem to work however I incorporate it into my function. Any ideas?

function DOUBLE(input) {
  if (input.map) {            // Test whether input is an array.
    return input.map(DOUBLE); // Recurse over array if so.
  } else {
    return input * 2;
  }
}

Edit: This is how I tried to use Google's example for my function:

function sitemaps(sitemapUrl) {

    var array = [];
    var xml = UrlFetchApp.fetch(sitemapUrl).getContentText();
    var document = XmlService.parse(xml);
    var root = document.getRootElement()
    var sitemapNameSpace = XmlService.getNamespace("http://www.sitemaps.org/schemas/sitemap/0.9")

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

    for (var i = 0; i < urls.length; i++) {
     var loc = urls[i].getChild('loc',sitemapNameSpace).getText();
     var lastmod = urls[i].getChild('lastmod',sitemapNameSpace).getText();
    array.push([loc, lastmod]);
  }
   if (sitemapUrl.map) {            
    return sitemapUrl.map(sitemaps); 
  } else {  
    return array  

}

You are no using the same format as the Google example . As of right now you are checking if the input is an array after actually retrieving the data.

But you using fetch with an array as input could trigger an Error and the function may no get to the point where it checks if the sitemapUrl can be used with map .

Also take into account that map will call the function in every single element of the array and return an array with a result for each of element. So in your case B3:B6 would call the function for the value at B3, B4, B5 and B6 and return an array of length 4 with the result. For your case in which you want a single list you need to flattern the array afterwards

I would change your function to be like this:

function sitemaps(sitemapUrl) {

    if (sitemapUrl.map) {
        return sitemapUrl.map(sitemaps).flat();
    } else {

        var array = [];
        var xml = UrlFetchApp.fetch(sitemapUrl).getContentText();
        var document = XmlService.parse(xml);
        var root = document.getRootElement()
        var sitemapNameSpace = XmlService.getNamespace("http://www.sitemaps.org/schemas/sitemap/0.9")

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

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

Although what you are doing is fine take into account that it also exists a way to retrieve all the request at the same time ( UrlFetchApp.fetch() ) but for this specific case you would need to flatten a reshape the input array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM