我试图从服务器端函数getPrepList()返回一个数组。从客户端访问时,“ prepList []”返回“未定义”。这只是完整的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');
};
// //
<!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>
最佳答案
如果要通过按钮调用showPrepList(prepList)
,
<button align="center" onClick="showPrepList()">Get Prep List</button><br />
prepList
将是undefined
,因为您没有将任何参数传递为prepList
。尝试,
<button align="center" onClick="showPrepListFromSheet()">Get Prep List</button><br />
<script>
console.info("before execution");
function showPrepListFromSheet(){
google.script.run
.withSuccessHandler(showPrepList)
.getPrepList();
function showPrepList(prepList){
console.log(prepList);
};
}</script>
读书:
Html Best practices
Server code Best practices
关于javascript - google.script.run返回未定义的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53525797/