【问题标题】:Google Apps Script: Setting color of an event using Calendar APIGoogle Apps 脚本:使用 Calendar API 设置事件的颜色
【发布时间】:2015-07-21 19:44:31
【问题描述】:

我想为事件设置特定的颜色。

我相信我必须使用日历 API。我不知道该怎么做。

我正在尝试的代码是:

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

colorId 11 应设置为 红色,但所有事件均以默认颜色显示。

感激地收到任何帮助/提示,​​

非常感谢,西蒙

【问题讨论】:

  • 您是否尝试过改用setColor?它需要一个十六进制颜色字符串。在任何createEvent 方法中,我都没有将颜色视为可选参数。
  • 您好,感谢您的回复。我试过使用 setColor 但也无法让它工作。有没有人有一些我可以复制的示例代码。非常感谢,西蒙

标签: google-apps-script google-calendar-api


【解决方案1】:

实际上可以使用高级日历服务。

(This post was helpful)

创建新事件的代码如下所示:(灵感来自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());
}

并修改现有事件(具有其 ID)是这样的:

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

使用(例如)Google online API tryout here 列出颜色代码

在使用脚本编辑器中的资源菜单运行此代码之前,必须启用高级 Google 日历服务,请参见下图。

【讨论】:

    【解决方案2】:

    这总是可能的。试试吧。

    在你的函数中:

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

    参考:https://developers.google.com/apps-script/reference/calendar/event-color

    要设置的字符串是事件颜色的数字字符串表示。

    【讨论】:

      【解决方案3】:

      目前这是不可能的。您可以在日历上使用 setColor,但不能在事件上使用。您可以从开发人员页面发送有关 API 的反馈,并希望它被添加。

      同时,我建议您创建一个具有所需颜色的日历并将所有事件放置在其中,因为这将使它们具有该颜色。

      【讨论】:

        【解决方案4】:

        在我的情况下,我不是在创建,而是更新现有的日历事件,但我认为是相似的。

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

        完整的解决方案可以在这里找到 http://isbyr.com/colour-code-google-calendar-events-automatically-using-google-apps-script/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多