Panorama
The cell A1
has the value 01:00
(one hour). By using a function like the following:
function prueba() {
var libro = SpreadsheetApp.getActiveSpreadsheet();
var hoja = libro.getSheetByName("Hoja 1");
var datos = hoja.getDataRange().getValues();
var valor = datos[0][0];
Logger.log(valor.getHours());
Logger.log(valor.getMinutes());
}
Values are recorded
1.0
36.0
The expected is that the recorded values would be:
1.0
0.0
How can I make my code read the expected values?
Comments
This is a "real question" for which I found the answer on stackoverflow.com . See reference.
The question wasn't translated, but you could tell it's based on the reference.
Panorama
The spreadsheet and Javascript handle dates differently so it is necessary to introduce a few additional lines of code. A main function and two complementary ones are presented to obtain seconds, minutes or hours from a date.
Code
Modifications to the function presented in the question:
From Eric Koleda 's answer on 2013-07-22 14:20:54Z the following helper functions were taken:
Example of use to register the hours of the cell
A1
.Explanation
From Sergei insas ' answer on 2013-07-18 15:36:28Z
Spreadsheets use as a reference date
12/30/1899
while Javascript uses01/01/1970
, this means that there is a difference of 25,568 days between both references. This is assuming the same time zone is used on both systems. When converting a date value in a spreadsheet to a Javascript date object, the GAF engine automatically adds the difference to maintain consistency between the two dates.In this case you don't want to know the actual date of something, instead you want an absolute value of hours, for example a duration, so you need to remove the 25.568 day offset. This is done using the
getTime()
Javascript method which returns milliseconds counted from the reference date, the only thing you know is the value in milliseconds of the reference spreadsheet date and subtract this value from the date object. current. Then a bit of math to get hours instead of milliseconds and we're done.Comments
Issue 402: Get literal string from a spreadsheet range was mentioned in Eric's answer, but this was marked as fixed 4 days ago with the note that documentation is yet to be published. It was resolved by adding range.getDisplayValue and range.getDisplayValues, however, this might not be useful if you don't want to modify the format in which a date type data is displayed.
Regarding the code, the comments included in it were translated, but the variable names were left as is. Introduced a line break on one of the lines that was too long to prevent the horizontal scrollbar from showing.
Short answer
From a beginner's, or "quick and dirty" point of view, perhaps the simplest solution is that instead of using methods like
getValue
/getValues
usegetDisplayValue
/getDisplayValues
to work with String objects instead of Date objects.Basic concepts
Internally, Google and JavaScript spreadsheets use serial numbers to handle dates and times, that is, they use an "EPOCH", as a reference point or zero and from which time units are added to determine the date-time.
time unit
EPOCH
Conversion
When Google Apps Script reads a time value, it also considers the spreadsheet time zone and the Apps Script project time zone, which are not always the same, and this could add a few minutes and seconds in certain cases, for example
0:00:00
, the Date object will showSat Dec 30 1899 00:36:36 GMT-0600 (CST)
Example
Modification of the question code
Another example
The following function takes the data from the active cell and if it is later than 7:05 it writes to the right
Sí
, and otherwiseNo
. Use getDisplayValue and the match method of the String object with the regular expression/\d\d/g
to extract the hour and minute.