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

197
Vistas
Google app script delete event from 2 possible calendar

I would like to delete calendar event form 2 possible calendar. I try to define 2 calendar IDs using OR || but if calendar id return null on first try the deleteevent become error. Anyone can help?

var calendarId = "calendarID1" || "calendarID2" ;
var eveId = 2;
//var typeId = 3;

function deleteEvent() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var lr = rows.getLastRow();

  var eventID = sheet.getRange(lr, eveId, 1, 1).getValue();
  //var type = sheet.getRange(lr, typeId, 1, 1).getValue();

  var cal = CalendarApp.getCalendarById(calendarId);
  var event = cal.getEventSeriesById(eventID);
  event.deleteEventSeries();

}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Description

A few things about my coding preferences first. I always wrap my code in a try{} catch(err){} block. That way if I make a coding mistake it shows up and if there is an error during execution during testing it shows up in the Execution transcript. If you are testing through script editor you could replace Logger.log with console.log. Secondly, I avoid global variables. I've also eliminated some of your redundant code.

Your code var calendarId = "calendarID1" || "calendarID2" ; would result in calendarId = false, not an Id. Instead use an array to contain both Ids. Then use an Array.forEach() to loop through the Ids as shown below.

Code.gs

function deleteEvent() {
  try {
    var calendarIds = ["calendarID1","calendarID2"];
    var eveId = 2;
    var sheet = SpreadsheetApp.getActiveSheet();
    var rows = sheet.getDataRange();
    var lr = rows.getLastRow();

    var eventID = sheet.getRange(lr, eveId, 1, 1).getValue();
    //var type = sheet.getRange(lr, typeId, 1, 1).getValue();

    calendarIds.forEach( calendarId => {
        let cal = CalendarApp.getCalendarById(calendarId);
        let event = cal.getEventSeriesById(eventID);
        if( event) event.deleteEventSeries();
      }
    );
  }
  catch(err) {
    Logger.log(err);
  }
}

Reference

  • Array.forEach()
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