将Google或iCal日历数据导入Google电子表格

时间:2015-02-11 23:52:47

标签: google-apps-script google-calendar-api

我想在谷歌电子表格中保留谷歌日历条目的记录,以进一步处理数据。我有以下代码,我从其他来源借用:

function importEvents(){
var startOfDay = new Date();
  startOfDay.setUTCHours(0);
  startOfDay.setMinutes(0);
  startOfDay.setSeconds(0);
  startOfDay.setMilliseconds(0);  
var endOfDay = new Date(startOfDay.getTime() + 24 * 60 * 60 * 1000);
var Calendar = CalendarApp.getCalendarById("[calendarIDhere]");
var events = Calendar.getEvents(startOfDay, endOfDay)
var events_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ImportedEvents");
var lr = events_sheet.getLastRow();
var eventarray = new Array();
var i = 0; // edited
 for (i = 0; i < events.length; i++) {
  line = new Array();
  line.push(events[i].getTitle());
  line.push(events[i].getDescription());
  line.push(events[i].getStartTime());
  line.push(events[i].getEndTime());
  eventarray.push(line);
 }

events_sheet.getRange('A1:D' + (lr)).setValues(eventarray);

var l = events_sheet.getLastRow();
var m = events_sheet.getMaxRows();
events_sheet.deleteRows(l+1,m-l);
}

使用.getRange()函数获取有关数据高度不正确的错误

1 个答案:

答案 0 :(得分:1)

如果您在某个范围内设置值,则您的范围必须与列和行中值数组的大小相匹配。

而不是以下行:

events_sheet.getRange('A1:D' + (lr)).setValues(eventarray);

试试这个:

events_sheet.getRange('A1:D' + eventarray.length).setValues(eventarray);