2

I'd like to calculate the age of a person whose birthday exists as event series within my calendar. To do this I need to know the first event within this series and that's the question: how to get the first event of a series from an actual event?

Thanks Ronny

RoBra81
  • 21
  • 3

2 Answers2

1

The other answer doesn't actually answer the initial request, the CalendarApp has no method to get the start date of a recurring event. You should use the advanced Calendar API (must be enabled manually, see below and follow instructions)

enter image description here

Then use the advanced API to get the information you want, (the auto complete feature works on these methods too so you can easily see what is available)

Test code below, note that event ID is different for the advanced Calendar API, you have to remove the part after '@'.

function createTestEvents() {
  var recurrence = CalendarApp.newRecurrence().addWeeklyRule().times(10);
  var testEvent = CalendarApp.getDefaultCalendar().createEventSeries('test event serie', new Date('2016/05/10'), new Date(new Date('2016/05/10').getTime()+12*3600000), recurrence);
  var id = testEvent.getId();
  Logger.log('Event Series ID: ' + id);
  viewTestEvent(id)
}

function viewTestEvent(id){
  var event= CalendarApp.getDefaultCalendar().getEventSeriesById(id);
  var calId = CalendarApp.getDefaultCalendar().getId();
  Logger.log('event title = '+event.getTitle());
  var AdvanncedId = id.substring(0,id.indexOf('@'));
  Logger.log('AdvanncedId = '+AdvanncedId);
  var testEvent = Calendar.Events.get(calId, AdvanncedId);
  Logger.log('testEvent start = '+ testEvent.start);
  return testEvent;
}

function test(){ // a few examples of what you can retrieve...
  var event = viewTestEvent('59buf7nq6nr6qo79bh14kmsr6g@google.com');
  Logger.log('\n\nstart = '+event.start);
  Logger.log('\n\ncreated on = '+event.created);
  Logger.log('\n\nend on = '+event.end);
  Logger.log('\n\nrecurrence = '+event.recurrence);
}

enter image description here

enter image description here

Serge insas
  • 45,904
  • 7
  • 105
  • 131
0

You need to use the startDate parameter to get the date of the first event in the series (only the day is used; the time is ignored).

var eventSeries = CalendarApp.getDefaultCalendar().createAllDayEventSeries('No Meetings',
     new Date('January 2, 2013 03:00:00 PM EST'),
     CalendarApp.newRecurrence().addWeeklyRule()
         .onlyOnWeekday(CalendarApp.Weekday.WEDNESDAY)
         .until(new Date('January 1, 2014')));
 Logger.log('Event Series ID: ' + eventSeries.getId());

You can also get the event series with the given ID using getEventSeriesById(iCalId).

If the ID given is for a single CalendarEvent, then a CalendarEventSeries will be returned with a single event in the series. Note that if the event series belongs to a calendar other than the default calendar, this method must be called from that Calendar; calling CalendarApp.getEventSeriesById(id) directly will only return an event series that exists in the default calendar.

Hope this helps!

abielita
  • 13,147
  • 2
  • 17
  • 59