如何在不影响脚本的情况下使用IMPORTRANGE填充创建日历事件表?

时间:2016-06-11 01:29:42

标签: google-apps-script google-sheets

此公式在A2中,并从我的预订表中的数据填充

=QUERY(
   ImportRange("Sheet_Key","sheet1!A7:Z"),
   "Select Col7,Col2,Col8,Col9,Col12,Col19"
 )

日历事件脚本https://stackoverflow.com/a/15790894/6400958运行正常,在直接输入数据时需要5-10秒才能运行,但是当导入范围公式与完全相同的数据量使用时,会产生重复并超过运行时间。 / p>

这是Demo file。请注意,事件数据只有三行。

以下是剧本:

 /**
  * Adds a custom menu to the active spreadsheet, containing a single menu item
  * for invoking the exportEvents() function.
  * The onOpen() function, when defined, is automatically invoked whenever the
  * spreadsheet is opened.
  * For more information on using the Spreadsheet API, see
  * https://developers.google.com/apps-script/service_spreadsheet
  */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Export Events",
    functionName : "exportEvents"
  }];
  sheet.addMenu("Calendar Actions", entries);
};

/**
 * Export events from spreadsheet to calendar
 */
function exportEvents() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var headerRows = 1;  // Number of rows of header info (to skip)
  var range = sheet.getDataRange();
  var data = range.getValues();
  var calId = "My_CALENDAR_ID";
  var cal = CalendarApp.getCalendarById(calId);
  for (i=0; i<data.length; i++) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[0]);  // First column
    var title = row[1];           // Second column
    var tstart = new Date(row[2]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[3]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = row[4];
    var desc = row[5];
    var id = row[6];              // Sixth column == eventId
    // Check if event already exists, update it if it does
    try {
      var event = cal.getEventSeriesById(id);
    }
    catch (e) {
      // do nothing - we just want to avoid the exception when event doesn't exist
    }
    if (!event) {
      //cal.createEvent(title, new Date("March 3, 2010 08:00:00"), new Date("March 3, 2010 09:00:00"), {description:desc,location:loc});
      var newEvent = cal.createEvent(title, tstart, tstop, {description:desc,location:loc}).getId();
      row[6] = newEvent;  // Update the data array with event ID
    }
    else {
      event.setTitle(title);
      event.setDescription(desc);
      event.setLocation(loc);
      // event.setTime(tstart, tstop); // cannot setTime on eventSeries.
      // ... but we CAN set recurrence!
      var recurrence = CalendarApp.newRecurrence().addDailyRule().times(1);
      event.setRecurrence(recurrence, tstart, tstop);
    }
    debugger;
  }
  // Record all event IDs to spreadsheet
  range.setValues(data);
}

1 个答案:

答案 0 :(得分:1)

开放式引用("sheet1!A7:Z")很可能会使var range = sheet.getDataRange();的结果范围包含大量空白行。添加where子句以过滤掉空行,例如where Col2 <> ''。最终的公式将如下所示

=QUERY(
  ImportRange("Sheet_Key","sheet1!A7:Z"),
  "Select Col7,Col2,Col8,Col9,Col12,Col19 where Col2 <> ''"
 )