Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
AppScript: Google Calendar Remove Events

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.

enter image description here

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
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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';
      }
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!