Google 表格中 GOOGLEMAPS 自定义应用程序脚本函数的数组输入

Array input for GOOGLEMAPS custom apps script function in Google Sheets

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

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;
};

目前,该代码能够找到两个给定地址之间的距离,return 它可以用于单个输入。为了绕过 Google 的 API 请求限制,我添加了缓存先前值的功能。此外,代码会在遇到错误时重新运行(例如 API 请求限制)。

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

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

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
}

注意:如果你想 return 空 rows/cells,请尝试:

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 秒内 return。因此,也许计算整个矩阵(A2:A20 和 E1:Z1)就够了。

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

那么也许您可以处理一些列,然后 'freeze' 结果(复制、粘贴为值)然后继续处理其他列?

看看有没有帮助?