Tuesday 28 February 2023

Delete Google Calendar Events by keyword and date range

The following Google Apps Script is designed to search a date range for Google Calendar events containing a given string (something to help target those events) and then delete them.

Search for and delete Google Calendar events

Instructions

There are 4 items at the beginning of the script to complete for your requirements:

  1. Start date - format mm/dd/yyyy - forms the date range to look for events within.
  2. End date - format mm/dd/yyyy - forms the date range to look for events within.
  3. Search string - bit of text that is unique to the events you wish to delete to differentiate them from other Calendar events you may not wish to remove. Note: is not case-sensitive and will not pick-up search string inside of another word eg 'the' in 'there'.
  4. Calendar ID - typically your email address for your personal Google Calendar to search for events in.


The Code

There are a few bits of the code I want to draw attention to. We use the 'CalendarApp' to get the Google Calendar that is to be scanned for events:

var calendar = CalendarApp.getCalendarById(calendarID);

In order to get all of the events we feed in the start/end date along with some additional 'options' - specifically search to filter the results:

var options = { search: searchstring };
var existingEvents = calendar.getEvents(startDate, endDate, options);

For the rest of the code we can loop through the array of results and eventually 'deleteEvent'.



Download

Delete Google Calendar Events by keyword and date range download (please use 'Overview' > 'Make a copy' for your own version).

No comments:

Post a Comment