Google Apps Script: Setting color of an event using Calendar API
Solution 1
It is actually possible using the advanced Calendar Service.
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: '[email protected]'}
],
// 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.
Solution 2
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.
jockster
Updated on June 23, 2022Comments
-
jockster almost 2 years
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