【问题标题】:Changing calendar event color更改日历事件颜色
【发布时间】:2016-03-16 19:13:09
【问题描述】:

我希望能够以编程方式更改日历事件的颜色。我正在阅读这个文档,但不明白这里的“密钥”指的是什么:https://developers.google.com/google-apps/calendar/v3/reference/colors。是事件ID吗?

我还想知道是否可以通过电子表格更改日历活动颜色。我有一个脚本可以将电子表格中的条目添加到日历中,但我也希望能够定义颜色。如果可能,请向我指出有用的阅读材料或示例。

编辑 12/20/2015- 添加工作脚本以添加事件:

function createEvent() {
var calendarId = 'MY_ID';
var event = {
summary: 'test',
description: 'test desc',
"end": {
"date": "2015-12-21"
},
"start": {
"date": "2015-12-21"
},
colorId: 10
};
event = Calendar.Events.insert(event, calendarId);
Logger.log('Event ID: ' + event.getId());
}

【问题讨论】:

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


【解决方案1】:

哦,您是否对 Google 缺乏这方面的文档感到困惑?别担心,我也是。Here's 数字(键)和相关颜色的图表。最后看起来是这样的:

var event = {
    summary: "Summarizing",
    description: "Descriptive",
    start: {
      date: Utilities.formatDate(start, "GMT-5", "yyyy-MM-dd")
    },
    end: {
      date: Utilities.formatDate(end, "GMT-5", "yyyy-MM-dd")
    },
    colorId: 10
  };
  event = Calendar.Events.insert(event, calendarId); 
 }

希望对您有所帮助!对于第二部分:应该可以,只要确保您使用的是 Google Calendar Advanced 服务即可。如果没有更多关于你想要做什么的细节,很难提供更多信息:/

【讨论】:

  • 啊,所以关键是 colorID- 知​​道了,谢谢!我收到一个错误,我认为是因为日历高级服务未启用,即使我启用了它。我进行了测试,看看我是否可以设置颜色 ID(包含在主帖正文中的脚本)。知道为什么我会收到此错误“找不到方法 createAllDayEvent(字符串,对象,对象,对象)。(第 3 行,文件“代码”)”
  • 好吧,如果你想设置你正在创建的事件的颜色,更重要的问题是你使用的是 CalendarApp 方法,而不是高级日历服务,文档位于:developers.google.com/apps-script/advanced/calendar - 不幸的是,(据我所知)无法使用 CalendarApp 创建彩色事件。至于您的错误,高级参数应该在一个对象中,而不是分开 - 不确定这是否是错误的原因,但它会阻止代码执行。
  • 如果你想让它更简单,看看我用 Romain Vialard 写的这个例子:sites.google.com/site/scriptsexamples/…
  • 谢谢,但我也这样做是为了学习使用 Google Apps 脚本和 js。
  • 你是完全正确的!只是以防万一......对于懒惰的程序员;-)
【解决方案2】:

旧帖子,但如果有人正在寻找一些示例代码。 以下代码将所有信息存储在电子表格中:

function myFunction() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Your Sheet Name")
var index = 2;
var lastRow = sheet.getLastRow();

for (;index <= lastRow; index++){
var title = sheet.getRange(index, 1, 1, 1).getValue();
var startTime = sheet.getRange(index, 2, 1, 1).getValue();
var endTime = sheet.getRange(index, 3, 1, 1).getValue();
var description = sheet.getRange(index, 4, 1, 1).getValue();
var location = sheet.getRange(index, 5, 1, 1).getValue();
var guests = sheet.getRange(index, 6, 1, 1).getValue();
var eventColour = sheet.getRange(index, 7, 1, 1).getValue();
var sendInvites = true;

var calendar = CalendarApp.getCalendarById("your Calendar ID").createEvent(title, startTime, endTime,
{description: description, location: location, guests: guests, sendInvites: sendInvites }).getId();

if (eventColour === "Condition1") CalendarApp.getEventById(calendar).setColor("10")
if (eventColour === "Condition2") CalendarApp.getEventById(calendar).setColor("11")
if (eventColour === "Condition3") CalendarApp.getEventById(calendar).setColor("12")
if (eventColour === "Condition4") CalendarApp.getEventById(calendar).setColor("13")


}// End of For Loop
}// End of Function

【讨论】:

    【解决方案3】:

    为了快速参考,我想加入并参考 Google 的 ColorEnum 文档,并在此处列出可用的日历颜色:

     1 Pale Blue
     2 Pale Green
     3 Mauve
     4 Pale Red
     5 Yellow
     6 Orange
     7 Cyan
     8 Gray
     9 Blue
    10 Green
    11 Red
    

    【讨论】:

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