1

在 Windows 7 机器上运行 firefox 23.0.1

我正在尝试将一长串日期(1,000-2,000)从我的日历导入电子表格。它工作得很好,直到我开始添加我所有的学生课程会议。现在它似乎工作了,但我收到了超时消息,这需要很长时间。

有一张单独的表格,只记录 A1:学生姓名、B1:位置和 C:1 开始时间

function calpull(){
  //http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
  // The code below will retrieve events between 2 dates for the user's default         calendar and
  // display the events the current spreadsheet
     var cal = CalendarApp.getDefaultCalendar();
     var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
     var events = cal.getEvents(new Date("August 15,2013"), new Date("January 15, 2014"));

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].getLocation(),     events[i].getStartTime(),]];
   // the +3 makes the data start on the third row, protecting the first two rows
     var row=i+3;
      //for some unknown reason, I have to set the active sheet here
     var range=SpreadsheetApp.getActiveSpreadsheet().getSheets()[1].getRange(row,1,1,3);
    range.setValues(details);
  }
}

我的问题是,我认为它会单独读取每个事件,并将其放在一行中。我试图弄清楚如何将我想要的所有数据保存到一个数组中,然后将数组一次保存到电子表格中。

我的最后一次脚本尝试看起来像这样,但不起作用:

function calpull(){
  //http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
  // The code below will retrieve events between 2 dates for the user's default calendar and
  // display the events the current spreadsheet
     var cal = CalendarApp.getDefaultCalendar();
     var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
     var events = cal.getEvents(new Date("August 15,2013"), new Date("August 15, 2014"));

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].getLocation(),                 events[i].getStartTime(),]];
   // the +3 makes the data start on the third row, protecting the first two rows
     var row=i+3;
      //for some unknown reason, I have to set the active sheet here
     var range=ss.getRange(row,1,1,3);
    range.setValues(details);
  }
}

我的问题是,我认为它会单独读取每个事件,并将其放在一行中。我试图弄清楚如何将我想要的所有数据保存到一个数组中,然后将数组一次保存到电子表格中。

非常菜鸟问题,我似乎总是缺少一些基本信息。每个人都认为我会知道。

4

1 回答 1

3

这是一个简单的例子,说明如何使用数组来做到这一点:

1 - 从日历获取数据:

  var Calendar = CalendarApp.getCalendarsByName('calendar_name');
  var events = Calendar[0].getEvents(startDate , endDate, {max: 4000});// this option to ensure that you get more than the default 1000 events
  if (events[0]){
    var eventArray = new Array();// this array will hold the whole sheet data
    for (i = 0; i < events.length; i++) {
      var line = new Array();// this array will hold data for each row
      FUS1=events[i].getStartTime().toString().substr(25,6)+":00";// this variable is used to take care of daylight savings parameter of each event to prevent time errors on long periods of time
      var title = events[i].getTitle()
      line.push(title);
      line.push(Utilities.formatDate(events[i].getStartTime(), FUS1, "dd-MM-yyyy")+' à ' +Utilities.formatDate(events[i].getStartTime(), FUS1, "HH:mm"));
      line.push(Utilities.formatDate(events[i].getEndTime(), FUS1, "dd-MM-yyyy")+' à ' +Utilities.formatDate(events[i].getEndTime(), FUS1, "HH:mm"));
      line.push(events[i].getLocation());
      line.push(' -- ');// this can be removed, it simply adds a column with "--"
      line.push(((events[i].getEndTime() - events[i].getStartTime())/ 3600000).toString().replace('.',','));
      line.push(' ')
      eventArray.push(line);
    }
  }

2 - 将数组写入工作表(并即时格式化列......但这纯粹是可选的):

  sheet.getRange(1,1,eventArray.length,eventArray[0].length).setValues(eventArray);
  sheet.setColumnWidth(1, 450).setColumnWidth(2, 150).setColumnWidth(3, 150).setColumnWidth(4, 250).setColumnWidth(5, 120).setColumnWidth(6, 75).setColumnWidth(7, 450);
于 2013-09-05T05:52:50.363 回答