GAS功能' getSheetValues'返回错误的值秒

时间:2014-04-29 15:18:09

标签: google-apps-script google-sheets

我有一个转储到电子表格中的表单,我试图使用时间戳作为从表单响应中读取和写入的唯一标识符。我使用onFormSubmit函数(从电子表格运行)来捕获响应,将其转换为对象,添加一些其他值,发送电子邮件,然后将这些值返回到电子表格。

但是,当我将这些值保存到电子表格中,并且我正在搜索具有匹配时间戳的行时,我遇到了一个问题,其中getSheetValues函数返回值中的时间值电子表格一秒钟关闭,但只有一些时间。这不是时区的问题(正如许多其他帖子所关注的那样),但电子表格实际上报告了单元格中包含的内容。

以下是电子表格中的值(直接查看时):

Timestamp   Type of Time Off    Reason For Request  Start Date   End Date   Status  Email ID    Request ID  Reviewed by Reviewed Timestamp  Reviewed Reason
4/29/2014 10:06:41  Vacation Day    I would like some time off  4/29/2014   4/30/2014                       
4/29/2014 10:08:08  Vacation Day    I would like some time off  4/29/2014   4/30/2014                       
4/29/2014 10:09:10  Vacation Day    I would like some time off  4/29/2014   4/30/2014   Pending 145add16569c73e2                
4/29/2014 10:11:22  Vacation Day    I would like some time off  4/29/2014   4/30/2014                       
4/29/2014 10:12:13  Vacation Day    I would like some time off  4/29/2014   4/30/2014                       
4/29/2014 10:13:25  Vacation Day    I would like some time off  4/29/2014   4/30/2014                       
4/29/2014 10:13:53  Vacation Day    I would like some time off  4/29/2014   4/30/2014   Pending 145add5b5f926a16    2           
4/29/2014 10:14:17  Vacation Day    I would like some time off  4/29/2014   4/30/2014                       
4/29/2014 10:16:36  Vacation Day    I would like some time off  4/29/2014   4/30/2014

以下是在该范围内调用的getSheetValues返回的值:

[
[Timestamp, Type of Time Off, Reason For Request, Start Date, End Date, Status, Email ID, Request ID, Reviewed by, Reviewed Timestamp, Reviewed Reason], 
[Tue Apr 29 10:06:40 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ], 
[Tue Apr 29 10:08:07 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ], 
[Tue Apr 29 10:09:10 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, Pending, 145add16569c73e2, , , , ], 
[Tue Apr 29 10:11:21 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ], 
[Tue Apr 29 10:12:12 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ], 
[Tue Apr 29 10:13:24 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ], 
[Tue Apr 29 10:13:53 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, Pending, 145add5b5f926a16, 2.0, , , ], 
[Tue Apr 29 10:14:16 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ], 
[Tue Apr 29 10:16:35 GMT-04:00 2014, Vacation Day, I would like some time off, Tue Apr 29 00:00:00 GMT-04:00 2014, Wed Apr 30 00:00:00 GMT-04:00 2014, , , , , , ]
]

正如你所看到的,不只是一些,但大多数时间都是一秒钟。两个匹配后的尾随值来自于我将与该请求相关的其他值保存回电子表格(时间戳有效匹配)。

这是一个已知问题吗? GAS(或谷歌电子表格)是否解释了将其抛弃的日期?某种舍入?任何建议或解决方法将不胜感激。

2 个答案:

答案 0 :(得分:0)

将时间戳转换为数字而不是日期以进行比较可能更容易。

您是否有可以与您使用的代码共享的工作表?我需要确切了解确定解决方案的确切方法。

答案 1 :(得分:0)

似乎日期对象在两个上下文中以相同的方式存储,只是以不同的方式显示。 Google表格在JavaScript的Date.toString()发言时缩短了几秒钟。

例如," 2014年1月1日12:00:00.750"显示为" 2014年1月1日12:00:01"在Google表格中使用默认的日期/时间格式。如果您改为使用包含毫秒值的自定义单元格格式,则秒数将更改回" 00"。在您的代码中,您将Date对象转换为String,并且该代码不会对该值进行舍入,而是显示" Wed Jan 01 12:00:00 GMT-05:00 2014"。

我不认为这两种行为都是严格错误的,它们只是不同。如上所述,您可能需要比较不基于字符串但基于时间戳的值。或者,使用另一种方法查找您感兴趣的行(可能是SpreadsheetApp.getActiveRange())。