Search code examples
google-calendar-apigoogle-calendar-recurring-events

Google calendar api: recurring events exceptions + master event update


I have an app with the following scenario:

  • user creates a recurring event in google calendar in my app (via google calendar API v3)
  • they change time of one of the instances (which creates exception instance - 'exception' in a good way)
  • now they want to extend that recurring event and they change either count or end date of the master event

The problem is that all the exceptions disappears after that last step (calendar UI seem to handle that, but if done via the API the exceptions are gone). I've noticed however that outlook manages that case just fine via their API.

Is there any simple way of preserving the exceptions in this scenario in google calendar via API?

I've also noticed that if I try to further update one of the original exceptions after the master is updated, that gives error (an 'exception' in a bad way) and points that the instance event is marked as deleted hence can't be updated ('get' still returns it). In this case it created a new instance event with a new id in place of the original one.

UPDATE

A pseudo code to update the master event would look something like this:

let originalMastEvent = {
        id:"google_generated_master_event_id",
        ...
        recurrence: [ 'RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=WE,SA;COUNT=6' ],
        ...
}

//change the count from 6 to 10, the rest stays the same:

let newMastEvent = {
    id:"google_generated_master_event_id",
    summary: 'Summary',
    start: { dateTime: '2020-06-24T01:00:00+02:00', timeZone: 'UTC' },
    end: { dateTime: '2020-06-24T02:00:00+02:00', timeZone: 'UTC' },
    recurrence: [ 'RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=WE,SA;COUNT=10' ],
    sendUpdates: 'all',
    attendees:[ { email: '[email protected]'} ]
}

const {google} = require('googleapis/build/src/index'); //I'm on node.js

let auth = {...} //setup google api oauth obj
let calendar = await google.calendar({version: 'v3', auth});

let res = await calendar.events.update({
    calendarId: 'primary',
    eventId: newMastEvent.id,
    resource: newMastEvent,
    sendUpdates: newMastEvent.sendUpdates
})

Solution

  • Your issue is related to how are you adding the exceptions and also in the way you are updating the main event.

    According to this page here you need to use a POST request when inserting an exception.

    Therefore, you will have to use the Events:insert request:

    POST https://www.googleapis.com/calendar/v3/calendars/calendarId/events
    

    With the following body:

    {
      "start": {
        "dateTime": "MODIFIED_START_DATE",
        "timeZone": "YOUR_TIMEZONE"
      },
      "end": {
        "dateTime": "MODIFIED_END_DATE",
        "timeZone": "YOUR_TIMEZONE"
      },
      "recurringEventId": "ID_OF_THE_RECURRENT_EVENT",
      "originalStartTime": {
        "dateTime": "ORIGINAL_DATE_TIME",
        "timeZone": "YOUR_TIMEZONE"
      },
      "iCalUID": "ID",
    }
    

    Afterwards, if you want to update the main event, for example extending the number of occurrences, you can simply to the request below:

    PUT https://www.googleapis.com/calendar/v3/calendars/calendarId/events/eventId
    

    With the following body:

    {
      "start": {
        "dateTime": "START_DATE_OF_THE_ORIGINAL_EVENT",
        "timeZone": "YOUR_TIMEZONE"
      },
      "end": {
        "dateTime": "END_DATE_OF_THE_ORIGINAL_EVENT",
        "timeZone": "YOUR_TIMEZONE"
      },
      "recurrence": [
        "RRULE:FREQ=WEEKLY;WKST=SU;COUNT=3;BYDAY=FR"
      ]
    }
    

    After the above requests, you should be able to still have the exception in place while also making the extension of the event.

    Reference