如何在 JavaScript 中获取 Google 日历事件的 ColorID

how to get the ColorID of a Google Calendar Event in JavaScript

我想编写一个脚本来将事件从一个 Google 日历复制到另一个日历,但只复制 粗体红色 的事件。

就目前而言,我可以在多个日历之间复制事件,但我不知道如何从事件中获取 ColorID,因此只有粗体红色事件被复制。 通过使用 Google 脚本,在 Javascript 中可能会出现这种情况吗?

我在过去的几天里多次搜索这个,但只能根据 PHP 找到答案。老实说,我无法想象通过 Google 脚本的 javascript 不可能发生这样的事情。

这是我目前编写的脚本:

function myFunction() }
var CalendarSource = CalendarApp.getCalendarById("CalendarIDPrimaryCalendar@gmail.com"); var CalendarDest = CalendarApp.getCalendarById("CalendarIDDuplicatedCalendar@group.calendar.google.com"); var Today = new Date(); //Deze variabele bevat de datum van vandaag var HalfYear = new Date(new Date(Today).setMonth(Today.getMonth() + 6));//Deze variabele bevat dat datum van vandaag + 6 maanden var EventToCopy = CalendarSource.getEvents(Today, HalfYear); //Deze variabele bevat alle Calendar Events voor het aankomende half jaar

// Nieuwe events aanmaken. for (var i in EventToCopy){ var newEvent = CalendarDest.createEvent(EventToCopy[i].getTitle(), EventToCopy[i].getStartTime(), EventToCopy[i].getEndTime()); } }

我想要的是添加一个 if 语句(或其他东西),它检查是否只有 ColorID 为 11 的事件才会被复制。

有人可以帮助我吗?

提前致谢,

杰克斯

如果你检查这个 documentation of Google Calendar API,它在这里解释颜色如何在日历 API 上工作的基本解释,你也可以在这里检查不同 属性 名称的描述这里。

colors.get 还为您提供了不同的示例代码。

现在,对于您想要的 javascript 或应用程序脚本代码,我认为来自 Calendar Apps Script documentation 的示例代码可以让您了解如何使用 colorId

function createEvent() {
var calendarId = 'primary';
var start = getRelativeDate(1, 12);
var end = getRelativeDate(1, 13);
var event = {
summary: 'Lunch Meeting',
location: 'The Deli',
description: 'To discuss our plans for the presentation next week.',
start: {
dateTime: start.toISOString()
},
end: {
dateTime: end.toISOString()
},
attendees: [
{email: 'alice@example.com'},
{email: 'bob@example.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());
}

经过一周的搜索、阅读、尝试并因沮丧而撞墙后,我终于创建了一个脚本来完成我想要的任务:)。你可以在下面找到它。您可以随心所欲地使用它,只需通过不更改第一条评论来承认我的工作(和我的头痛;))。当您使用它时,我也很想听听您的意见。

 /* ===========================================
    =============== Created by: ===============
    ============== Jack Reinieren =============
    ========== Date: 29 Juli 2016 =============
    ===========================================
 */

// declare variables
var PriCalendar = "PrimaryCalendar@gmail.com"; //name of primary calendar
var SecCalendar = "*********@group.calendar.google.com"; //name of secondary calendar
var CalendarSource = CalendarApp.getCalendarById(PriCalendar); //variable to store the PriCalendar in
var CalendarDest = CalendarApp.getCalendarById(SecCalendar);//variable to store the SecCalendar in
var Today = new Date(); //todays date
var HalfYear = new Date(new Date(Today).setMonth(Today.getMonth() + 6)); //todays date + 6 months
var Events = Calendar.Events.list(PriCalendar, {timeMin:Today.toISOString(), timeMax:HalfYear.toISOString()}).items; 
// The above creates a list of events with the start date of today and the end date 6 months ahead in time.

//Loop to check if there are events with colored in red if so, then copy to the Secondary Calendar
for (var i in Events){ 
  /* Check if status of events is 'confirmed'  AND the colorId of the event is "11" 
(which means "red", check the colorchart in https://eduardopereira.pt/2012/06/google-calendar-api-v3-set-color-color-chart/) 
*/
if (Events[i].status == "confirmed" && Events[i].colorId == "11"){

  /*Check whether an event already exists, 
this is because Google Calendar doesn't really delete events, when you press delete. Instead the event gets the status "cancelled". 
More info here: 
*/  
  try {
  var newEvent = Calendar.Events.insert(Events[i], SecCalendar);
  }
  catch (e){
    // I don't do anything with the errors I encounter, because for what I want, this is not really necessary.
    // You can put in anything you want of course :)
           }
       }
   }

以下是可点击的链接,代码注释中也有说明:

正如我之前所说,如果您使用(部分)此代码,我很乐意收到您的来信。

谨致问候。

杰克