简体   繁体   English

Google Apps Script Calendar Service:如何避免超过最大执行时间?

[英]Google Apps Script Calendar Service: How to avoid exceeding maximum execution time?

I need to edit a few hundred or even a few thousand calendar events through Google Apps Script.我需要通过 Google Apps Script 编辑几百甚至几千个日历事件。 It is about just minor changes to title ( event.setTitle() ) and description ( event.setDescription() ), nothing fancy.这只是对标题( event.setTitle() )和描述( event.setDescription() )的微小更改,没什么特别的。 Trying with about 600 events the maximum execution time of 360 seconds is already exceeded.尝试处理大约 600 个事件,已超过 360 秒的最大执行时间。

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?如何连续处理多个块中的事件?

Answer:回答:

Use PropertiesService to save where you got to so you can continue where you left of on next run.使用PropertiesService保存您到达的位置,以便您可以在下次运行时继续离开的位置。

Code:代码:

You can use a PropertiesService key value pair to save the value of the count through events[] :您可以使用PropertiesService键值对通过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.它会使脚本变慢一点,但是每次运行它时,它都会从上次停止的日历事件中继续运行。

I hope this is helpful to you!我希望这对你有帮助!

References:参考:

I have previously used something similar to the following:我以前使用过类似于以下内容的内容:

At the start of each loop check that a sufficient buffer time is available to complete the loop.在每个循环开始时,检查是否有足够的缓冲时间来完成循环。

Update the buffer time whenever a single loop time exceeds it.每当单个循环时间超过缓冲时间时更新缓冲时间。

Use the PropertiesService to store both the buffer and the last index.使用PropertiesService存储缓冲区和最后一个索引。

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 ps = PropertiesService.getScriptProperties();
var startIndex = ps.getProperty('lastIndex') || 0;
var buffer = ps.getProperty('buffer') || 100;

var startTime = new Date();
var maxTime = 1000*60*6; //6 mins


for (var i = startIndex; i < events.length; i++) {
  var loopStart = new Date()
  if (loopStart - startTime > (maxTime-buffer) ) {
     ps.setProperty('lastIndex', i);
     break;
  }
  events[i].setTitle(events[i].getTitle() + " something");
  events[i].setDescription(events[i].getDescription() + " something else");
  var loopTime = new Date() - loopStart;
  if (loopTime  > buffer ) buffer = loopTime * 1.5
}

ps.setProperty('buffer',buffer) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM