Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda