简体   繁体   English

Apps 脚本到 JSON 日期时间格式问题

[英]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:352023-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。在这种情况下,如何修改它如下?

Pattern 1:模式 1:

In this pattern, only Google Apps Script is modified.在此模式中,仅修改了 Google Apps 脚本。

IMPORTANT:重要的:

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函数中的一个,并按如下方式修改它。

Modified script:修改脚本:

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);
}

Pattern 2:模式 2:

In this pattern, only Google Apps Script is modified.在此模式中,仅修改了 Google Apps 脚本。 In this modification, the date object retrieved by getValues is formatted.在此修改中,格式化了getValues检索到的日期 object。

From:从:

record[rows[0][j]] = datarow[j];

To:到:

record[rows[0][j]] = j == 0 ? Utilities.formatDate(datarow[j], Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm") : datarow[j];
  • Please modify the format for your actual situation.请根据您的实际情况修改格式。

Pattern 3:模式 3:

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如下。

From:从:

content += '<td>' + 
    value.DATE + '</td>';

To:到:

content += '<td>' + 
    moment(new Date(value.DATE)).format('YYYY-MM-DD HH:mm') + '</td>';
  • Please modify the format for your actual situation.请根据您的实际情况修改格式。

Note:笔记:

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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