So I'm pretty new to coding with GAS and I've been working on this the other day. The idea is to have a spreadsheet of events and create them in my calendar and check if the event already exists and update it if there are any changes to the name or date of the event.
Here's my code for reference:
function events() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("test");
const cal = CalendarApp.getCalendarById("calID");
for (i = 3; i <= sheet.getLastRow(); i++) {
var date = sheet.getRange(i, 8).getValue();
if (date > 0) {
var title = sheet.getRange(i, 3).getValue();
var id = sheet.getRange(i, 9).getValue();
if (id != 0) {
var ide = cal.getEventById("id");
var idf = ide.getAllDayStartDate();
var idn = ide.getTitle();
if (date != idf) {
ide.setAllDayDate(fecha);
}
if (title != idn) {
ide.setTitle(nombre);
}
}
else {
var nid = cal.createAllDayEvent(name, date).getId();
sheet.getRange(i, 9).setValue(nid);
}
}
}
}
I was able to create the events succesfully but when it checks the date of an already existing one it says this:
Error TypeError: Cannot read property 'getAllDayStartDate' of null
Later, using the Logger.log() I found out that this is returning as null even tho the event with that ID exists and it has access to the calendar:
var ide = cal.getEventById("id");
I then looked online and found this but when i tried running the code marked as a solution this returns empty:
var event = CalendarApp.getDefaultCalendar().getEvents(now, nextEvent);
I then tried deleting and allowing again the permissions for calendar and sheets but it didn't work. I even tried editing the appsscript.json file by hand like this:
{
...
"oauthScopes": [
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.readonly",
"https://www.google.com/calendar/feeds",
"https://www.googleapis.com/auth/spreadsheets.currentonly",
"https://www.googleapis.com/auth/spreadsheets"
]
...
}
But that didn't work either.
Any help would be much appreciated!
Edit:
I've now tested with the default calendar and creating a new calendar but it doesn't work. I'm starting to think it's a bug but I'm not sure what could be causing it.