I am trying to return an array from my server side function getPrepList(). When accessing from the client side 'prepList[]' returns 'undefined'. This is just a snippet of the full code.gs file.
//< code.gs >//
function getPrepList(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var doPrepRange = sheet.getRange('F:F');
var itemNameRange = sheet.getRange('A:A');
var prepList = new Array();
for(var i = 1; i < getFirstEmptyRow(); i++){
if(doPrepRange.getCell(i,1).getValue() == true){
prepList.push(itemNameRange.getCell(i, 1).getValue());
};
};
Logger.log(prepList);
return(prepList);
};
function doGet(){
return HtmlService.createHtmlOutputFromFile('index');
};
// < index.html > //
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1><div align="center">PREP LIST MAKER v 2.0</h1></div>
<button align="center" onClick="createCountSheet()">Nightly Count Sheet</button><br />
<button align="center" onClick="showPrepList()">Get Prep List</button><br />
<script>
console.info("before execution");
function createCountSheet(){
google.script.run
.generateCountSheet();
};
google.script.run
.withSuccessHandler(showPrepList)
.getPrepList();
function showPrepList(prepList){
console.log(prepList);
};
</script>
</body>
</html>
return JSON.stringify(myData);....myData = JSON.parse(receivedInput);Also note that your server code is horrendously slow due to repeated use of the Spreadsheet Service (you callgetValueandgetCellin a loop when you could just work with an in-memory array).