简体   繁体   中英

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. The date is the main subject of this question. The texting information is written to a google spreadsheet. 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. Can someone please help me understand why google scripts is defaulting to 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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