为什么我的 Apps 脚本突然停止工作?

Why has my Apps Script suddenly stopped working?

我在 Google Sheet 中使用了以下 Apps 脚本代码几个月没有问题。

每天晚上 9 点到 10 点由时间触发器触发,它从我的 Google 日历中获取同一天的每个事件,并将包含这些详细信息的相应行添加到 Google Sheet.

// Add Google Calendar events to Google Sheets.
// Sheet row additions to fire Zapier ClickTime actions.
// Script originally from https://blog.ouseful.info/2010/03/05/grabbing-google-calendar-event-details-into-a-spreadsheet/

function caltest3(){
  //http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
  // The code below will retrieve events between 2 dates for the user's default calendar and
  // display the events the current spreadsheet
  var cal = CalendarApp.getDefaultCalendar();
  var sheet = SpreadsheetApp.getActiveSheet();

  // Today's date, via 
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth();
  var yyyy = today.getFullYear();


  // Use Google Calendar classes, https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object):

  // Get all events between this range
  var events = cal.getEvents(new Date(yyyy, mm, dd, 0, 0, 0), new Date(yyyy, mm, dd, 23, 0, 0));

  // For every event, 
  for (var i=0;i<events.length;i++) {

    // Calculate hour length of event
    var hours = Math.abs(events[i].getEndTime() - events[i].getStartTime()) / 36e5;

    // Combine elements of event
    // var details=[[events[i].getStartTime(),  events[i].getEndTime(), hours, events[i].getTitle(), events[i].getDescription()]];

    // Appends a new row with columns to the bottom of the spreadsheet containing the values in the array
    sheet.appendRow([events[i].getStartTime(),  events[i].getEndTime(), hours, events[i].getTitle(), events[i].getDescription()]);

  }
}

但是,一夜之间,我从 Google...

收到了这条消息

Your script, Calendar Transfer, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.

The script is used by the document Calendar Listings.

Sincerely, Google Apps Script

最后一次成功添加行是在 2 月 15 日。2 月 16 日或 17 日没有要添加的事件,因此 2 月 18 日是第一次失败。

这是怎么回事?

是否与从经典日历切换到新日历有关?

或者看起来它在添加到 Sheet 时遇到了一些问题?

第 34 行是标记为 Appends a new row ... 的最后一行。

我在该网站上进行了搜索,并在谷歌上搜索了“服务超时”错误。看起来这个错误意味着 Google 服务器响应速度不够快。

NOTE: Google servers response time isn't deterministic. Sometimes they are faster than others and hopefully very rarely the are so slow that the "service timed out" error occurs.

一种解决方案是使用 Bruce McPherson 的 exponential backoff 库或类似的库。

What is exponential backoff

This is recommended technique to use for calling services that are rate limited. They will be retried a few times if they are detected as having failed with errors that can be recovered from using a special wait algorithm. This is a much better technique than using Utilities.sleep between each call since it only waits if it needs to and therefore doesn't waste any execution time.

另一种方法是在 time-driven 触发的脚本失败时创建一个脚本变体 运行。

参考文献: