MDurie
MDurie

Reputation: 71

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();
     }
}

Upvotes: 4

Views: 7635

Answers (2)

MDurie
MDurie

Reputation: 71

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();
     }
}
}

Upvotes: 3

ziganotschka
ziganotschka

Reputation: 26836

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();
    }
  }
}

Upvotes: 1

Related Questions