How do you set the color of a Google calendar event using the API

Viewed 3434

This question has been asked a while ago but has not been answered. I think things have moved on since.

I can create an event using the API but I am unable to set the colour to anything other than the default. This is :colorId is not working? Any ideas?

    event = Google::Apis::CalendarV3::Event.new({
                                                    :summary => @measure.value,

                                                    :description => 'created by ' + current_user.username,
                                                    :start => {
                                                        :date_time => event_starttime
                                                    },
                                                    :end => {
                                                        :date_time => event_endtime
                                                    },
                                                    :colorId => "6"
                                                })
3 Answers

I just came across this because I was having the same problem. I'm using the ruby api client and I dug into its code to find this:

# The color of the event. This is an ID referring to an entry in the event
# section of the colors definition (see the  colors endpoint). Optional.
# Corresponds to the JSON property `colorId`
# @return [String]
attr_accessor :color_id

Updating my event insert call to use color_id: 'a number from 1 to 11' worked for me.

As mentioned in @CChandler81 answer, an event's color is set using a colorId; an integer value from 1 to 11.

This image shows 11 events using the 11 colors.

The events were created using the following Python code:

colorId = 1
for day in range(4):
    event_date = datetime.date.today() + datetime.timedelta(days=day)
    event_date_str = event_date.strftime('%Y-%m-%d')
    for start_hour in range(0,12,4):
        start_time = datetime.time(hour=start_hour)
        end_time = datetime.time(hour=start_hour + 4)
        # Create calendar event specifying colorId
        event = {
            'summary': f'colorId {colorId}',
            'colorId': colorId,
            'start': {
                'dateTime': f"{event_date_str}{start_time.strftime('T%H:%M:%S')}",
                'timeZone': 'Europe/London',
            },
            'end': {
                'dateTime': f"{event_date_str}{end_time.strftime('T%H:%M:%S')}",
                'timeZone': 'Europe/London',
            }
        }
        service.events().insert(calendarId='primary', body=event).execute()
        # Prepare for next event or finish if all colors used
        colorId += 1
        if colorId > 11:
            break

The above was added into the quickstart example from the Google Calendar API Python Quick Start Guide. A useful example of getting color information, in various languages, is available at Colors: get.

Updating the Python example:

service = build('calendar', 'v3', credentials=creds)
colors = service.colors().get().execute()
for key, value in colors.items():
    print(f'color key \'{key}\': {value}')

for item in ['calendar', 'event']:
    print(f'Colors {item}:')
    for colorId, color in colors[item].items():
        back = color['background']
        fore = color['foreground']
        print(f'colorId \'{colorId}\': Background={back}, foreground={fore}')

Slotting above into the same quickstart example outputs:

color key 'kind': calendar#colors
color key 'updated': 2012-02-14T00:00:00.000Z
color key 'calendar': {'1': {'background': '#ac725e', 'foreground': '#1d1d1d'}, '2': {'background': '#d06b64', 'foreground': '#1d1d1d'}, '3': {'background': '#f83a22', 'foreground': '#1d1d1d'}, '4': {'background': '#fa573c', 'foreground': '#1d1d1d'}, '5': {'background': '#ff7537', 'foreground': '#1d1d1d'}, '6': {'background': '#ffad46', 'foreground': '#1d1d1d'}, '7': {'background': '#42d692', 'foreground': '#1d1d1d'}, '8': {'background': '#16a765', 'foreground': '#1d1d1d'}, '9': {'background': '#7bd148', 'foreground': '#1d1d1d'}, '10': {'background': '#b3dc6c', 'foreground': '#1d1d1d'}, '11': {'background': '#fbe983', 'foreground': '#1d1d1d'}, '12': {'background': '#fad165', 'foreground': '#1d1d1d'}, '13': {'background': '#92e1c0', 'foreground': '#1d1d1d'}, '14': {'background': '#9fe1e7', 'foreground': '#1d1d1d'}, '15': {'background': '#9fc6e7', 'foreground': '#1d1d1d'}, '16': {'background': '#4986e7', 'foreground': '#1d1d1d'}, '17': {'background': '#9a9cff', 'foreground': '#1d1d1d'}, '18': {'background': '#b99aff', 'foreground': '#1d1d1d'}, '19': {'background': '#c2c2c2', 'foreground': '#1d1d1d'}, '20': {'background': '#cabdbf', 'foreground': '#1d1d1d'}, '21': {'background': '#cca6ac', 'foreground': '#1d1d1d'}, '22': {'background': '#f691b2', 'foreground': '#1d1d1d'}, '23': {'background': '#cd74e6', 'foreground': '#1d1d1d'}, '24': {'background': '#a47ae2', 'foreground': '#1d1d1d'}}
color key 'event': {'1': {'background': '#a4bdfc', 'foreground': '#1d1d1d'}, '2': {'background': '#7ae7bf', 'foreground': '#1d1d1d'}, '3': {'background': '#dbadff', 'foreground': '#1d1d1d'}, '4': {'background': '#ff887c', 'foreground': '#1d1d1d'}, '5': {'background': '#fbd75b', 'foreground': '#1d1d1d'}, '6': {'background': '#ffb878', 'foreground': '#1d1d1d'}, '7': {'background': '#46d6db', 'foreground': '#1d1d1d'}, '8': {'background': '#e1e1e1', 'foreground': '#1d1d1d'}, '9': {'background': '#5484ed', 'foreground': '#1d1d1d'}, '10': {'background': '#51b749', 'foreground': '#1d1d1d'}, '11': {'background': '#dc2127', 'foreground': '#1d1d1d'}}
Colors calendar:
colorId '1': Background=#ac725e, foreground=#1d1d1d
colorId '2': Background=#d06b64, foreground=#1d1d1d
colorId '3': Background=#f83a22, foreground=#1d1d1d
colorId '4': Background=#fa573c, foreground=#1d1d1d
colorId '5': Background=#ff7537, foreground=#1d1d1d
colorId '6': Background=#ffad46, foreground=#1d1d1d
colorId '7': Background=#42d692, foreground=#1d1d1d
colorId '8': Background=#16a765, foreground=#1d1d1d
colorId '9': Background=#7bd148, foreground=#1d1d1d
colorId '10': Background=#b3dc6c, foreground=#1d1d1d
colorId '11': Background=#fbe983, foreground=#1d1d1d
colorId '12': Background=#fad165, foreground=#1d1d1d
colorId '13': Background=#92e1c0, foreground=#1d1d1d
colorId '14': Background=#9fe1e7, foreground=#1d1d1d
colorId '15': Background=#9fc6e7, foreground=#1d1d1d
colorId '16': Background=#4986e7, foreground=#1d1d1d
colorId '17': Background=#9a9cff, foreground=#1d1d1d
colorId '18': Background=#b99aff, foreground=#1d1d1d
colorId '19': Background=#c2c2c2, foreground=#1d1d1d
colorId '20': Background=#cabdbf, foreground=#1d1d1d
colorId '21': Background=#cca6ac, foreground=#1d1d1d
colorId '22': Background=#f691b2, foreground=#1d1d1d
colorId '23': Background=#cd74e6, foreground=#1d1d1d
colorId '24': Background=#a47ae2, foreground=#1d1d1d
Colors event:
colorId '1': Background=#a4bdfc, foreground=#1d1d1d
colorId '2': Background=#7ae7bf, foreground=#1d1d1d
colorId '3': Background=#dbadff, foreground=#1d1d1d
colorId '4': Background=#ff887c, foreground=#1d1d1d
colorId '5': Background=#fbd75b, foreground=#1d1d1d
colorId '6': Background=#ffb878, foreground=#1d1d1d
colorId '7': Background=#46d6db, foreground=#1d1d1d
colorId '8': Background=#e1e1e1, foreground=#1d1d1d
colorId '9': Background=#5484ed, foreground=#1d1d1d
colorId '10': Background=#51b749, foreground=#1d1d1d
colorId '11': Background=#dc2127, foreground=#1d1d1d
Related