Home > Software design >  TypeError: Cannot read property 'createAllDayEvent' of null when creating GoogleScripts au
TypeError: Cannot read property 'createAllDayEvent' of null when creating GoogleScripts au

Time:10-13

First of all, I am a very new programmer, so I apologize for my ignorance. Regardless, I wrote this code today to take data from a google sheet, and import that to a google calendar.

//create deposit date 
function sheetsToCalendar() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
  var eventCal = CalendarApp.getCalendarById("[omitted]@group.calendar.google.com");
  var lastRow = spreadsheet.getLastRow();

  var depositDate = spreadsheet.getRange(lastRow, 28).getValue(); 

  Logger.log(depositDate)
  eventCal.createAllDayEvent('Deposit Date', depositDate)

}

The intention being that anytime the program is executed,that the last cell of column 28 is the date being added to the google calendar.

However, when it is run, I get the error TypeError: Cannot read property 'createAllDayEvent' of null.

The Logger.log(depositDate) produces the correct date assigned in the spreadsheet, so I am not sure where to look.

Thank you again :)

CodePudding user response:

This works for me:

function sheetsToCalendar() {
  const sh = SpreadsheetApp.getActiveSheet();
  const cal = CalendarApp.getCalendarById(gobj.globals.calendarid);//contains a global object parameter that holds my calendar id
  const depositDate = new Date(sh.getRange(sh.getLastRow(), 28).getValue());//try using the new Date() constructor to get the date from a spreadsheet
  Logger.log(depositDate)
  cal.createAllDayEvent('Deposit Date', depositDate);//this was created on my calendar
}
  • Related