[英]When I try to fetch data from Google Sheets, if this data contains a date or time, it is not fetched into the Google app script
I searched for this info in many websites, but unfortunately I did not find it.我在很多网站上搜索过这个信息,但不幸的是我没有找到。 I used an app script from Google to fetch data from Google Sheet The code I used to get data from google sheet;我使用 Google 的应用程序脚本从 Google Sheet 获取数据我用来从 Google Sheet 获取数据的代码; Can't pass any data contains date or time, such as 2021/01/01
or 12:00 AM
or 12:00:00
but when removing date or time fetching data does work fine无法传递任何包含日期或时间的数据,例如2021/01/01
或12:00 AM
或12:00:00
但删除日期或时间时获取数据确实可以正常工作
Technologies used: google app script, google sheet使用的技术:谷歌应用程序脚本,谷歌表
The question is: How to fetch data, regardless of its type?问题是:如何获取数据,而不管其类型?
Code.gs It works as it should Code.gs正常工作
var ss1 = "1bqKXcD2uXUIegqsb4pwieuhwe29-3872_Kj50kfvvXU";
function getLicenseExpirationData(){
//Get spread sheet with id = to var ss1
var ss = SpreadsheetApp.openById(ss1);
//Get spread sheet named Reports
var ws = ss.getSheetByName("Reports");
//Get data from a spread sheet
var LicenseExpirationData = ws.getRange(2, 1,ws.getLastRow()-1,9).getValues();
//After geting all data return it so we can use it
return LicenseExpirationData;
};
TableScript.html I guess the problem is here TableScript.html我猜问题出在这里
document.addEventListener("DOMContentLoaded",function(){
//Get all result from google sheet and if you did get it display it or do display error
google.script.run.withSuccessHandler(generateExpirationTable).getLicenseExpirationData();
});
//function name generateTable
function generateExpirationTable(dataArray){
//Display result inside in id with name ExpirationData
var tbody = document.getElementById("ExpirationData");
//Do this for each result
dataArray.forEach(function(r){
//Create Element <tr>
var row = document.createElement("tr");
//Create Element <td> for column1
var column1 = document.createElement("td");
//Display result1 inside <td>
column1.textContent = r[0];
//Create Element <td> for column2
var column2 = document.createElement("td");
//Display result2 inside <td>
column2.textContent = r[1];
//Create Element <td> for column3
var column3 = document.createElement("td");
//Display result3 inside <td>
column3.textContent = r[2];
//Create Element <td> for column3
var column4 = document.createElement("td");
//Display result4 inside <td>
column4.textContent = r[3];
//Create Element <td> for column3
var column5 = document.createElement("td");
//Display result5 inside <td>
column5.textContent = r[4];
//Create Element <td> for column3
var column6 = document.createElement("td");
//Display result6 inside <td>
column6.textContent = r[5];
//Create Element <td> for column3
var column7 = document.createElement("td");
//Display result7 inside <td>
column7.textContent = r[6];
//Create Element <td> for column3
var column8 = document.createElement("td");
//Display result8 inside <td>
column8.textContent = r[7];
//Create Element <td> for column3
var column9 = document.createElement("td");
//Display result9 inside <td>
column9.textContent = r[8];
//Create Element <td> for column3
var column10 = document.createElement("td");
//Display result10 inside <td>
column10.textContent = r[9];
//Move to next column
row.appendChild(column1);
//Move to next column
row.appendChild(column2);
//Move to next column
row.appendChild(column3);
//Move to next column
row.appendChild(column4);
//Move to next column
row.appendChild(column5);
//Move to next column
row.appendChild(column6);
//Move to next column
row.appendChild(column7);
//Move to next column
row.appendChild(column8);
//Move to next column
row.appendChild(column9);
//Move to next row
row.appendChild(column10);
//Move to next row
tbody.appendChild(row);
});
};
function showError() {
alert('Error!');
}
View items as Html It works as it should以 Html 格式查看项目它应该可以正常工作
<table class="mt-0 pt-0 table">
<thead>
<tr>
<th>Date</th>
<th>Name</th>
<th>Num</th>
<th>Data2</th>
<th>Data3</th>
<th>Data4</th>
<th>Data5</th>
<th>Data6</th>
<th>Data7</th>
<tr>
</thead>
<!--Display table result on id body-table-->
<tbody id="ExpirationData">
</tbody>
</table>
Two things:两件事情:
When retrieving date object from Google Sheets it is best to use getDisplayValues() instead of getValues()
从 Google Sheets 检索日期对象时,最好使用getDisplayValues()而不是getValues()
When passing arrays from code.gs to clientside - pass it as a string, that is: JSON.stringify()
the array on Apps Script side before returning it a JSON.parse()
it again on Javascript side将数组从 code.gs 传递到客户端时 - 将其作为字符串传递,即: JSON.stringify()
Apps Script 端的数组,然后在 Javascript 端再次返回JSON.parse()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.