简体   繁体   中英

get cell value as customize date format from google sheet

I am trying to pass the value "input1" to the HTML dialog box. It seems simple enough. . . thank you for any help. Code.gs

function doGet() {
      var ss = SpreadsheetApp.openByUrl(url);//Spreadsheet url
      var ws = ss.getSheetByName("Options");
   var tmp = HtmlService.createTemplateFromFile("page");
  tmp.input1 = ws.getRange("A2").getValues();//cell valve is 01-01-2020
   return tmp.evaluate();

}

page.html

<html>
<head>
</head>
<body>
<input  value="<?!= input1;?>" type="text" >
</body>
</html>

It works perfect but and get value from cell date format is “Wed Jan 01 2020 12:30:00 GMT+0530 (India Standard Time)” but I want in different format like “01-01-2020”

You could format the date using Utilities.formatDate(date, timeZone, format)

And if you don't want to hardcode the timezone, you could use

var timeZone = Session.getScriptTimeZone();

Sample:

function doGet() {
   var ss = SpreadsheetApp.openByUrl(url);//Spreadsheet url
   var timeZone = ss.getSpreadsheetTimeZone();
   var ws = ss.getSheetByName("Options");
   var tmp = HtmlService.createTemplateFromFile("page");
   var value = ws.getRange("A2").getValue();//cell valve is 01-01-2020
   tmp.input1 = Utilities.formatDate(value, timeZone,  "dd/MM/yyyy");
   return tmp.evaluate();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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