脚本尝试将全天事件导入日历时的错误代码

时间:2017-10-17 17:53:34

标签: google-calendar-api

我一直在使用相同的脚本将约会导入日历2年,没有任何问题。今天突然间,我收到一个读取TypeError的错误代码:在对象Calendar中找不到函数createAllDayEvent。 (第35行,文件"代码")

为什么会这样?我们使用此脚本来安排公司交付,所以我真的需要它来工作。任何帮助将不胜感激!!

这是我一直在使用的脚本......

function importCalendar() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var headerRows = 1;  // Number of rows of header info (to skip)
    var range = sheet.getDataRange();
    var data = range.getValues();
    var calId = "CALENDAR ID HERE";
    var cal = CalendarApp.getCalendarById(calId);
    for (i=0; i<data.length; i++) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var startDate = row[3];  // Fourth column
    var title = row[1];           // Second column
    var location = row[2];
    var description = row[4];
    var id = row[6];              // Seventh column == eventId
    var advancedArgs ={description: description, location: location};
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
    }
    if (!event) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createAllDayEvent(title, new Date(startDate), advancedArgs).getId(); This is the row with the error.
      row[6] = newEvent;  // Update the data array with event ID
    }
    else {
      Utilities.sleep(5000);
      event.setTitle(title);
      event.setDescription(description);
      event.setLocation(location);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, new Date(startDate));
    }
    debugger;
    }
  // Record all event IDs to spreadsheet
    range.setValues(data);
}

1 个答案:

答案 0 :(得分:0)

您可以参考此主题:Cannot find function createEvent (or createAllDayEvent) in object Calendar. (Google Spreadsheet App)。确保startDate变量的格式正确。

  

如果你不确定'date'变量实际上是一个日期   你可以用

  cal.createAllDayEvent(title, new Date(date), {description:desc,location:loc});
     

说,用记录器检查很容易

Logger.log(date)
     

应以Tue Sep 18 03:00:00 PDT 2012

的形式返回日期值

其他参考:Google script google sheet to calendar TypeError: Cannot find function createEvent in object Calendar. (line 18, file "Code")