我使用Google脚本从资源日历中提取数据。该脚本按预期提取单个日历,但我想将多个资源日历提取到1个表中。有人能指出我正确的方向吗?我已经考虑创建一个数组,将多个日历包含在“mycal”中,但我无法让它运行。感谢
function export_gcal_to_gsheet(){
var mycal = "engelska.se_xxxx@resource.calendar.google.com";
var cal = CalendarApp.getCalendarById(mycal);
var today = new Date();
var events = CalendarApp.getCalendarById(mycal).getEventsForDay(today);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clearContents();
var header = [["Calendar Address", "Event Title", "Event Description", "Event Location", "Event Start", "Event End", "Calculated Duration", "Visibility", "Date Created", "Last Updated", "MyStatus", "Created By", "All Day Event", "Recurring Event"]]
var range = sheet.getRange(1,1,1,14);
range.setValues(header);
for (var i=0;i<events.length;i++) {
var row=i+2;
var myformula_placeholder = '';
var details=[[mycal,events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), events[i].getStartTime(), events[i].getEndTime(), myformula_placeholder, ('' + events[i].getVisibility()), events[i].getDateCreated(), events[i].getLastUpdated(), events[i].getMyStatus(), events[i].getCreators(), events[i].isAllDayEvent(), events[i].isRecurringEvent()]];
var range=sheet.getRange(row,1,1,14);
range.setValues(details);
var cell=sheet.getRange(row,7);
cell.setFormula('=(HOUR(F' +row+ ')+(MINUTE(F' +row+ ')/60))-(HOUR(E' +row+ ')+(MINUTE(E' +row+ ')/60))');
cell.setNumberFormat('.00');
}
}
答案 0 :(得分:0)
如果你将mycal变成一个数组,你将需要第二个循环来逐步通过该数组,并且基于mycal的变量赋值将需要在该循环中。你已经拥有的事件循环也需要在循环中。因为每个日历中的事件数可能不同,所以维护行计数器的最佳方法是在两个循环之前声明它并在写入行之后递增它。
function export_gcal_to_gsheet(){
var mycal = ["engelska.se_xxxx@resource.calendar.google.com",
"calenderaddress2",
"calenderaddress3",
"calenderaddress4"];//note , between address, no , after last
//here we do all the things that need to be done only once.
var today = new Date();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.clearContents();
var header = [["Calendar Address", "Event Title", "Event Description", "Event Location", "Event Start", "Event End", "Calculated Duration", "Visibility", "Date Created", "Last Updated", "MyStatus", "Created By", "All Day Event", "Recurring Event"]]
var range = sheet.getRange(1,1,1,14);
range.setValues(header);
var row=2;// this needs to be outside the loops in order to use it as a counter in nested loops
for (var j = 0; j< mycal.lengh; j++){
//here we do the things we do once per calander
var events = CalendarApp.getCalendarById(mycal[j]).getEventsForDay(today);//refer to the calender in mycal based on loop counter
var cal = CalendarApp.getCalendarById(mycal[j]);//refer to the calender in mycal based on loop counter
//incerting a header for each calander would be done here. make sure to increment row
for (var i=0;i<events.length;i++) {
var myformula_placeholder = '';
var details=[[mycal,events[i].getTitle(), events[i].getDescription(), events[i].getLocation(), events[i].getStartTime(), events[i].getEndTime(), myformula_placeholder, ('' + events[i].getVisibility()), events[i].getDateCreated(), events[i].getLastUpdated(), events[i].getMyStatus(), events[i].getCreators(), events[i].isAllDayEvent(), events[i].isRecurringEvent()]];
var range=sheet.getRange(row,1,1,14);
range.setValues(details);
var cell=sheet.getRange(row,7);
cell.setFormula('=(HOUR(F' +row+ ')+(MINUTE(F' +row+ ')/60))-(HOUR(E' +row+ ')+(MINUTE(E' +row+ ')/60))');
cell.setNumberFormat('.00');
row++; //increment row counter.
}
}
}