2016-07-25 108 views
0

我想编写一个脚本来将事件从一个Google日历复制到另一个日历,但只有上的事件以大胆的红色着色。现在,我可以在多个日历之间复制事件,但我不知道如何从事件中获取ColorID,以便只有大胆的红色事件被复制。 通过使用谷歌脚本在Javascript中可以这样做吗?如何在JavaScript中获取Google日历事件的ColorID

我在过去几天中搜索过这个,但只能找到基于PHP的答案。说实话,我无法想象这样的事情不可能通过谷歌脚本的JavaScript。

这里是我写到目前为止脚本:

 
function myFunction() }
var CalendarSource = CalendarApp.getCalendarById("[email protected]"); var CalendarDest = CalendarApp.getCalendarById("[email protected]"); 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

回答

0

搜索的一周后的想法,阅读,尝试并因为沮丧而将我的头靠在墙上,我终于创建了一个脚本来完成我想要的任务:)。你可以在下面找到它。无论如何,你都可以随意使用它,只需承认我的工作(以及我的头痛;)),而不必更改第一条评论。当您使用它时,我也很乐意收到您的来信。

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

// declare variables 
var PriCalendar = "[email protected]"; //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 :) 
      } 
     } 
    } 

这里是点击链接这是在代码的注释中还指出:

正如我以前说过的,如果您使用了此代码的部分内容,我很乐意听取您的意见。

祝好。

杰克

0

如果选中该documentation of Google Calendar API,在这里解释的颜色是如何工作的日历API的基本解释,你也可以检查这里的不同属性名称的说明这里。

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

现在,您想要的JavaScript或Google 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: '[email protected]'}, 
{email: '[email protected]'} 
], 
// Red background. Use Calendar.Colors.get() for the full list. 
colorId: 11 
}; 
event = Calendar.Events.insert(event, calendarId); 
Logger.log('Event ID: ' + event.getId()); 
} 
+0

感谢KENdi为您的快速反应:)。据我可以看到这个代码在日历中创建一个事件。 我想要的是搜索已经创建的事件。带有“colorId:11”的事件必须复制到另一个日历。“11”之外的“colorId”事件不能复制到第二个日历。 也许KENDI或其他人可以帮助我呢?可以肯定的是,我并不想要_calendar_的ColorID,我想要在日历中创建的_events_(“约会”或“会议”)的ColorID。 – Jackuss

相关问题