【问题标题】:Array input for GOOGLEMAPS custom apps script function in Google SheetsGoogle表格中GOOGLEMAPS自定义应用脚本函数的数组输入
【发布时间】:2021-04-08 08:06:22
【问题描述】:

我正在尝试升级下面的代码,使其能够接受数组输入。

const md5 = (key = '') => {
 const code = key.toLowerCase().replace(/\s/g, '');
 return Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, key)
   .map((char) => (char + 256).toString(16).slice(-2))
   .join('');
};

const getCache = (key) => {
 return CacheService.getDocumentCache().get(md5(key));
};

// Store the results for 6 hours
const setCache = (key, value) => {
 const expirationInSeconds = 6 * 60 * 60;
 CacheService.getDocumentCache().put(md5(key), value, expirationInSeconds);
};

const GOOGLEMAPS_DISTANCE = (origin, destination, mode = 'driving') => {
 const key = ['distance', origin, destination, mode].join(',');
 // Is result in the internal cache?
 const value = getCache(key);
 // If yes, serve the cached result
 if (value !== null) return value;
 const { routes: [data] = [] } = Maps.newDirectionFinder()
   .setOrigin(origin)
   .setDestination(destination)
   .setMode(mode)
   .getDirections();
 if (!data) {
   GOOGLEMAPS_DISTANCE;
 }
 const { legs: [{ distance: { text: distance } } = {}] = [] } = data;
 // Store the result in internal cache for future
 setCache(key, distance);
 return distance;
};

目前,代码能够找到两个给定地址之间的距离,并将其返回给单个输入。为了解决 Google 的 API 请求限制,我添加了缓存以前值的功能。此外,只要遇到错误(例如 API 请求限制),代码就会重新运行。

现在,我想升级函数以便能够接受origindestination 的数组。我找到了Google documentation 用于通过使用map 调用添加此功能,但我似乎无法使其工作。如果有人愿意回复并帮助我,我将不胜感激。

【问题讨论】:

    标签: google-maps google-apps-script google-sheets google-api


    【解决方案1】:

    尝试在函数声明之后立即添加这几行。

    const GOOGLEMAPS_DISTANCE = (origin, destination, mode = 'driving') => {
     
      
      if(origin.map && destination.map) {
        return origin.map((origin, i) => GOOGLEMAPS_DISTANCE(origin[0], destination[i][0]))
    }
    
    //...rest of your code
    }

    注意:如果您想为空行/单元格返回空白,请尝试:

    const GOOGLEMAPS_DISTANCE = (origin, destination, mode = 'driving') => {
      
      if(origin.map && destination.map) {
          return origin.map( (origin, i) => origin[0] && destination[i][0]? GOOGLEMAPS_DISTANCE(origin[0], destination[i][0]) : [])
      }
      //...rest of your code
      }

    编辑

    我调整了您的 GOOGLEMAPS_DISTANCE 函数,使其现在可以与矩阵一起使用。

    const GOOGLEMAPS_DISTANCE = (locations, mode = 'driving') => {
     
     if(locations.map) {
          return locations.map(location => GOOGLEMAPS_DISTANCE(location));
      }
    
    const [origin, destination] = locations.split("_");
     
     const key = ['distance', origin, destination, mode].join(',');
     // Is result in the internal cache?
     const value = getCache(key);
     // If yes, serve the cached result
     if (value !== null) return value;
     const { routes: [data] = [] } = Maps.newDirectionFinder()
       .setOrigin(origin)
       .setDestination(destination)
       .setMode(mode)
       .getDirections();
     if (!data) {
       return 'no data found'
     }
     const { legs: [{ distance: { text: distance } } = {}] = [] } = data;
     // Store the result in internal cache for future
     setCache(key, distance);
     return distance;
    };

    请注意,自定义函数调用必须在 30 秒内返回。所以也许计算整个矩阵(A2:A20 和 E1:Z1)会太多。

    我设法让它与矩阵的一部分一起工作(查看屏幕截图并注意更改后的公式)。

    所以也许您可以处理一些列,然后“冻结”结果(复制、粘贴为值),然后继续处理其他列?

    看看有没有帮助?

    【讨论】:

    • 您好,感谢您的回复。我已经尝试过这种方式,但它仍然不接受数组输入并抛出此错误:TypeError: Cannot read property '0' of undefined(第 22 行)。你知道哪里出了问题吗?
    • 用您的代码(以及我添加的代码)尝试了几个位置,效果很好。您能否分享您的电子表格(已删除敏感数据),以便我仔细查看?
    • docs.google.com/spreadsheets/d/… 这里是电子表格副本的链接。我删除了所有敏感数据和所有编辑限制。
    • 我又试了几次,如果阵列都是垂直的,它确实有效。所以,非常感谢。虽然,我想做的是创建一个具有垂直和水平数组的距离矩阵(正如您可能在示例电子表格中看到的那样),并且由于某种原因,这不适用于此代码。我认为问题是我要输入的两个数组是不同的形状。你知道为什么这可能是一个问题吗?
    • 这很好用,但问题是矩阵必须是动态的,因为左侧数组不断变化,所以我不能冻结这些值。您是否认为可以让代码在没有自定义公式的情况下计算整个矩阵?由于 Apps 脚本执行时间限制为 6 分钟,因此有时间完成所有操作,然后将其复制进去。我想到的另一种可能性是让代码使用单独的 Google 表格,而不是保存结果6小时缓存,然后可以从那里查找所有以前的结果。
    猜你喜欢
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多