admin管理员组

文章数量:1389928

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
  • Please provide a minimal reproducible example. Also can you share additional code so that we can understand more. – Lime Husky Commented Mar 14 at 14:18
  • 1 Check for this answer. – A. Cedano Commented Mar 14 at 14:49
  • 2 @Cooper Oh dang. If you put that as answer I'll mark it as accepted. – IMTheNachoMan Commented Mar 14 at 15:09
  • 2 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 use getEventById what more information do you need? – Tedinoz Commented Mar 15 at 7:02
  • 1 AFAIK 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
 |  Show 8 more comments

1 Answer 1

Reset to default 1

You 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.

  1. Function findRecurringEvents()
    • Insert your own calendarId
    • run the script
  • 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.
  1. Apply the Calendar API as described in Google Apps Script: Calendar Service: Find first event in CalendarEventSeries

  2. 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"

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

本文标签: