[英]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
我在很多網站上搜索過這個信息,但不幸的是我沒有找到。 我使用 Google 的應用程序腳本從 Google Sheet 獲取數據我用來從 Google Sheet 獲取數據的代碼; 無法傳遞任何包含日期或時間的數據,例如2021/01/01
或12:00 AM
或12:00:00
但刪除日期或時間時獲取數據確實可以正常工作
使用的技術:谷歌應用程序腳本,谷歌表
問題是:如何獲取數據,而不管其類型?
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我猜問題出在這里
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!');
}
以 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>
兩件事情:
從 Google Sheets 檢索日期對象時,最好使用getDisplayValues()而不是getValues()
將數組從 code.gs 傳遞到客戶端時 - 將其作為字符串傳遞,即: JSON.stringify()
Apps Script 端的數組,然后在 Javascript 端再次返回JSON.parse()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.