Maps.NewDirectionfinder()。SetArrive& setDepart(Google Maps api)在gsheets应用程序脚本AREN AREN;
我有两个实现方法,试图获得任意驾驶路线的持续时间,并使用Google表中的应用程序脚本设置到达或出发时间。我已经用多个起源,目的地和时间组合对它们进行了测试,但是我无法返回到达或出发时间有所不同的持续时间。我已经验证了直接访问Google地图时的路线时间确实有所不同。
这是
ed7plm/copy”时间在脚本中进行了硬编码,但是我已经为测试而变化了):
function GetDuration(location1, location2, mode) {
//var arrive= new Date(2022, 07, 04, 18);// 7th of July 06:00 am
var arrive= new Date(2022, 07, 04, 17);
//var arrive = new Date(new Date().getTime() + (10 * 60 * 60 * 1000));//arrive in ten hours from now
//var directions = Maps.newDirectionFinder().setDepart(arrive)
var directions = Maps.newDirectionFinder().setArrive(arrive)
.setOrigin(location1)
.setDestination(location2)
.setMode(Maps.DirectionFinder.Mode[mode])
.getDirections();
return directions.routes[0].legs[0].duration.text;
}
实现2(时间是变量 adrive
从Gsheet读取):
const GOOGLEMAPS_DURATION = (origin, destination, adrive, mode = "driving") => {
if (!origin || !destination) {
throw new Error("No address specified!");
}
if (origin.map) {
return origin.map(DISTANCE);
}
const key = ["duration", origin, destination, adrive, mode].join(",");
const value = getCache(key);
if (value !== null) return value;
const { routes: [data] = [] } = Maps.newDirectionFinder()
.setOrigin(origin)
// .setDepart(adrive)
.setArrive(adrive)
.setDestination(destination)
.setMode(mode)
.getDirections();
if (!data) {
throw new Error("No route found!");
}
const { legs: [{ duration: { text: time } } = {}] = [] } = data;
setCache(key, time);
return time;
};
如何使其中一个实现方法与出发时间或到达时间一起工作?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请在下面找到自定义功能,以获取
MAPS
服务的驾驶或步行距离和其他此类数据。功能检查参数,可以一次迭代较大的值范围,并使用cacheservice
来缓存结果长达六个小时,以避免超过速率限制。要找到一个驾驶距离,您只需要指定
start_address
和end_address
。要查找驾驶时间,您需要另外指定
单位
的“小时” 或“分钟”
,travel_mode
和dection_time
。请注意,您需要指定未来的时间开始旅行,因为持续时间取决于这是否是高峰时间和其他此类事情。该函数使用。结果在
duration_in_traffic
.getDirections()响应。请注意,该字段仅在出发时间不是过去而是将来才能提供。。要测试函数,请将DateTime值在将来的中在
d2:d
中,然后将此公式插入单元格j2
:= googlemapsdistance(A2:A13,B2:B13,“分钟”,“驾驶”,D2:D13)
参见 Directions示例/流量信息示例/流量信息以获取更多信息。
Google Maps Direction查询的消费者帐户配额每天1,000个电话,而Google Workspace域帐户每天10,000个电话。结果的缓存有助于避免超过极限。请参阅 Google Services Services的配额。
Please find below a custom function to get driving or walking distances and durations and other such data from the
Maps
service. The function checks arguments, can iterate over larger ranges of values in one go, and usesCacheService
to cache results for up to six hours to help avoid exceeding rate limits.To find a driving distance, you only need to specify
start_address
andend_address
.To find a driving duration, you need to additionally specify
units
of"hours"
or"minutes"
, thetravel_mode
, anddepart_time
. Note that you need to specify the future time you will start the trip, because durations depend on whether it is a rush hour and other such things.The function accomplishes the duration fetch using .setDepart(). The result is in the
duration_in_traffic
field in the .getDirections() response. Note that the field is only available when the departure time is not in the past but in the future.To test the function, put datetime values that are in the future in cells
D2:D
, then insert this formula in cellJ2
:=GoogleMapsDistance(A2:A13, B2:B13, "minutes", "driving", D2:D13)
See Directions examples / Traffic information for more information.
The consumer account quota for Google Maps Direction queries is 1,000 calls per day, while for Google Workspace Domain accounts it is 10,000 calls per day. The caching of results helps avoid exceeding the limit. See Quotas for Google Services.
的是,我想建议使用事件或触发器,并为所有途径/到达和目的地进行更新
。
有趣 某些问题其他已经在第三方组件(如Map+Sheets+Zapier)中看到了,这可能有助于您寻找格式化数据以正确更新,请参阅在这里
Instant vs。民意调查:Google表扳机被标记为“瞬间”,但触发仍需几分钟。 Google表的触发器在Zapier触发器中是独一无二的。当电子表格中有一个触发事件时,Zapier就此从Google获得了通知Webhook。之后,Zapier向Google表发送了新数据请求,因此它同时使用了轮询和即时触发方法。这个过程大约需要3分钟。
That's interesting, I would like to suggest using events or trigger, and update for all the routes/arrivals and destinations
Some additional reading..
Common problems: Before you begin there are some issues others have seen with 3rd party components like maps+sheets+zapier, that might help you look for formatting the data to correctly update, please see here
Instant Vs. Polling: The Google Sheets trigger is marked "instant" but it still takes a few minutes to trigger. The triggers for Google Sheets are unique among Zapier triggers. When there is a trigger event in the spreadsheet, Zapier gets a notification webhook from Google about this. After that, Zapier sends Google Sheets a request for new data, so it uses both the polling and instant trigger methods. This process takes about 3 minutes overall.