Google表格脚本中的Javascript:使用setNumberFormat的帮助 [英] Javascript in Google Sheets script: help using setNumberFormat

查看:81
本文介绍了Google表格脚本中的Javascript:使用setNumberFormat的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这对您来说是一个简单的问题。我完全没有编码知识。但是一直在Google表格中使用以下脚本从另一张表格中获取更改的数据并每天记录下来,并随时添加。不记得我在哪里找到该脚本了-如果我这样做了,我会回去问它的创建者。一切正常。唯一的事情是我必须每天手动复制粘贴我喜欢的日期格式。因此,我希望脚本以 dd / MM / yyyy格式打印日期(同时在单元格中保留小时和分钟数据)。我一直在网上阅读和搜索,并尝试了一些年龄,但无法弄清楚。这是基本代码:

Hoping this is a simple problem for you lot. I have no coding knowledge at all. But been using the below script in a Google Sheet to grab changing data from another sheet and log it daily, appending as it goes. Can't remember where I found the script - if I did I'd go back and ask its creator. It's been working fine; only thing is I have to manually copy paste my preferred date format every day. So I'd like the script to print the date in "dd/MM/yyyy" format (while retaining hours and minutes data inside the cell). I've been reading and searching online, and experimenting for ages but can't figure it out. This is the base code:

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("History");
  var source = sheet.getRange("A2:C2");
  var values = source.getValues();
  values[0][0] = new Date();
  sheet.appendRow(values[0]);
};

我尝试将 setnumberformat 放入位置,几乎总是会出错。也许是受其他示例启发,我的最佳尝试是添加以下新行:

I've tried placing setnumberformat in various places and nearly always get an error. Perhaps my best attempt, inspired by other examples I've seen, was to add these new lines:

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("History");
  var source = sheet.getRange("A2:C2");
  var values = source.getValues();
  values[0][0] = new Date();
  sheet.appendRow(values[0]);
  var cell = SpreadsheetApp.getActiveSheet().getRange(2, 1, 979);
  cell.setNumberFormat("dd/MM/yyyy");
};

我希望这会在附加新数据后格式化整个日期行(行A)。即使可行,这可能也是一个笨拙的解决方案,但没有成功。到目前为止,这是我唯一的尝试,不会返回错误!所以,是吗?但这不会更改日期格式。所以我放弃了。有任何想法吗?

I hoped this would format the entire date row (Row A) after appending the new data. Probably a clunky solution even if it worked, but it didn't. It's my only attempt so far that doesn't return an error! So, yay? But it doesn't change the date format. So I give up. Any ideas?

推荐答案

指定特定单元格的格式

var cell = sheet.getRange("A2");
cell.setNumberFormat("dd/MM/yyyy");

指定单元格范围的格式

var cells = sheet.getRange("A2:C2");
cells.setNumberFormat("dd/MM/yyyy");

请参阅范围格式

这篇关于Google表格脚本中的Javascript:使用setNumberFormat的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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