简体   繁体   中英

Reading and writing time values from/to a spreadsheet using GAS

I really thought I was starting to understand how this works until I tried to distil a test case for this question and am totally confused again.

With a google spreadsheet you can write a custom function like this:

function dateToSeconds(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  return (hours*3600) + (minutes*60) + seconds;
}

and call it in a cell by doing eg

=dateToSeconds(A1)

and it does what you expect. However, if you call it like so:

function test() {
    var sheet = SpreadsheetApp.getActiveSheet();

    sheet.getRange("A2").setValue(dateToSeconds(sheet.getRange("A1").getValue()));
}

there's a discrepancy of 25m 21s. What the heck?

Also, if you want to return a date object from a script so that it is formatted as a time, you can do eg

function newTime(hours, minutes, seconds) {
    // 1899-12-30 is the epoch for time values, it seems
    // http://stackoverflow.com/questions/4051239/how-to-merge-date-and-time-as-a-datetime-in-google-apps-spreadsheet-script
   return new Date(1899, 11, 30, hours, minutes, seconds, 0);
 }

and call it by doing:

=newTime(A1, A2, A3)

and it works fine. If you do:

function test() {
    var sheet = SpreadsheetApp.getActiveSheet();
    var values = sheet.getRange("A1:C3").getValues();
    sheet.getRange("D3").setValue(newTime(values[0], values[1], values[2]));
}

then (given an input of 11:00:00) it gets formatted as a date like:

01/01/1970 01:00:00

which looks like it's being interpreted as an hour after the more traditional 1970 epoch? I thought the behaviour I had seen before was a bit symmetrical with the 25m 21s offset above and I'd see:

10:34:39

My test case is here

There does seem to be something strange going on with the behavior of your =dateToSeconds() versus your test() function. It looks like there is a bug that's causing the time discrepancy between the two. Please raise a bug for that in the issue tracker .

I was however able to get this working by using number formatting on the cells. Have a look at my modified copy of your spreadsheet . In cell A2 I entered "29/04/2012 11:00:00" and then set Format > Number > 15:59:00 Time. You can also do this programmatically by using Range.setNumberFormat ("HH:mm:ss"); This results in cell A2 showing 11:00:00.

Then, I modified your test function to have this code:

function test() {
  var sheet = SpreadsheetApp.getActiveSheet();

  sheet.getRange("E2").setValue(dateToSeconds(sheet.getRange("A2").getValue()));
  Logger.log(sheet.getRange("A2").getValue());
  Logger.log(sheet.getRange("A2").getNumberFormat());

  var values = sheet.getRange("A4:C4").getValues();
  Logger.log(values[0][0] + " " + values[0][1] + " " + values[0][2]);
  sheet.getRange("E4").setValue(newTime2(values[0][0], values[0][1], values[0][2])).setNumberFormat("HH:mm:ss");
}

and instead of a newTime function, I created a newTime2 with this code:

function newTime2(hours, minutes, seconds) {
  var date = new Date();
  date.setHours(hours);
  date.setMinutes(minutes);
  date.setSeconds(seconds);
  Logger.log(date);
  return date;
}

I hope that helps.

See this Post on how to get the correct value, there is a correction factor because Spreadsheet time and Java Script time are not the same they have different starting points:

GAS: How to read the correct time values form Google Spreadsheet

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