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. :)
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.
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 ...
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.
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.
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.
For the function spreadsheetTest()
, modify as follows.
return data;
To :
return JSON.stringify(data);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With