How to create two days AllDay Event with Google Ap

2019-04-10 11:37发布

问题:

I want to use Google Apps Script createAllDayEvent(title, date) create an All day Google Calendar, but there is only one date parameter, so I only could create one day AllDay Google Calendar.

Now I need to create two days AllDay Google Calendar (e.g.:from Jun 28, 2013 to Jun 29, 2013), how could I do?

Thanks for help!

回答1:

You need to use createAllDayEventSeries(), which accepts a Recurrence parameter.

var recurrence = CalendarApp.newRecurrence().addDailyRule().times(2);
var eventSeries = CalendarApp.getDefaultCalendar().createAllDayEventSeries('My All Day Event',
    new Date('June 28, 2013'),
    recurrence,
    {guests: 'everyone@example.com'});
Logger.log('Event Series ID: ' + eventSeries.getId());


回答2:

You can't add create a multi-day 'all-day' event that spans multiple days via CalendarApp Class. You have to do it using the Calendar API via the Advanced Google services.

1) Enable Calendar API for the script On the google scripts page, go to the menu item 'Resources' → 'Advanced Google services'. Turn on the Calendar API (currently v3).

2) Enable the service via the Google API console From the Advanced Google Services page, follow the link to the API Console. Click '+ Enable API'. Select the 'Calendar API', then enable it.

3) Use the below code snippet (which creates a two-day span, all-day event) as an example

var calId = '##########################@group.calendar.google.com';
var event = {
             "summary":"Summary",
             "location":"Location",
             "description":"Description",
             "start":{
                      "date":"2017-06-03"
                     },
             "end":{
                    "date":"2017-06-05"
                   }
            };
Calendar.Events.insert(event,calId);

// You can check how the events are stored below
var events = Calendar.Events.list(calId, {timeMin: (new Date(2017,5,2)).toISOString(),
                                          timeMax: (new Date(2017,5,10)).toISOString(),
                                          maxResults: 2500});

You'll note that the above code will generate an all-day event from 3rd June to 4th June (the end date is essentially 2017-06-05T00:00:00).

There are other event properties you can add, somewhat documented on the Calendar API/Events/insert page. Using Calendar.Events.list to fetch the list of events will give you an idea.

If you look at the Calendar API Events, you'll note that there is a lot of overlap with the iCal spec.