Reputation: 941
If you run the codes below and your spreadsheet has a date column, the date format looks like this on the client page: Thu May 23 2013 00:00:00 GMT-0400 (EDT)
I want to format the date to something like this: May 23, 2013
I figure you could do that by changing the line
<td><?= data[i][j] ?></td>
to
<td><?= Utilities.formatDate(new Date(data[i][j]), "GMT-5", "MMM dd, yyyy")data[i][j] ?></td>
...but the problem is that not every data in the array is a date object. How do I check if an object in an array is of a certain type? I would like to determine if the current object is a date object before applying the formatDate function.
Code.gs
function doGet() {
return HtmlService
.createTemplateFromFile('index')
.evaluate();
}
function getData() {
return SpreadsheetApp
.openById('123abc')
.getDataRange()
.getValues();
}
index.html
<? var data = getData(); ?>
<table>
<? for (var i = 0; i < data.length; i++) { ?>
<tr>
<? for (var j = 0; j < data[i].length; j++) { ?>
<td><?= data[i][j] ?></td>
<? } ?>
</tr>
<? } ?>
</table>
Upvotes: 0
Views: 91
Reputation: 45710
There are many questions and answers here about getting an object's type in javascript. Make sure you read them all!
Here's a simple change that matches the style of your existing code. Put the formatter function in Code.gs
, and have the templated html utilize it on each 'cell' as the table is built.
... everything you have plus this:
function formatIfDate(obj) {
if (obj.constructor.name == 'Date')
return Utilities.formatDate(new Date(obj), "GMT-5", "MMM dd, yyyy")
else
return obj
};
Change
<td><?= data[i][j] ?></td>
to
<td><?= formatIfDate(data[i][j]) ?></td>
Upvotes: 1