[英]Apps Script to JSON Date Time Format Problem
I have a Google Sheet where column 'A' is a date Time column.我有一个 Google 表格,其中“A”列是日期时间列。 LINK
关联
i converted this Google sheet file to JSON Data.我将此 Google 表格文件转换为 JSON 数据。 Link
关联
from this JSON data is fetching my website page in a table.从这个 JSON 数据中获取表格中的我的网站页面。 Link
关联
The Problem with date is it's showing 17/01/2023 19:47:35 To 2023-01-17T14:17:35.270Z How can I make this Readable日期的问题是它显示17/01/2023 19:47:35到2023-01-17T14:17:35.270Z我怎样才能使它可读
My Apps Script Code is below我的 Apps 脚本代码如下
function doGet(e) {
var sheet = SpreadsheetApp.getActive();
var nse = sheet.getSheetByName("Sheet1");
var data = [];
var rlen = nse.getLastRow();
var clen = nse.getLastColumn();
var rows = nse.getRange (1,1, rlen, clen).getValues();
for(var i = 1; i < rows.length; i++) {
var datarow = rows[i];
var record = {};
for(var j=0; j < clen ; j++){
record[rows[0][j]] = datarow[j];
}
data.push(record);
}
var result = JSON.stringify(data);
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}
and My Javascript code is here.我的 Javascript 代码在这里。
<section>
<h1>My Google Sheet Table</h1>
<!-- TABLE CONSTRUCTION-->
<table id='mytable'>
<!-- HEADING FORMATION -->
<tr>
<th>Date</th>
<th>Name</th>
<th>Phone Number</th>
<th>Months</th>
</tr>
<script>
$(document).ready(function () {
// FETCHING DATA FROM JSON FILE
$.getJSON("JsonDataLink",
function (data) {
var content = '';
// ITERATING THROUGH OBJECTS
$.each(data, function (key, value) {
//CONSTRUCTION OF ROWS HAVING
// DATA FROM JSON OBJECT
content += '<tr>';
content += '<td>' +
value.DATE + '</td>';
content += '<td>' +
value.Name + '</td>';
content += '<td>' +
value.Phone + '</td>';
content += '<td>' +
value.Months + '</td>';
content += '</tr>';
});
//INSERTING ROWS INTO TABLE
$('#mytable').append(content);
});
});
</script>
</section>
</body>
</html>
When I saw your provided sample Spreadsheet, it seems that the values of the column "A" of "DATE" are the date object. In this case, how about modifying it as follows?当我看到你提供的示例电子表格时,“DATE”的“A”列的值似乎是日期object。在这种情况下,如何修改它如下?
In this pattern, only Google Apps Script is modified.在此模式中,仅修改了 Google Apps 脚本。
As an important point, in your provided sample Spreadsheet, there are 2 functions of doGet
.重要的一点是,在您提供的示例电子表格中,有
doGet
的 2 个函数。 In this case, the expected result might not be able to be obtained.在这种情况下,可能无法获得预期的结果。 So, please remove one of the 2
doGet
functions and please modify it as follows.因此,请删除 2 个
doGet
函数中的一个,并按如下方式修改它。
function doGet(e) {
var sheet = SpreadsheetApp.getActive();
var nse = sheet.getSheetByName("Sheet1");
var data = [];
var rlen = nse.getLastRow();
var clen = nse.getLastColumn();
var rows = nse.getRange(1, 1, rlen, clen).getDisplayValues(); // Modified
for (var i = 1; i < rows.length; i++) {
var datarow = rows[i];
var record = {};
for (var j = 0; j < clen; j++) {
record[rows[0][j]] = datarow[j];
}
data.push(record);
}
var result = JSON.stringify(data);
return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}
In this pattern, only Google Apps Script is modified.在此模式中,仅修改了 Google Apps 脚本。 In this modification, the date object retrieved by
getValues
is formatted.在此修改中,格式化了
getValues
检索到的日期 object。
record[rows[0][j]] = datarow[j];
record[rows[0][j]] = j == 0 ? Utilities.formatDate(datarow[j], Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm") : datarow[j];
In this pattern, only Javascript is modified.在此模式中,仅修改了 Javascript。 In this modification, moment.js is used.
在这个修改中,使用了 moment.js 。 You can see it with as
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js
of CDN.你可以通过 CDN 的
https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js
看到它。
Please modify your Javascript as follows.请修改您的Javascript如下。
content += '<td>' +
value.DATE + '</td>';
content += '<td>' +
moment(new Date(value.DATE)).format('YYYY-MM-DD HH:mm') + '</td>';
When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version.当您修改 Web Apps 的 Google Apps Script 时,请将 deployment 修改为新版本。 By this, the modified script is reflected in Web Apps.
至此,修改后的脚本反映在Web Apps中。 Please be careful about this.
请注意这一点。
You can see the detail of this in my report " Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me) ".您可以在我的报告“ 重新部署 Web 应用程序而不更改 URL 的 Web 应用程序的新 IDE(作者:我) ”中查看详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.