I am using getEvents
to find events. It returns all events expanded. So if there is a recurring event, it returns all instances. I want just the original/main/root instance.
I know I can use getEventSeries
but then I have to check every event returned by getEvents
. I'm hoping there is a way to get getEvents
to return just the original/root/main instance of recurring events.
CalendarApp.getDefaultCalendar().getEvents(
new Date(2025, 2, 24),
new Date(2025, 2, 25)
).forEach(...)
I am using getEvents
to find events. It returns all events expanded. So if there is a recurring event, it returns all instances. I want just the original/main/root instance.
I know I can use getEventSeries
but then I have to check every event returned by getEvents
. I'm hoping there is a way to get getEvents
to return just the original/root/main instance of recurring events.
CalendarApp.getDefaultCalendar().getEvents(
new Date(2025, 2, 24),
new Date(2025, 2, 25)
).forEach(...)
Share
asked Mar 14 at 13:50
IMTheNachoManIMTheNachoMan
5,8935 gold badges59 silver badges115 bronze badges
13
|
Show 8 more comments
1 Answer
Reset to default 1You want to identify recurring events in a given date range, and then access the eventId for the "initial event".
This can be done in a staged process.
Function findRecurringEvents()
- Insert your own
calendarId
- run the script
- Insert your own
- Note that the actual starting date of the recurrent event is 4 March and end date is 7 March. But the function searches for "recurrent events" between 5 and 7 March to demonstrate that it is not necessary to include the "initial event" in
getEvents
and that the EventId will be returned anyway.
Apply the Calendar API as described in Google Apps Script: Calendar Service: Find first event in CalendarEventSeries
Function test()
- insert your own
eventId
- run the script
- Note:
- the event ID is different for the advanced Calendar API,
- you have to remove the part after '@' (this is handled in the script)
- Example (based on sample data)
- eventId for Apps script = "[email protected]"
- eventId for calendar API = "5l1tk6qfp29tpg8lvfbfchu00"
- insert your own
function findRecurringEvents() {
var calendarId = "<<Insert calendar ID>>"
var cal=CalendarApp.getCalendarById(calendarId);
var startTime=new Date(2025,2,5);
var endTime=new Date(2025,2,7);
var events=cal.getEvents(startTime, endTime);
// Logger.log(events) // DEBUG
// Logger.log("DEBUG: Number of events = "+events.length)
// credit Tanaike
// Google Apps Script Calendar Service: Get only the first events of all recurring (all day) events
// https://stackoverflow/a/60627083/1330560
// get details of recurring events
var firstEvents = events.reduce(function(ar, e) {
var id = e.getId();
// Logger.log("DEBUG: e="+e+", id="+id+", ar="+ar+", recurrent?="+e.isRecurringEvent())
const eventCreated = e.getDateCreated();
const startTime = e.getStartTime();
const endTime = e.getEndTime();
//Logger.log("DEBUG: start time="+startTime+", end time="+endTime+", date created="+eventCreated)
if (e.isRecurringEvent() ) {
ar.push({eventTitle: e.getTitle(), eventId: id, eventStartDate: startTime, eventEndDate: endTime});
}
return ar;
}, [])
Logger.log(firstEvents) // DEBUG
}
// so37476850
// Credit: Serge insas
function test(){ // a few examples of what you can retrieve...
var eventId = "<<Insert eventId>>"
var event = viewTestEvent(eventId)
//Logger.log('\nStart = '+event.start);
//Logger.log('\nStart = '+event.start+'\nCreated on = '+event.created);
//Logger.log('\nStart = '+event.start+'\nCreated on = '+event.created'\nEnd on = '+event.end);
Logger.log('\nStart = '+event.start+'\nCreated on = '+event.created+'\nEnd on = '+event.end+'\nRecurrence = '+event.recurrence);
}
function viewTestEvent(id){
var calId = "<<Insert calendar Id>>"
var cal=CalendarApp.getCalendarById(calId)
var event= cal.getEventSeriesById(id)
//var calId = CalendarApp.getDefaultCalendar().getId();
//Logger.log('event title = '+event.getTitle());
var advancedId = id.substring(0,id.indexOf('@'));
//Logger.log("Event title = "+event.getTitle()+"\nAdvancedId = "+advancedId)
var testEvent = Calendar.Events.get(calId, advancedId);
Logger.log("Event title = "+event.getTitle()+"\nAdvancedId = "+advancedId+"\nEvent start = "+ testEvent.start+"\nEvent end = "+ testEvent.end);
return testEvent;
}
SAMPLE - Output: findRecurringEvents()
[
{
eventTitle=A recurring event,
eventStartDate=Wed Mar 05 09:00:00 GMT+11:00 2025,
eventEndDate=Wed Mar 05 11:30:00 GMT+11:00 2025,
[email protected],
},
{
eventTitle=A recurring event,
eventStartDate=Thu Mar 06 09:00:00 GMT+11:00 2025,
eventEndDate=Thu Mar 06 11:30:00 GMT+11:00 2025,
[email protected],
}
]
SAMPLE - Output: test()
Event title = A recurring event
AdvancedId = 5l1tk6qfp29tpg8lvfbfchu00a
Event start = {"dateTime":"2025-03-04T09:00:00+11:00","timeZone":"Australia/Sydney"}
Event end = {"dateTime":"2025-03-04T11:30:00+11:00","timeZone":"Australia/Sydney"}
Start = {"dateTime":"2025-03-04T09:00:00+11:00","timeZone":"Australia/Sydney"}
Created on = 2025-03-17T07:56:30.000Z
End on = {"dateTime":"2025-03-04T11:30:00+11:00","timeZone":"Australia/Sydney"}
Recurrence = RRULE:FREQ=DAILY;UNTIL=20250307T125959Z
SAMPLE - Event Editor
SAMPLE - Calendar View
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744652903a4586007.html
to return just the original/root/main instance of recurring events.
And do what? What I'm getting at is... you are saying you want the "original/root/main instance". I tried the @Tanaike code and the ID of any recurring event (regardless of the timing/occurrence) is always the same. That is to say, there is only one ID for a recurring event. So if you usegetEventById
what more information do you need? – Tedinoz Commented Mar 15 at 7:02AFAIK there is no way to identify the recurrence of an event once it's created.
@Cooper (and IMTheNachoMan). FYI, I have found precedents for getting details of a "recurring" event after it has been created. Google Apps Script: Calendar Service: Find first event in CalendarEventSeries (May 2016-and included in my answer); Get recurrence of CalendarEvent in G-Suite Addon (Feb 2020). Important: "event ID is different for the advanced Calendar API, you have to remove the part after '@'." – Tedinoz Commented Mar 18 at 1:10