5

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:

var event = CalendarApp.getOwnedCalendarById(calendarID).createEvent(class, startTime, endTime, {description:lessonPlanDataComplete[t], colorId: 11});

The colorId 11 should set as red, but all events coming out as default color.

Any helps/hints gratefully received,

Many thanks, Simon

Kos
  • 4,890
  • 9
  • 38
  • 42
jockster
  • 111
  • 1
  • 2
  • 4
  • Have you tried using `setColor` instead? It takes a hexadecimal color string. In none of the `createEvent` methods do I see color as an optional parameter. – Andy Jul 21 '15 at 23:41
  • Hi, thanks for your reply. Ihave tried using setColor but can't get that to work either. Does anyone have some example code I could copy. Many thanks, Simon – jockster Jul 22 '15 at 07:33

4 Answers4

7

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.

enter image description here

Community
  • 1
  • 1
Serge insas
  • 45,904
  • 7
  • 105
  • 131
5

This is always possible. Try it out instead.

in your function:

var newEvent = calendar.createEvent(event_title, event_start_time, event_end_time, 
  {
    location: event_location, 
    guests: newGuests, 
    sendInvites: 'true', 
    description: event_description
  }
);

newEvent.setColor('4');

Refer to: https://developers.google.com/apps-script/reference/calendar/event-color

The string to set is the numerical string representation of event color.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Francis Woon
  • 51
  • 1
  • 1
0

This is currently not possible. You can use setColor on a Calendar but not on an event. You can send feedback about the API from the developer pages about this and hope that it gets added.

Meanwhile, I suggest that you create a calendar with the desired colour and place all events in there as this will make them have that colour.

Penguin_
  • 21
  • 1
  • 6
0

In my case I'm not creating, but updating existing calendar events, but I think stood be similar.

event.colorId = filterColorId;
Calendar.Events.patch(event, 'primary', event.id)

The whole solution can be found here http://isbyr.com/colour-code-google-calendar-events-automatically-using-google-apps-script/

Ilya
  • 591
  • 5
  • 6