使用Apps Script和Google的CalendarApp,我试图从两个日历中获取所有活动两天(今天+明天)并按开始时间排序。
我目前正在这样做:
// Function passed to sort() later to compare two calendar events
function compareEvents(event1, event2) {
if (event1.getStartTime().getTime() === event2.getStartTime().getTime())
return 0;
if (event1.getStartTime() < event2.getStartTime())
return -1;
return 1;
}
// Main function
function getEventsAndSortThem() {
var cal1 = CalendarApp.getCalendarsByName('<Name 1 here>')[0];
var cal2 = CalendarApp.getCalendarsByName('<Name 2 here>')[0];
var events = []
var today = new Date();
var tomorrow = new Date(today.getTime() + 1*24*3600*1000);
var cal1Events = cal1.getEvents(today, tomorrow); // Get events for two days
var cal2Events = cal2.getEvents(today, tomorrow); // Get events for two days
for (var i in cal1Events)
events.push(cal1Events[i]);
for (var i in cal2Events)
events.push(cal2Events[i]);
//Sort using compareEvents
events.sort(compareEvents);
for (var i in events) {
var event = events[i];
Logger.log(event.getStartTime()+" ["+event.getTitle()+"]");
}
}
这是最有效的方法吗?在compareEvents函数中,每个getStartTime()都会发出一个新的HTTP请求吗?
或者是当我使用getEvents()从日历中获取事件时,返回与事件相关的所有数据,并在本地处理后续的getStartTime(),而无需新的API调用?
将我想要的字段从事件保存到我自己的自定义对象并使用它执行任何后续处理会更有效吗?