小编sac*_*tyr的帖子

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

我对Google Apps脚本很陌生,所以请耐心等待.

我正在从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]);

};
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 尽管我已将日期格式指定为"yyyy-MM-dd",但我的新行中A列中的日期是以"M/dd/yyyy"格式创建的.

我尝试使用Google表格中的下拉格式菜单将整个列A预格式化为"yyyy-MM-dd",但如果我运行上面的代码,我的新行仍然是"M/dd/yyyy".

好像我的代码完全忽略了Utilities.formatDate.仅供我从这里获得上述代码.

format date google-sheets google-apps-script

8
推荐指数
1
解决办法
3万
查看次数

标签 统计

date ×1

format ×1

google-apps-script ×1

google-sheets ×1