如何使用API​​设置Google日历事件的颜色

时间:2017-06-03 10:00:25

标签: api google-calendar-api

此问题已在前一段时间被问到,但尚未得到解答。我认为事情已经发生了变化。

我可以使用API​​创建一个事件,但我无法将颜色设置为默认值以外的任何颜色。这是:colorId不工作?有什么想法吗?

    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 个答案:

答案 0 :(得分:2)

我刚刚遇到这个因为我遇到了同样的问题。我正在使用ruby api客户端,我挖到code找到了这个:

# 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

更新我的事件插入调用以使用color_id: 'a number from 1 to 11'为我工作。

答案 1 :(得分:1)

@CChandler81 答案中所述,事件的颜色是使用 colorId 设置的;一个从 1 到 11 的整数值。

This image 使用 11 种颜色显示 11 个事件。

事件是使用以下 Python 代码创建的:

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

以上内容已添加到 Google 日历 API Python Quick Start Guide 的快速入门示例中。 Colors: get 提供了获取各种语言颜色信息的有用示例。

更新 Python 示例:

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}')

插入上面相同的快速入门示例输出:

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

答案 2 :(得分:0)

使用Events.insert创建日历活动时,有一个名为colorId的可选属性

  

colorId string事件的颜色。这是一个引用的ID   在颜色定义的事件部分中输入(请参阅颜色   终点)。可选的。可写

检查Colors property resource格式以设置背景和前景色。