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:
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.
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.
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/
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.