Null value returned to Apps Script webapp for a certain spreadsheet by server function

匆匆过客 提交于 2020-04-13 06:11:07

问题


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.

From :
return data;
To :
return JSON.stringify(data);

Pattern 2

For the function spreadsheetTest(), modify as follows.

From :
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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!