Skip to content

Google Apps Scripting

Versión en español de esta publicación.
Recently, I had some problems with my google calendar. A few days ago I made a mistake while trying to import a custom calendar that I needed into my google calendar account,

The custom calendar events got imported correctly into my google calendar account but it was too late when I noticed that the calendar I imported was the wrong one, ohhh, I was shocked to discover that there is no way to undo the long list of imported events from my calendar, at least not in an intuitive way as I through it could be.

Googling a little bit I saw there is a google scripting language that can be used to manipulate google apps entities. I saw a list of scripts and examples to get my calendar events, and also I found a script in the forums which I modified and use to mass delete the unwanted events from my calendar. I have to say I was lucky that there was a pattern to identify the unwanted events, in this case, I just used the event titles, they started with the same word :), if there were no pattern it may be possible to implement a different approach depending on the needs.

Here is the js code to delete such events maybe someone could find it useful,

In order to use this code just copy/paste it and modify the parameters to your needs using the online script editor located at http://www.google.com/script/start/ maybe I will be reading more code from the google apps API’s if time allows.

google_scripting.js

function deleteEvents() {
    var fromDate = new Date(2013,0,1,0,0,0);
    var toDate = new Date(2017,0,4,0,0,0);  
    var calendarName = 'Yohan Jasdid';  
    var eventStartStr = 'Sunrise';
     
    var calendar = CalendarApp.getDefaultCalendar();
    var events = calendar.getEvents(fromDate, toDate);
    for(var i = 0; i < events.length; i++){
        var ev = events[i];
        if (ev.getTitle().substring(0, eventStartStr.length) === eventStartStr) {
            Logger.log(ev.getTitle()); // show event name in log
            ev.deleteEvent();
            Utilities.sleep(100);
        }
    } 
}

Cheers! 🙂
-Yohan

Leave a Reply

Your email address will not be published. Required fields are marked *