Skip to content Skip to sidebar Skip to footer

Override Existing Google Calendar Event When New Event Is Created Via Script

I am using a Google Form to schedule appointments based on open events on my Google Calendar. I've sectioned off time slots on the calendar and if the title of the event is 'Open',

Solution 1:

You'll need to call calendar.getEvents() first, then loop through the event(s) you want to delete, and call .delete() on each of them. Here's the API link: https://developers.google.com/apps-script/reference/calendar/calendar#getEvents%28Date,Date,Object%29

Something like this:

var now = new Date();
var twoHoursFromNow = new Date(now.getTime() + (2 * 60 * 60 * 1000));
var events = CalendarApp.getDefaultCalendar().getEvents(now, twoHoursFromNow,
    {search: 'meeting'});

for(int i = 0; i < events.length; i++){
    ifthisis the right event (compare title, times, etc?){
        events[i].deleteEvent();
    }
}

In this case, you'll probably want to compare the descriptions. Also note that you can pass options to the getEvents call; one you can use to filter during the search is the search parameter like I did above. It filters the returned events on that text.

EDIT: Ideally, you'll want to use this: https://developers.google.com/apps-script/reference/calendar/calendar#getEventSeriesById%28String%29

That gets the event by the ID; I answered with the assumption you didn't have the ID stored anywhere though. If you do, all the better, and you'll be certain you're replacing the right event.

Solution 2:

It works! Here is my completed code for adding an event and deleting one that occurs at the same time.

functioncreateEvent(calendarId,title,startDt,endDt,desc) {
  var cal = CalendarApp.getCalendarById(calendarID);
  var start = newDate(startDt);
  var end = newDate(endDt);

  var event = cal.createEvent(title, start, end, {
    description : desc
  });

    var events = cal.getEvents(start, end, {
    search: 'open'
  });  

  for (i in events){
    events[i].deleteEvent();
  }
};

Post a Comment for "Override Existing Google Calendar Event When New Event Is Created Via Script"