gpt4 book ai didi

javascript - google.script.run 返回未定义的数组

转载 作者:行者123 更新时间:2023-12-01 01:28:36 25 4
gpt4 key购买 nike

我正在尝试从服务器端函数 getPrepList() 返回一个数组。从客户端访问时,“prepList[]”返回“undefined”。这只是完整 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>

阅读:

关于javascript - google.script.run 返回未定义的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53525797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com