Thomas Perrin
Thomas Perrin

Reputation: 784

Different date value retrieved from google sheet in Apps Script V8 and Legacy

I have this very simple 4 lines of code to retrieve a duration value in google sheet from Google Apps Script. The sheet is blank and has this only value for this test.

google sheet - one cell value only

function myFunction() {
  var my_ss = SpreadsheetApp.getActiveSpreadsheet();
  var my_sh = my_ss.getSheetByName("test");
  var my_value = my_sh.getRange("A1").getValue();
  console.log(my_value);
}

When I run this in the new runtime V8, I got the good duration of 5 minutes (with bad GMT but I don't use this). But with the current bug on V8 where we can't use the console to develop arrays and object, I downgraded the script to Apps script Legacy, and when I execute the exact same code (I mean I don't do anything but downgrading the environment with the Run menu), then I got the good GMT but the wrong duration! I got 55 minutes and 39 seconds instead of 5 minutes! I checked the time zone in apps script V8, apps script legacy and google sheet, it's the same every where, "GMT+01:00 Paris".

Even stranger: this test is actualy performed from a copy of an original file created on august 2019. In the original file I got the opposite behavior. I got the good value with apps script legacy, but with runtime V8 I got a different value (I got 23:14:21 instead of 00:05:00).

Does anyone else encounter this behaviors? Do you know how to run apps script legacy on new files without these problems? Thanks.

google sheet google apps script V8 vs Legacy

Upvotes: 2

Views: 209

Answers (2)

Thomas Perrin
Thomas Perrin

Reputation: 784

So, I noticed that we had this issue only in the case we upgrade to V8 on a file that has was created before V8 was published (and vice versa). So to avoid this, if I have an old file where I want to upgrade apps scripts to V8, I copy the file first, and in the new file (so a recent one when V8 already exists), then I don't encounter the problem...

Upvotes: 1

Marios
Marios

Reputation: 27350

As a workaround you can use getDisplayValue() instead :

var my_value = my_sh.getRange("A1").getDisplayValue();

and then, based on this value, you can construct a date object of your preference.

Upvotes: 1

Related Questions