简体   繁体   English

将多个 Arguments 添加到 Google 表格中的自定义 Function

[英]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.我正在尝试更改我的自定义 function 以查找站点地图 URL 及其更新日期以接受一系列输入。

Here is the current function that works:这是当前有效的 function:

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.我试过使用下面的谷歌示例,但似乎不起作用,但我将它合并到我的 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:编辑:这就是我尝试将 Google 的示例用于我的 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 .您没有使用与Google 示例相同的格式。 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 .但是您将fetch与数组一起用作输入可能会触发错误,并且 function 可能无法检查sitemapUrl可以与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.还要考虑到map将在数组的每个元素中调用 function 并返回一个包含每个元素结果的数组。 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.因此,在您的情况下,B3:B6 将为 B3、B4、B5 和 B6 处的值调用 function 并返回一个长度为 4 的数组和结果。 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 改成这样:

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.尽管您所做的很好,但考虑到它还存在一种同时检索所有请求的方法( UrlFetchApp.fetch() ),但对于这种特定情况,您需要展平重塑输入数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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