Google Apps脚本日期格式问题(Utilities.formatDate) [英] Google Apps Script date format issue (Utilities.formatDate)

查看:426
本文介绍了Google Apps脚本日期格式问题(Utilities.formatDate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Google Apps脚本很陌生,请耐心等待。



我从Google表格中的银行收取每日利率,并且使用以下代码为A5:F5中包含的费率追加新行A包含日期。




$ b

  function recordHistory(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(Interest Rates);
var source = sheet.getRange(A5:F5);
var values = source.getValues();
values [0] [0] = Utilities.formatDate(new Date(),GMT + 10:00,yyyy-MM-dd);
sheet.appendRow(values [0]);

};

我的问题是这个 - 虽然我已经指定日期格式为yyyy-MM-dd我新行中列A中的日期是以这种格式M / dd / yyyy创建的。

我曾尝试使用Google表格中的下拉格式菜单预先格式化整个列A,但是如果我运行上面的代码新行仍然在M / dd / yyyy。

这好像我的代码完全忽略了Utilities.formatDate。仅供参考,我从此处获得了上述代码。 Utilities.formatDate()实用程序格式化javascript字符串。 但是,当写入电子表格时,Google表格会解析新的一行数据,以确定数据类型和适当的格式,就像您通过用户界面输入数据一样。



由于您拥有可识别的日期字符串,因此Google表格会将其确定为日期,并将其存储为日期字符串,并应用默认日期格式。



您有两种方法可以使电子表格中的日期符合您的格式要求。


$ b


  1. 强制它被解释为一个String,即使它看起来像一个日期。


    $ b

      cell.setValue(Utilities.formatDate(new Date(),GMT + 10:00,'yyyy-MM-dd)); 
    // ^^强制字符串文字


  2. 设置单元格的日期格式。这将使单元格的作为日期,这对于计算很有用。



    日期格式遵循 SimpleDateFormat 规范。



    单元格A1包含日期
    var cell = SpreadsheetApp.getActiveSheet()。getRange(A1);
    cell.setNumberFormat('yyyy-mm-dd');



I am pretty new to Google Apps Script, so please bear with me.

I am collecting daily interest rates from a bank on Google Sheets, and I am using the following code to append new rows for the rates contained in A5:F5, with column A containing dates.

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Interest Rates");
  var source = sheet.getRange("A5:F5");
  var values = source.getValues();
  values[0][0] = Utilities.formatDate(new Date(), "GMT+10:00", "yyyy-MM-dd");
  sheet.appendRow(values[0]);

};

My issue is this - although I have specified the date format to be "yyyy-MM-dd" the dates in column A in my new rows are created in this format "M/dd/yyyy".

I have tried pre-formatting entire column A with "yyyy-MM-dd" using the drop down Format menu in Google Sheets, but if I run the code above my new row is still in "M/dd/yyyy".

It's as if my code ignores Utilities.formatDate completely. FYI I got the above code from here.

解决方案

The Utilities.formatDate() utility formats a javascript String. However, when written out to the spreadsheet, the new row of data is interpreted by Google Sheets to determine data types and appropriate formatting, just as if you'd typed it in through the user interface.

Since you've got a recognizable date string, Google Sheets decides it's a Date, stores it as such, and applies the default date format.

You've got two options for making the date in the spreadsheet meet your formatting needs.

  1. Force it to be interpreted as a String, even if it does look like a date.

    cell.setValue(Utilities.formatDate(new Date(), "GMT+10:00", "''yyyy-MM-dd"));
    //                                                           ^^ force string literal
    

  2. Set the date format for the cell. This will leave the value of the cell as a date, which is useful for calculations.

    Date formats follow the SimpleDateFormat specification.

    // Cell A1 contains a date
    var cell = SpreadsheetApp.getActiveSheet().getRange("A1");
    cell.setNumberFormat('yyyy-mm-dd');
    

这篇关于Google Apps脚本日期格式问题(Utilities.formatDate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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