'Delete events from calendar that were previously created via

I have created event with this script:

function createCalendarEvent() {
let communityCalendar = CalendarApp.getCalendarById("calendar_ID");
let sheet = SpreadsheetApp.getActiveSheet();
let schedule = sheet.getDataRange().getValues();
schedule.splice(0, 2);
schedule.forEach(function(entry){
  communityCalendar.createEvent(
    entry[2],
    entry[0],
    entry[1],
              {
                guests: entry[3],
                sendInvites: true
              }
);
});
}

Now if I have made a mistake and would like to delete those events, how do I do that?

Thought it's easy as changing

communityCalendar.createEvent

to

communityCalendar.deleteEvent

But it won't work (obviously)

TypeError: communityCalendar.deleteEvent is not a function
(anonimowy) @ Kod.gs:7
createCalendarEvent @ Kod.gs:6

Much thanks,

Bartosz



Solution 1:[1]

As mentioned in Cooper's comment, you need to first retrieve an Event object before calling deleteEvent() on it. The safest way would be to store the IDs on another sheet. Luckily, createEvent() returns the Event object after you create it so you can get its ID right away. As an example based on your code:

const createdEvents = [] //blank array that will contain the list of created event IDs

schedule.forEach(function(entry){

//push the event IDs to the array
  createdEvents.push(communityCalendar.createEvent(
    entry[2],
    entry[0],
    entry[1],
              {
                guests: entry[3],
                sendInvites: true
              }
).getId()
//calling communityCalendar.createEvent(...).getId() will return the event ID
//as a string like "[email protected]"
)
});

At this point you can paste the list of IDs in a separate sheet. Then if you need to delete the events you can get the IDs from the sheet and use getEventByID() to process them

function deleteEvents(){
  let communityCalendar = CalendarApp.getCalendarById("calendar_ID");
  let sheet SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Events List");
  let list = sheet.getRange("range where you pasted the IDs").getValues();

  list.forEach(function(entry){
    communityCalendar.getEventByID(entry).deleteEvent()
  });

}

Alternatively, you could try to "reverse" your original function by using the original spreadsheet data to search for the events with getEvents(). This would probably work if you're the only one managing this calendar, but if there are other events that were not created by your script that also match the search criteria they would be deleted as well.

It would look something like this:

function deleteEvents() {
let communityCalendar = CalendarApp.getCalendarById("calendar_ID");
let sheet = SpreadsheetApp.getActiveSheet();
let schedule = sheet.getDataRange().getValues();
schedule.splice(0, 2);
schedule.forEach(function(entry){

  communityCalendar.getEvents(
    entry[0],//getEvents() takes starttime and endtime as well so we can reuse them
    entry[1],
              {
                search: entry[2], 
//from your code we know that entry[2] is the title so we can use it as a search query
              }
)[0].deleteEvent(); //getEvents() returns an array so just take the first position and delete it

});
}

There's room for improvement but I hope this gives you a general idea of how it works. I would recommend to just store the IDs to avoid mistakes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel