Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
782 views
in Technique[技术] by (71.8m points)

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

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...