Google Apps Script: Calendar Service: Find first event in CalendarEventSeries

两盒软妹~` 提交于 2020-02-24 12:09:05

问题


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


回答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)

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);
}




回答2:


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!



来源:https://stackoverflow.com/questions/37476850/google-apps-script-calendar-service-find-first-event-in-calendareventseries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!