在将日历数据提取到电子表格时处理时区

时间:2013-02-14 17:55:42

标签: javascript timezone google-apps-script google-calendar-api google-sheets

我正在制作一个小型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;
}

1 个答案:

答案 0 :(得分:1)

我明白了。

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

ss.setSpreadsheetTimeZone(cal.getTimeZone());