简体   繁体   English

转换不带时区的日期格式 - Google 应用程序脚本 (Javascript)、Google 电子表格

[英]Convert date format without timezone - Google app script (Javascript), Google spreadsheet

I'm working on integration between Google spreadsheet and LINE Message API (BOT) where Google app script is back-end.我正在研究 Google 电子表格和 LINE Message API (BOT) 之间的集成,其中 Google 应用程序脚本是后端。

I get the date-format cell from Google spreadsheet and send to LINE bot but the message reply showed different thing.我从 Google 电子表格中获取日期格式的单元格并发送到 LINE bot,但消息回复显示不同的内容。 in a cell of Google SpreadsheetGoogle 电子表格的单元格中

1/5/2020 1/5/2020

In Google app script, I first coded it在 Google 应用程序脚本中,我首先对其进行了编码

var colb =  ss.getSheets()[0].getRange(i+3, 2).getValue();

but LINE message, it sends format included timezone as default但是 LINE 消息,它默认发送包含时区的格式

Sun Jan 05 2020 00:00:00 GMT+0700 (ICT) 2020 年 1 月 5 日星期日 00:00:00 GMT+0700 (ICT)

So I coded it所以我编码了

var colb =  Utilities.formatDate(ss.getSheets()[0].getRange(i+3, 2).getValue(), ss.getSpreadsheetTimeZone(), "dd/MM/YY");

This one is works but it's not working when cell is empty which is I have no clue why.这是有效的,但是当单元格为空时它不起作用,我不知道为什么。 So someone please help me with this.所以请有人帮我解决这个问题。 Thank in advance预先感谢

If cell is empty and you want to use Utilities.formatDate I would do like this:如果单元格为空并且您想使用 Utilities.formatDate 我会这样做:

var colb =  " " || Utilities.formatDate(ss.getSheets()[0].getRange(i+3, 2).getValue(), ss.getSpreadsheetTimeZone(), "dd/MM/YY");

So in case there is no value in cell just assign default value " "因此,如果单元格中没有值,只需分配默认值“”

If you need colb to contain the original value (including empty) when the date conversion doesn't work, you can do this:如果您需要colb在日期转换不起作用时包含原始值(包括空值),您可以这样做:

var value = ss.getSheets()[0].getRange(i+3, 2).getValue();
var colb = (typeof value == "object" && value instanceof Date) ? Utilities.formatDate(value, ss.getSpreadsheetTimeZone(), "dd/MM/YY") : value;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM