Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda