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

时间:2016-07-25 20:27:54

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

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

现在,我可以在多个日历之间复制事件,但我不知道如何从事件中获取ColorID,因此只会复制粗体红色事件。 使用Google Script在Javascript中是否可以这样做?

我在过去几天中多次搜索过,但只能找到基于PHP的答案。说实话,我无法通过谷歌脚本的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的事件将被复制。

有人能帮助我吗?

先谢谢,

Jackuss

2 个答案:

答案 0 :(得分:1)

经过一周的搜索,阅读,尝试并因为沮丧而撞到墙上,我终于创建了一个脚本来完成我想要的任务:)。你可以在下面找到它。你可以自由地使用它,只需通过不改变第一条评论来承认我的工作(以及我的头痛;))。当你使用它时,我也很想听到你的声音。

 /* ===========================================
    =============== 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: https://stackoverflow.com/questions/27542721/creating-the-same-event-after-removing-causes-409-error
*/  
  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 :)
           }
       }
   }

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

正如我之前所说,如果您使用(部分)此代码,我很乐意听取您的意见。

亲切的问候。

杰克

答案 1 :(得分:0)

如果你选中documentation of Google Calendar API,它会在这里解释有关颜色如何在Calendar API上运行的基本说明,你也可以在这里查看不同属性名称的描述。

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

现在,对于您想要的javascript或apps脚本代码,我认为来自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());
}