3

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • you can try using Calendar API in apps script and get an event series as a single event and then update the details. Or use a patch for updating the event details. This [SO question](http://stackoverflow.com/questions/30724417/change-exisiting-calendar-event-series-date), will show that you can use Events.get and Events.update in the Calendar API. To add Calendar APi click Resources > Advance Google Services > Enable Google Calendar – Mr.Rebot Mar 21 '16 at 22:28
  • 1
    Thanks for your comment, everything I tried is changing all the events, not a single occurrence... how can I get one of the occurence as a single event ? I've seen here (https://code.google.com/p/google-apps-script-issues/issues/detail?id=5051) that events have a specific ID containing a compact form of a RFC339 date string but didn't succeed to use it to get the event. Any clue ? – Serge insas Mar 21 '16 at 23:00

1 Answers1

2

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.

Bryan P
  • 5,031
  • 3
  • 30
  • 44
  • 1
    Thank you for your answer but unfortunately this solution (that I already tried, I should have mention that) is breaking the recurrence. The modified event is not part of the event series anymore... The side effect is that if you need to change any detail in these events (location, summary or attendees) you have to do it for every occurrence separately (at least the one you have changed) which is very uncomfortable for people in charge of the events organization – Serge insas Mar 28 '16 at 09:53
  • Those details need to change for all events or just the one where the time changed? I changed the time on one event and then changed the details on all with `cal.getEventSeriesById().setDescription()` and they all updated including the one with a different time. I did not see the un-binding behavior from the event series. – Bryan P Mar 28 '16 at 13:12
  • 1
    Wow, you're actually right... I guess I missed something when I tried... shame on me ;-) 500 rep points for you then ! thx – Serge insas Mar 28 '16 at 15:04