1

I have copied a script for: when you visit an url, with values ​​in the url: my googlesheet automatically fills with the values ​​hidden in the url in cell and on the same line: the date and time . Everything works except that the time is not that of my time zone.

What should I change to have the time in my time zone? Thanks

function doGet(e) { 
  Logger.log( JSON.stringify(e) );  // view parameters
  var result = 'Ok'; // assume success
  if (e.parameter == 'undefined') {
    result = 'No Parameters';
  }
  else {
    var sheet_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';       // Spreadsheet ID
    var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();     // get Active sheet
    var newRow = sheet.getLastRow() + 1;                        
    var rowData = [];
    var Curr_Date = new Date(); 
    rowData[0] = Curr_Date;                                             // Date in column A
    var Curr_Time = Utilities.formatDate(Curr_Date, "europe/paris", 'HH:mm:ss');
    rowData[1] = Curr_Time;                                             // Time in column B
    for (var param in e.parameter) {
      Logger.log('In for loop, param=' + param);
      var value = stripQuotes(e.parameter[param]);
      Logger.log(param + ':' + e.parameter[param]);
      switch (param) {
        case 'LDR': //Parameter
          rowData[2] = value; //Value in column C
          break;
        case 'Button': //Parameter
          rowData[3] = value; //Value in column D
          break;  
        default:
          result = "unsupported parameter";
      }
    }
    Logger.log(JSON.stringify(rowData));
    // Write new row below
    var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
    newRange.setValues([rowData]);
  }
  // Return result of operation
  return ContentService.createTextOutput(result);
}
/**
* Remove leading and trailing single or double quotes
*/
function stripQuotes(value) {
  return value.replace(/^["']|['"]$/g, "");
}
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Mehdi BH
  • 11
  • 1
  • Maybe try to get an UTC date and convert it to the timezone of your choice. [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Shim-Sao Feb 20 '20 at 10:35
  • When you say "What should I change to have the time in my time zone". Do you mean to change the value in ```Curr_Time``` variable, the value in ```Curr_Date``` variable or in both? – alberto vielma Feb 20 '20 at 14:59
  • I mean that the time that fills up in the cell is not that of my time zone. It is that of +6 – Mehdi BH Feb 21 '20 at 09:51

1 Answers1

1

There are two ways for changing the time zone in your Spreadsheet. The first one is through the UI and the second one is programmatically.

The first way -> From User Interface

Go to File -> Spreadsheet settings, a pop-up window will open as in the next image:

enter image description here

As you can see there's one option to change the time zone to the one you desire.

The second way -> Programmatically:

The Spreadsheet Class has the setSpreadsheetTimeZone(timezone) method, which will change the time zone in the Spreadsheet, but now programmatically. This is a little example of how using it:

function dateFormat() {
  var ss = SpreadsheetApp.openById("your-id");
  // Get the current Spreadsheet's time zone 
  var currentTimeZone = ss.getSpreadsheetTimeZone();
  Logger.log(currentTimeZone); // Check the current time zone
  // Set the new Spreadsheet's time zone
  var updatedTimeZone = ss.setSpreadsheetTimeZone("Europe/Paris");
  Logger.log(updatedTimeZone); // Check the updated time zone
}
alberto vielma
  • 2,302
  • 2
  • 8
  • 15