问题
I am trying to return the data in the spreadsheet to an Apps Script published as a webApp. However, for one of my spreadsheets (link), the returned data is null
. I logged the the data before returning to make sure that it isn't null, and I can see them in the logs.
This is my code:
function doGet(){
return HtmlService.createHtmlOutputFromFile('index');
}
function spreadsheetTest() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var maxRows = sheet.getMaxRows();
var data = sheet.getSheetValues(1, 1, maxRows, 10);
Logger.log(data)
return data;
}
<html>
<button type="button" onclick="clcked();">Click Me!</button>
<div id="Message"></div>
<script>
function clcked(){
google.script.run
.withSuccessHandler(function(response) {
console.log(response);
})
.spreadsheetTest();
}
</script>
</html>
Any help would be highly appreciated. :)
回答1:
I have ever experienced the same issue before. When the values with the date format are included in data
which is retrieved by getSheetValues()
, the returned values become null. I thought that the issue might occur when the date values are parsed and/or converted, when the values are sent from GAS to Javascript. By this, null is returned. In order to avoid this, I think that there are 2 patterns for your situation. Please chose one of them for your situation.
Pattern 1
For the function spreadsheetTest()
, modify as follows.
return data;
To :
return JSON.stringify(data);
Pattern 2
For the function spreadsheetTest()
, modify as follows.
var data = sheet.getSheetValues(1, 1, maxRows, 10);
To :
var data = sheet.getRange(1, 1, maxRows, 10).getDisplayValues();
If these are not useful for your situation, I'm sorry.
来源:https://stackoverflow.com/questions/50457896/null-value-returned-to-apps-script-webapp-for-a-certain-spreadsheet-by-server-fu