Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

Any help would be highly appreciated. :)

like image 254
Himal Samarasekara Avatar asked May 21 '18 23:05

Himal Samarasekara


People also ask

How does Apps Script handle data stored in Google Sheets?

Google Sheets stores data in different formats depending on the nature of the data. When these values are used in custom functions, Apps Script treats them as the appropriate data type in JavaScript.

How do I deploy a script as a web app?

Deploy a script as a web app 1 At the top right of the script project, click Deploy > New deployment. 2 Next to "Select type," click Enable deployment types settings > Web app. 3 Enter the information about your web app in the fields under "Deployment configuration." 4 Click Deploy. More ...

Who can execute the script in the web app?

The permissions for a web app differ depending how you choose to execute the app: Execute the app as me—In this case, the script always executes as you, the owner of the script, no matter who accesses the web app. Execute the app as user accessing the web app—In this case, the script runs under the identity of the active user using the web app.

How do I test a web app in devand?

Next to "Select type," click Enable deployment types settings> Web app. Under the web app URL, click Copy. Paste the URL in your browser and test your web app. This URL ends in /devand can only be accessed by users who have edit access to the script.


1 Answers

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.

like image 157
Tanaike Avatar answered Sep 25 '22 08:09

Tanaike