0
votes

I have events those repeat for every Monday starting from Feb 8, 2021. I want to modify the upcoming 28th Feb 2021 so that all future events will be repated every Wednesday. Note that the event must be updated will ALL FOLLOWING EVENTS (preserving the eventID)

var resource = Calendar.Events.get(calendar.getId(), eventId);
            if (resource) {
                resource.start.date = Utilities.formatDate(new Date("28-02-2021"), Session.getScriptTimeZone(), "yyyy-MM-dd");
                resource.end.date = Utilities.formatDate(new Date("28-02-2021"), Session.getScriptTimeZone(), "yyyy-MM-dd");
                var result = Calendar.Events.patch(resource, calendar.getId(), eventId)
            }

This code will update all the recurring events including the past ones. But I need only the following events to be updated.

1

1 Answers

1
votes

Based on the official document for Recurring Events, preserving the eventId is not possible since you need to create a new event based on your preferred modification and update the old recurring event and change its UNTIL parameter to end the series.

Modifying all following instances

In order to change all the instances of a recurring event on or after a given (target) instance, you must make two separate API requests. These requests split the original recurring event into two: the original one which retains the instances without the change and the new recurring event having instances where the change is applied:

  1. Call events.update() to trim the original recurring event of the instances to be updated. Do this by setting the UNTIL component of the RRULE to point before the start time of the first target instance. Alternatively, you can set the COUNT component instead of UNTIL.

  2. Call events.insert() to create a new recurring event with all the same data as the original, except for the change you are attempting to make. The new recurring event must have the start time of the target instance.


(UPDATE)

Sample Code:

function updateRecurr() {
  var calendarId = 'c_9cdqeqqluk7vsessartfxxxxx';
  var eventId = '17a97q639vmtkoghcxxxxx';

  //Update eventseries, change "until" parameter in rrule to Feb. 27, 2020
  var event = {
    recurrence:[
      "RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20210227;BYDAY=MO"
    ]
  }
  updatedEvent = Calendar.Events.patch(event,calendarId,eventId);
  Logger.log(updatedEvent);

  //Create new event having the same important info with different start time and rrule
  var newEvent = {
    summary: updatedEvent.summary,
    description: updatedEvent.description,
    start:{
      date: Utilities.formatDate(new Date("March 3, 2021"), Session.getScriptTimeZone(), "yyyy-MM-dd")
    },
    end:{
      date: Utilities.formatDate(new Date("March 3, 2021"), Session.getScriptTimeZone(), "yyyy-MM-dd")
    },
    recurrence:[
      "RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20210331;BYDAY=WE"
    ]
  }

  var newEventSeries = Calendar.Events.insert(newEvent,calendarId);
  Logger.log(newEventSeries);
  
}

OUTPUT:

Before:

enter image description here

enter image description here

After:

enter image description here

enter image description here


Note:

In the newly created event series, you will notice that I set the start date to "March 3, 2021", the reason behind this is because If I set that to "February 28, 2021" an event will also be created on that particular date. But in real life, when I created an event using Google Calendar and set the start date to "February 28, 2021" with event series of weekly occurrence every Wednesday. The actual start date set in the event was "March 3, 2021".

Just copy all other important event details if there is any, in your previous event series. In the example provided I just configured the event title and the description.