【发布时间】:2020-03-29 19:15:15
【问题描述】:
如何将 OnEdit 添加到脚本或防止脚本在特定的 Google 表格单元格中多次运行?我正在使用 Google 地图计算 2 点之间的时间,但服务调用次数过多尝试 Utilities.sleep(1000)。
我正在使用的脚本:
/**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/
function GOOGLEMAPS(start_address,end_address,return_type) {
// https://www.chicagocomputerclasses.com/
// Nov 2017
// improvements needed
var mapObj = Maps.newDirectionFinder();
mapObj.setOrigin(start_address);
mapObj.setDestination(end_address);
var directions = mapObj.getDirections();
var getTheLeg = directions["routes"][0]["legs"][0];
var meters = getTheLeg["distance"]["value"];
switch(return_type){
case "miles":
return meters * 0.000621371;
break;
case "minutes":
// get duration in seconds
var duration = getTheLeg["duration"]["value"];
//convert to minutes and return
return duration / 60;
break;
case "hours":
// get duration in seconds
var duration = getTheLeg["duration"]["value"];
//convert to hours and return
return duration / 60 / 60;
break;
case "kilometers":
return meters / 1000;
break;
default:
return "Error: Wrong Unit Type";
}
}
我希望脚本在计算出值后停止在特定电子表格单元格上运行。 有什么想法吗? 谢谢!
【问题讨论】:
标签: google-maps google-apps-script google-sheets triggers spreadsheet