Google Apps脚本日历服务:如何避免超出最长执行时间? [英] Google Apps Script Calendar Service: How to avoid exceeding maximum execution time?
问题描述
我需要通过Google Apps脚本编辑几百个甚至几千个日历事件.它只是对标题(event.setTitle()
)和说明(event.setDescription()
)的微小改动,没有什么花哨的地方.尝试处理大约600个事件,已经超过了360秒的最大执行时间.
I need to edit a few hundred or even a few thousand calendar events through Google Apps Script. It is about just minor changes to title (event.setTitle()
) and description (event.setDescription()
), nothing fancy. Trying with about 600 events the maximum execution time of 360 seconds is already exceeded.
var cal = CalendarApp.getCalendarById("Calendar Id");
var startTime = new Date(1850, 0, 1);
var endTime = new Date(2100, 0, 1);
var events = cal.getEvents(startTime, endTime);
for (var i = 0; i < events.length; i++) {
events[i].setTitle(events[i].getTitle() + " something");
events[i].setDescription(events[i].getDescription() + " something else");
}
如何依次处理多个事件?
How to process the events in several chunks successively?
推荐答案
答案:
使用PropertiesService
保存到达的位置,以便在下次运行时可以从左侧继续.
Answer:
Use PropertiesService
to save where you got to so you can continue where you left of on next run.
您可以使用PropertiesService
键值对通过events[]
保存计数值:
You can use a PropertiesService
key value pair to save the value of the count through events[]
:
function renameEvents() {
var cal = CalendarApp.getCalendarById("Calendar Id");
var startTime = new Date(1850, 0, 1);
var endTime = new Date(2100, 0, 1);
var events = cal.getEvents(startTime, endTime);
var sp = PropertiesService.getScriptProperties();
if (!(sp.getProperty("count")) || sp.getProperty("count") == 0) {
var count = 0;
else if ((sp.getProperty("count") > 0) {
var count = sp.getProperty("count");
}
for (var i = count; i < events.length; i++) {
events[i].setTitle(events[i].getTitle() + " something");
events[i].setDescription(events[i].getDescription() + " something else");
sp.setProperty("count", i)
}
}
这会使脚本变慢一些,但是每次运行它时,它将沿着上一个停止的日历事件继续进行.
It'll make the script a little slower, but each time you run it it'll continue along the calendar events from where the last one stopped.
希望对您有帮助!
- Class
PropertiesService
| Apps Script | Google Developers
这篇关于Google Apps脚本日历服务:如何避免超出最长执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!