Cannot find function createEvent (or createAllDayE

2019-07-29 06:40发布

问题:

Okay here is my code that is causing the error. It seems to be just the line where I attempt to add the all day event. I thought it was a date formatting issue but the line I commented out below it was created to test that and it resulted in the same error. So I am stumped. What needs to change to correct the error, or what does the error actually mean.

    function cal1() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var startRow = 2;  // First row of data to process
      var numRows = 300;   // Number of rows to process
      var dataRange = sheet.getRange(startRow, 1, numRows, 26); //What data will be used
      var data = dataRange.getValues();
      var cal = CalendarApp.getCalendarsByName("Production Schedule"); //Gets the correct calendar
    for (i in data) {
      var row = data[i];
      var title = row[4];  // Column with Title
      var desc = row[3];       // Column with Description
      var date = row[8];   //Column with Date
      var loc = row[5];    //Column with Location
      cal.createAllDayEvent(title, date, {description:desc,location:loc});
      //cal.createEvent("Test", new Date("3/10/2010 14:30:00"), new Date("3/10/2010 14:30:00"));  
  }
 }

回答1:

  var cal = CalendarApp.getCalendarsByName("Production Schedule"); //Gets the correct calendar

That gives you an array of calendars with that name (since Calendars can share names and therefore there can be more than one). Try this:

  var cal = CalendarApp.getCalendarsByName("Production Schedule")[0]; //Gets the correct calendar

That will give you the first (likely only) Calendar with that name.



回答2:

if you are not sure about the 'date' variable being actually a date you could use

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

that said, it is quite easy to check with the logger

Logger.log(date)

should return a date value in the form Tue Sep 18 03:00:00 PDT 2012



回答3:

I have same problem with getCalendarsByName. Rather, I use getCalendarById and it solved well. I think calendar name can get some confusion though, and bug.

You can find your calendar id on calendar setting, something like: 123xyz@group.calendar.google.com

Script will goes like this:

var cal = CalendarApp.getCalendarById("123xyz@group.calendar.google.com");