如何更改googlesheet中的时区? [英] how to change timezone in googlesheet?

查看:137
本文介绍了如何更改googlesheet中的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已为以下项目复制了一个脚本: 当您访问网址时,网址中包含值: 我的googlesheet自动填充单元格中URL中隐藏的值并在同一行:日期和时间. 一切正常,除了时间不是我所在时区的时间.

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

推荐答案

有两种方法可以更改电子表格中的时区.第一个是通过UI,第二个是通过编程.

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.

转到 文件->电子表格设置 ,将弹出一个弹出窗口,如下图所示:

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 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
}

这篇关于如何更改googlesheet中的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆