Is it possible to use regex with search parameter in CalendarApp.getEvents()?

Viewed 579

I'm trying to filter my Google Calendar events by name using getEvents(startTime, endTime, options), but need to exclude some special events.

var events = cal.getEvents(new Date("September 27, 2019 00:00:00 CST"), new Date("February 18, 2020 23:59:59 CST"), {search: 'MyEvent.*(VO|UE)'});

Is there a way to only get Events that contain "MyEvent", but also "VO" or "UE"?

2 Answers

As of now, it is not possible to query your events with the API or with Apps Script.

However, you could achieve this by filtering the information with the filter() function.

It would look like this:

function myFunction() {
  var cal = CalendarApp.getDefaultCalendar();
  var events = cal.getEvents(new Date("September 27, 2019 00:00:00 CST"), new Date("February 18, 2020 23:59:59 CST"), {search: 'MyEvent'}); //Still searching for MyEvent since that needs to be always present.
  var filteredEvents = events.filter(function  (event) {
                                     return event.getTitle().match('MyEvent.*(VO|UE)')!==null;
                                     });
  Logger.log(filteredEvents.map(function(event) { return event.getTitle(); }));
}

I hope this helps you!

I'm not sure about regular expressions, but you might be able to leverage the optional q parameter of the Calendar API's Events.list endpoint to perform a Free Text search.

Use of the term is primarily documented under the Drive API but those guides should be applicable (at least in part) to the Calendar API as well.

Note that you'll have to leverage either the Advanced Service for Google Calendar or query the Calendar REST API directly using UrlFetchApp.fetch().

Related