Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Google Calendar events based on event title

For some reason, a series of events have been created in my calendar that are not considered as recurring events. As we're talking about 2 events per week for the next 3 years, I'm looking to Google Apps Script to delete these events from my calendar.

The events have a single, specific name and are created by me. I want to delete all events in the future that have that specific name and are created by me.

As I'm completely new to scripting, I tried the sample code below. Unfortunately, it doesn't do anything in a test calendar I've setup (Test). The debugging option in Google Apps Script doesn't generate any errors or messages. Running the script doesn't generate any logs either...

So, who can help to point out what is wrong with the code below and how can I delete specific events, based on the event title and creator of that event?

function delete_events()
{
    var calendarName = 'Test';
    // for month 0 = Jan, 1 = Feb etc
    // below delete from Jul 13 2020 to Jul 18 2020
    var fromDate = new Date(2020,7,13,0,0,0); 
    var toDate = new Date(2020,7,18,0,0,0);
    var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
    var events = calendar.getEvents(fromDate, toDate);
    for(var i=0; i<events.length;i++){
        var ev = events[i];
        // show event name in log
        Logger.log(ev.getTitle()); 
        ev.deleteEvent();
     }
}
like image 381
MDurie Avatar asked Sep 18 '25 05:09

MDurie


2 Answers

Ok, I think I managed to solve it. I was able to delete specific events that were linked to a specific creator and title. Other events with the same title but different creator are still in the agenda.

function delete_events()
{
    var calendarName = 'Test';
    // for month 0 = Jan, 1 = Feb etc
    // below delete from Jul 13 2020 to Jul 18 2020
    var fromDate = new Date("2020-07-13"); 
    var toDate = new Date("2020-07-18");
    var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
    var events = calendar.getEvents(fromDate, toDate);
    for(var i=0; i<events.length;i++){
        var ev = events[i];
        if(ev.getTitle()=="EventX" & ev.getCreators()=="[email protected]"){
        // show event name in log
        Logger.log(ev.getTitle()); 
        ev.deleteEvent();
     }
}
}
like image 183
MDurie Avatar answered Sep 20 '25 02:09

MDurie


Make sure you set the dates correctly and query for the creators[]

  • As pointed out in the comments to you question and in the comment inside your code - Javascript counts the months starting with 0
  • Apps Script feature the methods getCreators() which returns you an array(!) of potentially several creator
  • Thus, you cannot make the comparison with ==, but need to use e.g. indexOf()

Sample:

function delete_events()
{
  var calendarName = 'Test';
  var myEmail = "YOUR EMAIL";
  var myTitle = "Hello";
  // for month 0 = Jan, 1 = Feb etc
  // below delete from now to Jul 18 2020
  var now = new Date(); 
  var toDate = new Date(2020,6,18,0,0,0);
  var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
  var events = calendar.getEvents(now, toDate);
  for(var i=0; i<events.length;i++){
    var ev = events[i];
    // show event name in log
    Logger.log(ev.getTitle()); 
    var creators = ev.getCreators();
//check if you are the calendar creator and the event title matches
    if(creators.indexOf(myEmail) >-1 && ev.getTitle() == myTitle){
      ev.deleteEvent();
    }
  }
}
like image 35
ziganotschka Avatar answered Sep 20 '25 03:09

ziganotschka