简体   繁体   English

Google脚本Javascript日期错误

[英]Google Scripts Javascript Date Error

I wrote texting software that, when a number receives a text, writes to a google spreadsheet the date and other information. 我写了短信软件,当号码收到短信时,它会将日期和其他信息写到google电子表格中。 The date is the main subject of this question. 日期是这个问题的主要主题。 The texting information is written to a google spreadsheet. 短信信息将写入Google电子表格。 I have another spreadsheet that is written to when the texting number receives a certain input. 我有另一个电子表格,当短信号码收到某个输入时,该电子表格将被写入。 When the month of the dates change, I want the new spreadsheet to create a break in the month recording, and create a heading for a new month. 当日期的月份更改时,我希望新的电子表格在月份记录中创建一个休息时间,并为新月份创建标题。 In order to write to the new spreadsheet and not overwrite the old entries, I am using this function: 为了写入新的电子表格而不覆盖旧的条目,我使用了以下功能:

var getFirstEmptyRowWholeRowInPaymentSheet = function() {
  var sheet = SpreadsheetApp.openById("Spreadsheet ID");
  var range = sheet.getDataRange();
  var values = range.getValues();
  var row = 0;
  for (var row=0; row<values.length; row++) {
    if (!values[row].join("")) break;
  }
  return (row+1);
}

As I said before, I want to compare the date that the text was received on with the old date. 正如我之前所说,我想将收到文本的日期与旧日期进行比较。 Here is the log statements I was using to try to figure out the problem: 这是我用来试图找出问题的日志语句:

var finalRow = getFirstEmptyWholeRowInPaymentSheet();
    var dayHolder = splitDate[1];
             var amountPaid = messageBody.split(" ")[2];
            Logger.log("Final Row: " + finalRow);
            Logger.log("Final Row - 1: " + (finalRow-1));
            Logger.log("Recieved Date: " + e.values[0]);
            Logger.log("Previous Date: " + paymentData.getCell((finalRow-1),1).getValues());
            Logger.log("Previous previous date: " + paymentData.getCell((finalRow - 2),1).getValues());
            Logger.log("Previous previous date: " + paymentData.getCell((finalRow - 3),1).getValues());
            Logger.log("Previous previous date: " + paymentData.getCell((finalRow - 4),1).getValues());
            Logger.log("Previous previous date: " + paymentData.getCell((finalRow - 5),1).getValues());

This is what the log looks like: 日志如下所示:

1: Final Row: 10
2: Final Row - 1: 9
3: Received Date: 1/6/2017 10:32:18
4: Previous Date: Sun Jan 01 2017 00:00:00 GMT-0800 (PST)
5: Previous previous date: undefined
6: Previous previous date: undefined
7: Previous previous date: undefined
8: Previous previous date: undefined

This is not correct, the previous date should be 1/6/2017 10:17:47. 这是不正确的,以前的日期应该是1/6/2017 10:17:47。 Can someone please help me understand why google scripts is defaulting to Sun Jan 01 2017 00:00:00 GMT-0800 (PST)? 有人可以帮我了解为什么Google脚本默认为Sun Jan 01 2017 00:00:00 GMT-0800(PST)吗?

By using getValues() (plural) instead of getValue() (singular) you are returning a "Rectangular Grid" (a two-dimensional array) instead of the individual cell value you want: 通过使用getValues() (复数)而不是getValue() (单数),您将返回“矩形网格”(二维数组)而不是所需的单个单元格值:

Logger.log(paymentData.getCell((finalRow-1),1).getValue());

https://developers.google.com/apps-script/reference/spreadsheet/range#getValue() https://developers.google.com/apps-script/reference/spreadsheet/range#getValue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM