Nolan Pestano
Nolan Pestano

Reputation: 167

TypeError: Cannot read property 'createAllDayEvent' of null when creating GoogleScripts automation

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 :)

Upvotes: 0

Views: 1585

Answers (2)

Nícolas Gomes
Nícolas Gomes

Reputation: 11

I figured out that you can't put a ID from a different account that you using in your App Script's project in the getCalendarById function.

For example, if you are editing your script in the "[email protected]" you need to create an calendar ID using the same email account.

This fixed the null error for me.

Upvotes: 1

Cooper
Cooper

Reputation: 64062

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
}

Upvotes: 1

Related Questions