我正在努力在几个日历之间构建一些逻辑。
我在检索两个日历后陷入困境。我不确定如何在底部的评论中优雅地添加我的逻辑。
function copyInstalls(){
var now = new Date();
var later = new Date("January 1, 2015 08:00:00 PDT"); // be lazy for now
// get my calendar to see whether I have install events scheduled
var my_calendar = CalendarApp.getDefaultCalendar();
var my_install_events =
CalendarApp.getCalendarById('example@group.calendar.google.com').getEvents(now, later, {search: 'Install'});
// snag the Installation Events calendar
var install_calendar = CalendarApp.getCalendarById('example@group.calendar.google.com');
var install_events = CalendarApp.getCalendarById('example@group.calendar.google.com').getEvents(now, later, {search: 'Install'});
for (var event in my_install_events){
// if it doesn't exist in install_events, create a new event
// Not sure how to do this part
}
}
答案 0 :(得分:4)
您需要明确哪些标准可以确定您的某个“安装”事件是否已经在InstallCalendar上。由于您的目的是复制原始事件的副本,因此我们不能指望事件ID是相同的。 (但是,如果您邀请 InstallCalendar,那么它将会 - 并且更好,重新安排会自动反映出来。)
我们可以假设在主题中匹配“安装”+相同的开始时间足以匹配。但是,在一个日历上重新安排会在另一个日历上孤立该事件,导致重复。
更好的选择是将自定义标记应用于InstallCalendar中的事件,为方便起见将其设置为原始事件的ID。然后,我们只需要搜索所有以前设置的标签,以查找来自my_calendar
的事件ID的匹配项。为此做准备,我们可以构建这些标记的数组:
// build array of event tags for comparison
var install_event_Tags = [];
install_events.forEach( function (event) {
install_event_Tags.push(event.getTag('origID'));
});
完成后,这就是我们如何确保我们只复制一次事件:
...
my_install_events.forEach( function (event) {
// if it doesn't exist in install_events, create a new event
var eventId = event.getId();
var match = install_event_Tags.indexOf(eventId);
if (match === -1) {
// Duplicate the event from my_calendar
var newEvent = install_calendar.createEvent(event.getTitle(), event.getStartTime(), event.getEndTime());
// Apply a tag with original event ID
newEvent.setTag('origID', eventId);
}
});
您的代码中似乎还有其他问题需要解决。
检索my_install_events
时,您正在打开共享的example
日历,该日历稍后也会用于install_events
。
// get my calendar to see whether I have install events scheduled
var my_calendar = CalendarApp.getDefaultCalendar();
var my_install_events =
CalendarApp.getCalendarById('example@group.calendar.google.com').getEvents(now, later, {search: 'Install'});
也许你的意思是......
// get my calendar to see whether I have install events scheduled
var my_calendar = CalendarApp.getDefaultCalendar();
var my_install_events = my_calendar.getEvents(now, later, {search: 'Install'});
^^^^^^^^^^^
本身并不是一个错误,但重复调用getCalendarById
会降低您的代码速度。由于您已经计算了install_calendar
,因此也可以使用它。
// snag the Installation Events calendar
var install_calendar = CalendarApp.getCalendarById('example@group.calendar.google.com');
var install_events = CalendarApp.getCalendarById('example@group.calendar.google.com').getEvents(now, later, {search: 'Install'});
更容易维护:
// snag the Installation Events calendar
var install_calendar = CalendarApp.getCalendarById('example@group.calendar.google.com');
var install_events = install_calendar.getEvents(now, later, {search: 'Install'});
^^^^^^^^^^^^^^^^
function copyInstalls(){
var now = new Date();
var later = new Date("January 1, 2015 08:00:00 PDT"); // be lazy for now
// get my calendar to see whether I have install events scheduled
var my_calendar = CalendarApp.getDefaultCalendar();
var my_install_events = my_calendar.getEvents(now, later, {search: 'Install'});
// snag the Installation Events calendar
var install_calendar = CalendarApp.getCalendarById('example@group.calendar.google.com');
var install_events = install_calendar.getEvents(now, later, {search: 'Install'});
// build array of event tags for comparison
var install_event_Tags = [];
install_events.forEach( function (event) {
install_event_Tags.push(event.getTag('origID'));
});
my_install_events.forEach( function (event) {
// if it doesn't exist in install_events, create a new event
var eventId = event.getId();
var match = install_event_Tags.indexOf(eventId);
if (match === -1) {
// Duplicate the event from my_calendar
var newEvent = install_calendar.createEvent(event.getTitle(), event.getStartTime(), event.getEndTime());
// Apply a tag with original event ID
newEvent.setTag('origID', eventId);
}
});
}
此脚本将制作从my_calendar
到InstallCalendar
的安装事件的唯一副本。这可能足以满足您的需求。但是,您可能还需要考虑其他方案:
InstallCalendar
中存在安装事件但重新安排了该怎么办? InstallCalendar
的事件已从my_calendar
中删除,该怎么办? InstallCalendar
的事件被删除了怎么办?当前脚本会再次复制它,但也许所需的行为是从my_calendar
删除它。