I have the following code that uploads my events from Google Sheet to Google Calendar. This code searches the rows that need to be synced to the calendar based on value in column E (status) x[4]
. Events that are marked as Pending Schedule
or Pending Removal
will be synced to the calendar and then update the respective rows in the sheet with Scheduled
or Removed
.
This code runs just fine in terms of scheduling events. But when it comes to removing events it only works if the first row of the data is marked to be removed, but not on other rows. It gives me TypeError: Cannot read property 'deleteEvent' of undefined
and when I investigated the deleteEvent
object shows as CalendarEvent
.
Seems like the way I have written the if()
functions is somehow not appropriate and the else if()
does not evaluate the whole data. I have tried replacing else if()
with another if()
but received the same error.
function calendarSync2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName('Test Sheet')
var settingsSheet = ss.getSheetByName('Calendar Settings');
var calendarId = settingsSheet.getRange('B2').getValues();
var eventCal = CalendarApp.getCalendarById(calendarId);
var eventArray = dataSheet.getRange('A2:I').getDisplayValues();
for (x = 0; x < eventArray.length; x++) {
var event = eventArray[x];
var eventName = event[1];
var eventDate = new Date(event[0]);
var timeZone = event[5];
var startTime = new Date(`${event[0]} ${event[2]} ${timeZone}`); ///Used Object literals of `` combined with ${}
var endTime = new Date(`${event[0]} ${event[3]} ${timeZone}`);
var status = event[4]; // Used in the following comparison expression instead of filter
if (status === 'Pending Schedule') { /// === undertakes strict comparison
var exisEvents = eventCal.getEvents(startTime, endTime, {search: eventName}); // prevents creation of duplicates
if (exisEvents.length == 0) {
Logger.log(`timed ${eventName} scheduled for: ${startTime} till ${endTime}`);
eventCal.createEvent(eventName, startTime, endTime);
eventArray[x][4] = "Scheduled"; // Updates the status
}
} else if (status === 'Pending Removal') {
Logger.log(`${eventName} at ${eventDate} REMOVED`)
var returnedEvent = eventCal.getEvents(startTime, endTime, {search: eventName});
var toRemove = returnedEvent[x];
Logger.log(`returnedEvent is: ${returnedEvent}`);
toRemove.deleteEvent();
eventArray[x][4] = "Removed";
}
}
dataSheet.getRange('A2:I').setValues(eventArray); // Overwrite the source data with the modified array
}
So with a little bit of digging I figured I need to create another loop for removing events. so the following code:
else if (status === 'Pending Removal') {
var returnedEvent = eventCal.getEvents(startTime, endTime, {search: eventName});
var toRemove = returnedEvent[x];
toRemove.deleteEvent();
eventArray[x][4] = "Removed";
}
Should be changed to:
else if (status === 'Pending Removal') {
var returnedEvents = eventCal.getEventsForDay(eventDate,
{search: eventName});
for (i = 0; i < returnedEvents.length; i++) {
var toRemove = returnedEvents[i];
toRemove.deleteEvent();
eventArray[x][4] = 'Removed';
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.