Home > database >  Modify a Google Calendar recurrence in Apps Script
Modify a Google Calendar recurrence in Apps Script

Time:02-02

I'm trying to create a script that, given a date, will change the end date of all my recurrent events this week to that date. Unfortunately, I can't find a way to update a recurrence info.

First, I iterate all events in the week. Then, thanks to the API, I can get the recurrence info of every event:

  var eventId = event.getId().split("@")[0];
  var eventFromAPI = Calendar.Events.get(ID_CALENDARIO_MENUS, eventId);
  var cadenaRecurrencia = eventFromAPI["recurrence"];

witch then I use to create a new recurrence to overwrite the existing one in the event.

  var recurrencia = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekdays(arrayWeekdays).until(fecha);

But when I try to set the new recurrence, it asks me for a start date that should be the original, and I can't seem to retrieve that data:

var serie = calendario_menus.getEventSeriesById(eventoId);
      serie.setRecurrence(recurrencia, date);

Is there a way to access an event's recurrence, modify it so it changes for all the events that use the recurrence and update it?

Thanks for your help.

CodePudding user response:

The issue you're encountering is because the setRecurrence() method requires both the new recurrence rule and the start time of the original event series, which you need to provide. You can retrieve the start time of the original event series by accessing the start property of the event object you obtained from the API. You can then use that start time when calling setRecurrence(). Here's an example:

var eventId = event.getId().split("@")[0];
var eventFromAPI = Calendar.Events.get(ID_CALENDARIO_MENUS, eventId);
var originalStartTime = new Date(eventFromAPI["start"]["dateTime"]);
var recurrence = CalendarApp.newRecurrence().addWeeklyRule().onlyOnWeekdays(arrayWeekdays).until(fecha);
var series = calendario_menus.getEventSeriesById(eventId);
series.setRecurrence(recurrence, originalStartTime);

CodePudding user response:

Yes, you are correct. The start date in this case is the start date of the event and not the recurrence. Setting the recurrence using the start date of the event will only affect future events in the series, and not the previous events. If you want to update the end date for all events in the series, you will need to retrieve all events in the series, and then update the end date for each event individually.



// Get the recurring event series

var serie = calendario_menus.getEventSeriesById(eventoId);

// Get all events in the series

var events = serie.getEvents();

// Get the new end date

var newEndDate = fecha;

// Iterate through all events in the series

 for (var i = 0; i < events.length; i  ) {
      var event = events[i];
      event.setEndTime(newEndDate);
    }
  • Related