Warm tip: This article is reproduced from serverfault.com, please click

Google calendar API > Edit Recurring Events > this and following option > persist exceptions

发布于 2020-12-07 16:27:07

I created a recurring series using Google-calendar-api from December 11 to December 20. I made a change (For eg: Description) of a single instance on December 18th using "this event" only option.

Then I'm editing the title on December 15 and choosing "This and following" options. This resets my exception on December 18th (where the description was changed) to match the December 15th description.

How to persist the exceptions of the series when splitting the parent series into two for 'this and following' as mentioned in the Google API docs.

Edit: To create Google recurring event:

$event = new Google_Service_Calendar_Event([
           'summary'  => $meeting->title,
           'location' => $meeting->location,
           'start' => [
                'date'     => $all_day_startdate,
                'dateTime' => $start_date_time,
                'timeZone' => $timezone
           ],
           'end' => [
               'date'     => $all_day_enddate,
               'dateTime' => $end_date_time,
               'timeZone' => $timezone
           ],
            'conferenceData' => $conference_link,
        ]);

         $event>setRecurrence(array(format_recurrence_rule_for_google($recurrence_pattern)));

        // set Google calendar event parameters.
        $calendar_id = 'primary';
        $opt_params  = [
            'sendNotifications'     => $send_invite,
            'conferenceDataVersion' => 1
        ];

        
        // create the Google calendar event
        $event = $service->events->insert($calendar_id, $event, $opt_params);

To update a single instance:

$event = new Google_Service_Calendar_Event([
            'summary'  => $meeting->title,
            'location' => $meeting->location,
            'start' => [
                   'date'     => $all_day_startdate,
                   'dateTime' => $start_date_time,
                   'timeZone' => $meet_creator->timezone
             ],
             'end' => [
                   'date'     => $all_day_enddate,
                   'dateTime' => $end_date_time,
                   'timeZone' => $meet_creator->timezone
             ],
             "recurringEventId" => $recurring_event_id,
             "originalStartTime" => [
                    "dateTime" => $exist_event->getOriginalStartTime()->getDateTime(),
                        "timeZone" => $meet_creator->timezone
              ],
              "iCalUID" => $exist_event->getiCalUID(),
            
             'conferenceData' => event_conference($meeting->conference)
            ]);

            $opt_params  = [
                    'conferenceDataVersion' => ($generate_conf || $cancel_conf) ? 1 : 0
                ];

            $calendar_id  = 'primary';
            $updatedEvent = $service->events->insert($calendar_id, $event, $opt_params);

This and following events code snippet

$current_event = $this->get_instance_with_event_id($parent_meeting);
$rec           = format_recurrence_rule_for_google($parent_meeting->meeting_options->recurrence);

$current_event->setRecurrence(array($rec));

// trim the parent recurring event into two
$service->events->update('primary', $parent_meeting->meeting_options->recurring_event_id, $current_event);

              
 $event = $this->create_initial_calendar_event() // which is the first snippet to create recurring event
Questioner
findX
Viewed
0
fullfine 2020-12-28 16:38:27

Short Answer

It is not possible

Explanation

The official documentation explains how to work with Recurring Events. It has a section that shows how to modify all following instances but following these steps you get two separated Recurring Events and the exception instances will disappear. There is only one way of modifying some parameters of an instance and apply them to this and following events and that is using the web version of the Calendar (it also respects the exceptions).

Workarounds

  1. Looping

    • Loop through all the instances and update only those you want. You can set a condition to apply the update only if a propriety (start,description) matches with your specification.

    • Disadvantage: one call per update.

  2. Update and Insert (check the official documentation here)

    • Events: update to trim the event and (re)define the instances that are not going to change. The body request is an Event resource that needs all the properties that have been used to create the original Recurring Event with the recurrence modified to cover the desired days.
    • Events: insert to insert the remaining instances with the modified properties. The body request has to be defined with all its properties as in the previous step.
    • Disadvantages: it creates two different Recurring Events and the Event resources had to be defined from zero. It also overlaps the exceptions.
  3. Get, Patch and Insert (can handle some exceptions)

    • Events: get to get the Recurring Event with all its information.
    • Events: patch to trim the event and keep the instances that have the exceptions. The body request is an Event resource that can be the defined in two ways:
      • From zero: it only needs start, end and recurrence. Using patch maintains all the other parameters.
      • From the original event obtained previously with get. In your case, you need to modify start.dateTime and end.dateTime to December 15 (and corresponding hours) and the new parameter that you want to change, for example the description.
    • Events: insert to insert the remaining instances without exceptions. The Event resource used in this step is a clone from the original Recurring Event. There are some properties that need to change: iCalUID = "", id = "", and for your case the recurrence in order to stop on December 15.
    • Disadvantages: it creates two different Recurring Events and takes three different API calls. The exceptions are only maintained in one Recurring Event.
  4. Cancel instance, insert new event

    • Events: instances to get the instances of a Recurring Event.
    • Cancel an instance of a Recurring Event changing its status: Cancelled exceptions of an uncancelled recurring Event indicate that this instance should no longer be presented to the user.
    • Events: insert to insert a new Event resource on the same date as the cancelled instance so that future changes to the Recurring Event do not change this exception. The properties can be obtained from the original instance.
    • Disadvantage: The exception event does not belong to the Recurring Event.

References