how to change timezone in googlesheet?

一个人想着一个人 提交于 2020-04-07 06:38:33

问题


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, "");
}

回答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:

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
}


来源:https://stackoverflow.com/questions/60317489/how-to-change-timezone-in-googlesheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!