我一直在使用此脚本将数据从Google日历中提取到电子表格中。我有几个问题需要改进:
谢谢!
function caltest3(){
var ss = SpreadsheetApp.openById("SPREADSHEET ID");
SpreadsheetApp.setActiveSpreadsheet(ss);
var cal=CalendarApp.getCalendarById("CALENDAR ID");
var sheet = SpreadsheetApp.getActiveSheet();
var events = cal.getEvents(new Date("January 1, 2013"), new Date("January 13, 2013"));
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var details=[[events[i].getTitle(), events[i].getStartTime(),
events[i].getEndTime(), events[i].getDescription(),
events[i].getLocation()]];
var row=i+1;
var range=sheet.getRange(row+1,1,1,5);
range.setValues(details);
}
}
}
答案 0 :(得分:3)
简短答案然后是一个例子:
CalendarApp
无法执行此操作,但如果循环遍历calendarIds列表并将该日历的结果事件添加到数组中,则可以对此数组进行排序并将其放入电子表格details
数组中的其他项目,只需记住按1
无论是从多个日历构建日历事件数组还是只构建一个日历事件数组,将它们添加到循环中的数组中,然后将它们写入任何循环外的电子表格,这比每次调用时逐行写入更好getRange()
&amp; setValues
方法它们是单独的API调用,这些调用对于Apps脚本来说是最昂贵的。只使用一个范围调用一次以适合您的数据,您的脚本将更快地运行一个数量级。
下面的脚本说明了答案1&amp; 2.如果您要为此设置计时器,您的需求可能会有所不同,因为您可能想要影响您查询的时间段?
function caltest3(){
var ss = SpreadsheetApp.openById( 'spreadsheetId' ),
sheet = ss.getSheetByName( 'sheetName' ),
cals = ['id1', 'id2', 'id3'], c, cal, calName,
start = new Date( 'whenever' ), end = new Date( 'whenever' ),
events, i, details,
eventslog = [], e,
rows = [], range;
for (c = 0; c < cals.length; c += 1) {
cal = CalendarApp.getCalendarById(cals[c]);
calName = cal.getTitle();
events = cal.getEvents(start, end);
// add the events of the current calendar to the array of all events
eventslog = eventslog.concat(
events.map(function(event) {
return {
time: new Date(event.getStartTime()).getTime(), // sort by this
details: [
event.getTitle(),
event.getStartTime(),
event.getEndTime(),
event.getDescription(),
event.getLocation(),
calName // change calendar info position in array to suit
]
};
})
);
}
// sort array of event so date order can be either way by reversing a & b
eventslog.sort(function(a, b) { return a.time - b.time; });
rows = eventslog.map(function(entry) { return entry.details; });
range = sheet.getRange(2, 1, rows.length, 6);
range.setValues(rows);
}
为清晰起见,代码间隔开来