从 google 电子表格更新日历

Update calendar from google spreadsheet

我想从电子表格更新我的日历:

我尝试了以下方法:

function caltest1() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1;   // Number of rows to process
  var dataRange = sheet.getRange(startRow, 1, numRows, 5);
  var data = dataRange.getValues();
  var calId = "TestCalendar"; 
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    var row = data[i];
    var title = row[0];  // First column
    var desc = row[1];       // Second column
    var tstart = row[2];
    var tstop = row[3];
    var loc = row[4];
    //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
    cal.createEvent(title, tstart, tstop, {description:desc,location:loc});
 }
}

但是,我收到以下错误:

TypeError: Cannot call method "createEvent" of null

使用了以下电子表格:

Link to spreadsheet

任何建议,我做错了什么?

感谢您的回复!

简短回答:您将日历的名称传递给 getCalendarById(),因此它返回 null。将 "TestCalendar" 更改为日历的 ID 或使用 getCalendarsByName().

我将按照我阅读的方式引导您完成此操作,以便您了解一种调试方法。

此错误告诉您您的 cal 变量在您调用 cal.createEvent( ... ) 的最后一行是 null。这意味着您之前的语句 var cal = CalendarApp.getCalendarById(calId); 正在返回 null。谷歌搜索此函数的 Google 日历 API 我看到您使用的是 getCalendarId() 的正确语法,但您传递的字符串参数似乎是日历的名称而不是 ID。