Google Apps Script: Setting color of an event using Calendar API

后端 未结 4 1671
感情败类
感情败类 2021-01-14 14:55

I would like to set specific colors for events.

I believe I have to use the Calendar API. I cannot figure out how to do this.

The code I am trying is:

4条回答
  •  滥情空心
    2021-01-14 15:24

    It is actually possible using the advanced Calendar Service.

    (This post was helpful)

    The code to create a new event goes like this : (inspired by Google's example)

    function createEvent() {
      var calendarId = 'primary';
      var event = {
        summary: 'Other test',
        location: 'right here',
        description:' chat... ;-)',
        start: {
          dateTime: new Date().toISOString()
        },
        end: {
          dateTime: new Date(new Date().getTime()+3600000).toISOString()
        },
        attendees: [
          {email: 'user@gmail.com'}
        ],
        // Red background. Use Calendar.Colors.get() for the full list.
        colorId: 11
      };
      event = Calendar.Events.insert(event, calendarId);
      Logger.log('Event ID: ' + event.getId());
    }
    

    and to modify an existing event (having its ID) goes like that :

    function ChangeEventColor(){
      var calendarId = 'primary';
      var eventId = 'omv°°°°°°°°°°8jbs'
      var event = Calendar.Events.get(calendarId, eventId)
      Logger.log('current color = '+event.colorId)
      event.colorId = 11
      Calendar.Events.patch(event,calendarId,eventId);
      Logger.log('new color = '+event.colorId)
    }
    

    Color codes are listed using (for example) the Google online API tryout here

    The advanced Google Calendar Service has to be enabled before you run this code using the ressources menu in the script editor, see illustration below.

提交回复
热议问题