Let's say you have recurring events like this:
- Event 1 (June 1st)
- Event 2 (June 2nd)
- Event 3 (June 3rd)
(It's a single event that repeats every day until there are 3 of itself)
There are two ways to edit recurring events: as individual events or as a single event.
As a single event
Google has one recommendation for how to do this: basically, when you are doing your Events List request you should set "singleEvents" to false (which it is by default). Note that here, "singleEvents" refers to "instances" (whereas I'm using it to refer to the original recurring event). Important note: if "singleEvents" is false, "orderBy" cannot be set to "startTime". My guess for why is that if a recurring event is shown as one event, it is "fuzzy" how it should be ordered with other events if all of its instances have different start times.
I have a different recommendation. Since I am editing both regular events and recurring events at the same time, I give the user the option how they want to edit the recurring events (it's a setting in my program): again, as individual events or as a single event.
So I simply get a list of events (with "singleEvents" set to "true" and "orderBy" set to "startTime". I think this makes the most sense to the user as this is what is shown in their calendar) and loop through that list of events. When I come across a recurring event (checked by !string.IsNullOrEmpty(event.recurringEventId)) and I want to edit recurring events as one single event, I call a Events Get request on that recurringEventId (this gives me the original recurring event). Then I use that resulting event to replace the current event in the loop. I also store that recurringEventId for later in a list so that when I loop through the other instances of that event that the user selected, I can simply skip them.
As individual events
Obviously this method breaks up the events so that they are separated from the original series (but sometimes that is what is desired).
Here we're going to skip the "Events Get" request and simply change some properties of the event. We're going to set the event's recurringEventId to "" (empty string) and set the event's originalStartTime to null. These changes are what break the recurrence, but they are necessary in order to not hit exceptions in some of the other API requests (especially when we're calling Events Update on the event later).
With these two methods we can simply loop through a list of events (including all the instances of a recurring event) and still editing recurring events as the user pleases!
Hope this helps someone out!
No comments:
Post a Comment