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

不问归期 提交于 2020-01-26 03:56:45

问题


I'm working on integration between Google spreadsheet and LINE Message API (BOT) where Google app script is back-end.

I get the date-format cell from Google spreadsheet and send to LINE bot but the message reply showed different thing. in a cell of Google Spreadsheet

1/5/2020

In Google app script, I first coded it

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

but LINE message, it sends format included timezone as default

Sun Jan 05 2020 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


回答1:


If you need colb to contain the original value (including empty) when the date conversion doesn't work, you can do this:

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;



回答2:


If cell is empty and you want to use Utilities.formatDate I would do like this:

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 " "



来源:https://stackoverflow.com/questions/59750523/convert-date-format-without-timezone-google-app-script-javascript-google-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!