使用isDate()
下面的功能来完成您的要求。可以更改日期格式以满足其他需要。有关详细信息,请参阅 [ Utilities.formatDate()
][3]。
// From http://stackoverflow.com/questions/1353684
// Returns 'true' if variable d is a date object.
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}
// Test if value is a date and if so format
// otherwise, reflect input variable back as-is.
function isDate(sDate) {
if (isValidDate(sDate)) {
sDate = Utilities.formatDate(new Date(sDate), "PST", "MM/dd/yyyy");
}
return sDate;
}
在您的脚本中,您使用了该Range.getValues()
函数,它将返回给定范围内数据的二维数组。(在您的情况下,您的整个工作表。)因此,您需要遍历读取的值并在您感兴趣的任何单元格上调用此函数,然后将信息写回电子表格。在 Google 的文档和 Stackoverflow 上都有很多这样的例子。