google-apps-scriptcalendargoogle-calendar-apigoogle-calendar-recurring-events

How to modify a specific instance of an event series


Let's suppose I create a recurring event that starts on Monday at 9 AM and ends at 11 AM, this event repeats every day for 3 days. Now I want (after I have created the events using recurrence) to change the start time of the event on Tuesday while leaving the other events unchanged, how could I do ?

I can easily get the recurrence rule for this eventSeries using the advanced calendar API, it returns something like

RRULE:FREQ=DAILY;COUNT=3 

This rule can be modified at will to change all the events (and I can also update the events using patch but not just for a single event. I tried the API tryout to get every instance of this eventSeries and I can indeed see every start and end times (*see below) but I didn't find any method to get that using the Calendar API in Google Apps Script.

I didn't find a way to modify a particular instance, i.e. to write back the modified values.

Since I can do this manually in the calendar web UI I guess I should be able to do it using a script but I really don't know how.

The code I use to get the event parameters is fairly simple :

  ...
  for(var n = 1 ; n < data.length ; n++){
    if(data[n][9] == ''){continue};
    var advancedID = data[n][9].substring(0,data[n][9].indexOf('@'));
    Logger.log(advancedID+'\n');
    ChangeEventRecurrence(calID,advancedID);
  }
}

function ChangeEventRecurrence(calID,advancedID){
  var event = Calendar.Events.get(calID, advancedID);
  Logger.log(event.recurrence+'\n'+JSON.stringify(event));
  // here I should be able to get all the instances of this eventSeries and change them if needed
 }

Here is a capture of an instance I get using the API tryout :

enter image description here


Solution

  • With CalendarApp, just specify the date range to look for the single event and loop through the result to then use .setTime() :

    var cal = CalendarApp.getCalendarsByName('<YOUR_CAL>')[0];
    var events = cal.getEvents(new Date("April 5, 2016 08:00:00 AM"), new Date("April 5, 2016 11:30:00 AM"));
    events.forEach( function(e) {
      //var e = events[0];
      //if ( e.getTitle() === 'Your Title' ) 
      e.setTime(new Date("April 5, 2016 11:00:00 AM"), new Date("April 5, 2016 01:00:00 PM"));
    });
    

    I can't tell if your "specific instance" means just a single event or every Tuesday event though, or whether you have a reason to even use the Advanced Calendar API for this case.