简体   繁体   English

Google Apps脚本日期格式问题(Utilities.formatDate)

[英]Google Apps Script date format issue (Utilities.formatDate)

I am pretty new to Google Apps Script, so please bear with me. 我对Google Apps脚本很陌生,所以请耐心等待。

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. 我正在从Google Sheets上的银行收取每日利率,我使用以下代码为A5:F5中包含的费率添加新行,其中A列包含日期。

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". 我的问题是 - 尽管我已将日期格式指定为“yyyy-MM-dd”,但我的新行中A列中的日期是以“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". 我已经尝试使用Google表格中的下拉格式菜单将整个列A预格式化为“yyyy-MM-dd”,但如果我运行上面的代码,我的新行仍然是“M / dd / yyyy”。

It's as if my code ignores Utilities.formatDate completely. 好像我的代码完全忽略了Utilities.formatDate。 FYI I got the above code from here . 仅供我从这里获得上述代码。

The Utilities.formatDate() utility formats a javascript String. Utilities.formatDate()实用程序格式化javascript字符串。 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. 但是,当写入电子表格时,Google表格会解释新的数据行,以确定数据类型和适当的格式,就像您通过用户界面输入一样。

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. 由于您有一个可识别的日期字符串,因此Google表格决定它是一个日期,将其存储,并应用默认日期格式。

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. 强制它被解释为String,即使它看起来像日期。

     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. 日期格式遵循SimpleDateFormat规范。

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

Solution : 方案:

values[0][0] = Utilities.formatDate(new Date(), 
"GMT+10:00", "yyyy-MM-dd").setNumberFormat('yyyy-mm-dd');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps脚本中的Utilities.formatDate()输出以前的日期(例如,输入25.05.2015->输出24.05.2015) - Utilities.formatDate() in Google Apps Script outputs previous date (e.g. input 25.05.2015 -> output 24.05.2015) Google App Script Utilities.formatDate(new Date()今天增加了几天 - Google App Script Utilities.formatDate(new Date() adding days to today 谷歌脚本“Utilities.formatDate”对时区参数不做任何事情 - Google Scripts "Utilities.formatDate" does nothing with the time zone parameter Utilities.formatDate() 返回月份为 0 - Utilities.formatDate() returning month as 0 使用 new Date() 和 Utilities.formatDate 进行日期操作会抛出意外结果 - Date Manipulation using new Date() & Utilities.formatDate throw Unexpected Results Google格式的日期与格式比较在Google Apps脚本中落后1天 - Date comparisons with formatDate in Google Apps Script are 1 day behind 使用Utilities.formatDate,2017年12月31日为1秒= 1年 - one second = 1 year for 12/31/2017 using Utilities.formatDate Utilities.formatDate 在第 7 天而不是第 1 天开始一​​周 - Utilities.formatDate starts the week on day 7 instead of day 1 google-apps-script格式日期 - google-apps-script Format Date 如何在Angular上使用formatDate格式化日期 - How to format a date with formatDate on angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM