Google表格中的日期/时间不会将正确的时间传输到Google日历中 [英] Date/Time in Google Sheet not transferring the correct time into Google calendar

查看:131
本文介绍了Google表格中的日期/时间不会将正确的时间传输到Google日历中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本来填充一个事件的谷歌日历。
我有一个日期字段和一个开始和结束时间字段。在Google电子表格中,当我运行下面的脚本时,开始时间是13:00:00,结果日历开始时间是14:00:00。

 函数onOpen(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name:Export Events,
functionName:exportEvents
}];
sheet.addMenu(日历操作,条目);
};
$ b / **
*将电子表格中的事件导出到日历
* /
function exportEvents(){
var sheet = SpreadsheetApp.getActiveSheet();
var headerRows = 1; //标题信息的行数(跳过)
var range = sheet.getDataRange();
var data = range.getValues();
var calId =trade.nsw.gov.au_004rslcuubrtlg81mkv48lse9c@group.calendar.google.com;
var cal = CalendarApp.getCalendarById(calId);
for(i in data){
if(i var row = data [i];
var date = new Date(row [2]); //第一列
var title = row [1]; //第二列
var tstart = new Date(row [3]);
tstart.setDate(date.getDate());
tstart.setMonth(date.getMonth());
tstart.setYear(date.getYear());
var tstop = new Date(row [4]);
tstop.setDate(date.getDate());
tstop.setMonth(date.getMonth());
tstop.setYear(date.getYear());
var loc = row [5];
var desc = row [6];
var id = row [10]; //第六列== eventId
//检查事件是否已经存在,如果是,则更新它
try {
var event = cal.getEventSeriesById(id);
}
catch(e){
//什么都不做 - 我们只是想避免在事件不存在的情况下发生异常
}
if(!event) {
//cal.createEvent(title,new Date(March 3,2010 08:00:00),new Date(March 3,2010 09:00:00),{description:desc,位置:LOC});
var newEvent = cal.createEvent(title,tstart,tstop,{description:desc,location:loc})。getId();
row [10] = newEvent; //用事件ID
更新数据数组
else {
event.setTitle(title);
event.setDescription(desc);
event.setLocation(loc);
// event.setTime(tstart,tstop); //无法在eventSeries上设置时间。
// ...但我们可以设置重复!
// var recurrence = CalendarApp.newRecurrence()。addDailyRule()。times(1);
//event.setRecurrence(recurrence,tstart,tstop);
}
调试器;
}
//将所有事件ID记录到电子表格
range.setValues(data);


解决方案

问题很简单,但解释不那么简单...



让我们试试看!



修改代码:

  var tstart = new Date(row [3]); 
Logger.log('hour only ='+ tstart)
tstart.setDate(date.getDate());
Logger.log('setDate ='+ tstart)
tstart.setMonth(date.getMonth());
Logger.log('setMonth ='+ tstart)
tstart.setYear(date.getYear());
Logger.log('setYear ='+ tstart)

正如您所看到的,我添加了几个 Logger.log 在您的代码中,并注意到时间值的差异,如下所示

  [14-07-28 16:18:53:441 BST]小时=星期六12月30日18:00 10:00:00 GMT-0000(GMT)
[14-07-28 16 :18:53:442 BST] setDate = Fri Dec 18 1899 10:00:00 GMT-0000(GMT)
[14-07-28 16:18:53:442 BST] setMonth = Sat 1899年7月29日10:00:00 GMT + 0100(BST)
[14-07-28 16:18:53:442 BST] setYear = Tue Jul 29 2014 10:00:00 GMT + 0100(BST)

当您仅创建数小时的日期对象时,该日期在逻辑上是12月30日1899年的冬季。 30 1899是在JS日期中用作参考的时代)

然后你设置日期(仍然在十二月),然后七月和七月的月份是明显的在夏天......你可以在记录器中看到日期对象已经从GMT切换到BST(英国夏令时,我猜?)和f在那里它是1小时后。



为了解决你有几种可能性,最简单的方法是在电子表格本身中合并时间和日期,这将创建一个完整的日期对象,不需要任何改变。



如果你不能(或不想)改变你的SS布局,我想更简单的方法是是使用 Utilities.formatDate()将您的时间值转换为字符串,并将此字符串解析为整数值以获得小时和分钟。



你必须改变你日期操作的序列顺序,但是从我看到的你的编码技巧应该不是问题。



<希望我的解释足够清楚;)


I have this script which is to populate a google calendar with an event. I have a date field and a start and finish time field. While in the google spreadsheet the start time is say 13:00:00 when I run the below script the resulting calendar start time is 14:00:00. Same thing happens with the end time.

    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 = "trade.nsw.gov.au_004rslcuubrtlg81mkv48lse9c@group.calendar.google.com";
  var cal = CalendarApp.getCalendarById(calId);
  for (i in data) {
    if (i < headerRows) continue; // Skip header row(s)
    var row = data[i];
    var date = new Date(row[2]);  // First column
    var title = row[1];           // Second column
    var tstart = new Date(row[3]);
    tstart.setDate(date.getDate());
    tstart.setMonth(date.getMonth());
    tstart.setYear(date.getYear());
    var tstop = new Date(row[4]);
    tstop.setDate(date.getDate());
    tstop.setMonth(date.getMonth());
    tstop.setYear(date.getYear());
    var loc = row[5];
    var desc = row[6];
    var id = row[10];              // 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[10] = 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);
}

解决方案

The problem is simple but the explanation is less simple...

Let us try anyway !

code modification :

var tstart = new Date(row[3]);
Logger.log('hour only = '+tstart)
tstart.setDate(date.getDate());
Logger.log('setDate = '+tstart)
tstart.setMonth(date.getMonth());
Logger.log('setMonth = '+tstart)
tstart.setYear(date.getYear());
Logger.log('setYear = '+tstart)

As you see, I added a couple of Logger.log in your code and notice a difference in time value like shown below

[14-07-28 16:18:53:441 BST] hour only = Sat Dec 30 1899 10:00:00 GMT-0000 (GMT)
[14-07-28 16:18:53:442 BST] setDate = Fri Dec 29 1899 10:00:00 GMT-0000 (GMT)
[14-07-28 16:18:53:442 BST] setMonth = Sat Jul 29 1899 10:00:00 GMT+0100 (BST)
[14-07-28 16:18:53:442 BST] setYear = Tue Jul 29 2014 10:00:00 GMT+0100 (BST)

When you create the date object with hours only, the date is logically december 30 1899 which is in winter.(december 30 1899 is the "epoch" used as reference in JS date)

Then you set the date (still in december) and then the month which is July and July is obviously in summer... as you can see in the logger, the date object has switched from GMT to BST (british summer time I guess ?) and from there it is 1 hour later.

To solve that you have a few possibilities, the simplest one would be to combine time and date in the spreadsheet itself, this would create a complete date object that wouldn't need any change.

If you can't (or don't want) to change your SS layout I guess the easier way would be to convert your time value to a string using Utilities.formatDate() and parse this string into integer values to get hours and minutes.

You'll have to change a bit the sequence order of your date manipulations but from what I see of your coding skills that should not be a problem.

Hoping my explanation was clear enough ;)

这篇关于Google表格中的日期/时间不会将正确的时间传输到Google日历中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆