0

我正在开发一个小的 Google Apps 脚本,它将事件数据从选定的 Google 日历导出到选定日期范围内的新 Google 电子表格。我遇到的一个问题是复制到电子表格的时间相差了三个小时。有关如何处理此问题并正确显示事件时间的任何建议?

到目前为止,这是我的代码:(这是一个正在进行的工作)

function doGet() {
  var app = UiApp.createApplication();
  var handler = app.createServerHandler("change");
  var picker1 = app.createDatePicker().addValueChangeHandler(handler).setId("picker1");
  var picker2 = app.createDatePicker().addValueChangeHandler(handler).setId("picker2");
  var pickerpanel = app.createHorizontalPanel();
  var panel = app.createVerticalPanel();
  pickerpanel.add(picker1);
  pickerpanel.add(picker2);
  panel.add(pickerpanel);
  var lb = app.createListBox(false).setId('lbCalSelId').setName('lbCalSelect');

  lb.setVisibleItemCount(3);
  var cals = CalendarApp.getAllCalendars();
  for (var i=0; i<cals.length;i++) {
    lb.addItem(cals[i].getName(),cals[i].getId());
  }
  panel.add(lb);

  var button = app.createPushButton().setText("Export").setId("button");
  var handler = app.createServerClickHandler('doExport').addCallbackElement(panel);
  button.addClickHandler(handler);
  panel.add(button);
  app.add(panel);
  return app;  
}

function change(eventInfo) {
  var app = UiApp.getActiveApplication();
  if (eventInfo.parameter.picker1) {
    UserProperties.setProperties({"DateRangeStart":eventInfo.parameter.picker1});
    app.add(app.createLabel("Start date " + eventInfo.parameter.picker1));
  }
  else if (eventInfo.parameter.picker2) {
    UserProperties.setProperties({"DateRangeEnd":eventInfo.parameter.picker2});
    app.add(app.createLabel("End date" + eventInfo.parameter.picker2));
  }

  return app;
}

function doExport(eventInfo) {
  var app = UiApp.getActiveApplication();
  var calId = eventInfo.parameter.lbCalSelect;
  var cal = CalendarApp.getCalendarById(calId);
  var rangeStart = UserProperties.getProperty("DateRangeStart");
  var rangeEnd = UserProperties.getProperty("DateRangeEnd");
  app.add(app.createLabel("The button was clicked!"));
  if (rangeStart && rangeEnd) {
   app.add(app.createLabel("exporting..."));
   var events = cal.getEvents(new Date(rangeStart), new Date(rangeEnd));
   var eventsData = [];
   var headerRow = ['Title','Start Time','End Time','Location','Description'];
   for (var i=0; i < events.length; i++) {
     var eventData = [];
     eventData.push(events[i].getTitle());
     eventData.push(events[i].getStartTime());
     eventData.push(events[i].getEndTime());
     eventData.push(events[i].getLocation());
     eventData.push(events[i].getDescription());
     eventsData.push(eventData);
   }
   var ss = SpreadsheetApp.create("Export of " + cal.getName() + " from " + rangeStart + " to " + rangeEnd);
   var sheet = ss.getSheets()[0];
   var destRange = sheet.getRange(1, 1, events.length, headerRow.length);
   destRange.setValues(eventsData);
  }
  else {
   app.add(app.createLabel("Range not specified."));
  }

  return app;
}
4

1 回答 1

1

我想到了。

我只是将电子表格的时区设置为与日历相同的时区,它就像一个魅力。

ss.setSpreadsheetTimeZone(cal.getTimeZone());
于 2013-02-15T16:00:33.347 回答