Google Script:TypeError:无法调用null的方法“createEvent”

时间:2015-09-28 13:04:35

标签: javascript google-api google-calendar-api

这是我的第一个google脚本,它不起作用。当我在脚本编辑器中启动此脚本进行测试时,我得到的错误位于标题“......第51行”上,该错误位于“var event = cal.createEvent(title,start,end,{.. “你能给我一个小费吗?

以下代码是否正常?

var calendarId = "xyz@group.calendar.google.com";

 //Column containg the Start Date/Time for the event
 var startDtId = 4;
 //Column containg the End Date/Time for the event
 var endDtId = 5;
 //Column containg the First Part of the Title for the event (In this case, Name)
 var titleId = 2;
 //Column containg the Comments for the event
 var descId = 3;
 //Column containg the Time Stamp for the event (This will always be 1)
 var formTimeStampId = 1;
 //Column containg the Location
 var locId = 6;
 //Column containg more Informations
 var infosId = 7;

 function getLatestAndSubmitToCalendar() {
     //Allow access to the Spreadsheet
     var sheet = SpreadsheetApp.getActiveSheet();
     var rows = sheet.getDataRange();
     var numRows = rows.getNumRows();
     var values = rows.getValues();
     var lr = rows.getLastRow();
     //Removed setting of Hour and Minute for the Start and End times as these are set in our form
     var startDt = sheet.getRange(lr, startDtId, 1, 1).getValue();
     var endDt = sheet.getRange(lr, endDtId, 1, 1).getValue();
     //Setting the Comments as the description, and addining in the Time stamp and Submision info
     var desc = "" + sheet.getRange(lr, descId, 1, 1).getValue() + "n" + subOn;
     //Create the Title using the Name and tType of Absence
     var title = sheet.getRange(lr, titleId, 1, 1).getValue() + " – " + sheet.getRange(lr, titleId2, 1, 1).getValue();
     //Setting the Location
     var loca = sheet.getRange(lr, locId, 1, 1).getValue();
     //Setting more Information
     var infos = sheet.getRange(lr, infosId, 1, 1).getValue();
     //Run the Crete event Function
     createEvent(calendarId, title, startDt, endDt, desc, loca, infos);
 };

 function createEvent(calendarId, title, startDt, endDt, desc, loca, infos) {
     var cal = CalendarApp.getCalendarById(calendarId);
     var start = new Date(startDt);
     var end = new Date(endDt);
     var loc = loca;

     var event = cal.createEvent(title, start, end, {
         description: desc,
         location: loc
     });
 };

我在下面的帖子中读到了答案,但我的calendar-id肯定是正确的。所以我在这个新帖子中问你。 Google script google calendar TypeError: Cannot call method "createEvent" of null

1 个答案:

答案 0 :(得分:0)

这意味着您的cal对象为null,因此CalendarApp.getCalendarByID(calendarID)

中存在错误