HTML
<html>
<table>
<tr>
<select id="Ultra" onchange="getForm()">
<option value="0.0">1</option>
<option value="1.0">2</option>
<option value="2.0">3</option>
<option value="3.0">4</option>
</select>
</tr>
<tr>
<td id="demo">
<script>
function getForm()
{
google.script.run.withSuccessHandler(myFunction).myFuncti();
}
function myFunction(arry)
{
var x = document.getElementById("Ultra").value;
for(i=0;i<arry.length;++i)
{
if(arry[i][0] == x)
{
var a = arry[i][1];
}
document.getElementById("demo").innerHTML = a;
}
}
</script>
</td>
</tr>
</table>
</html>
我使用 select 创建了一个下拉菜单,之后我调用了客户端函数 getForm()
。在该函数中,我使用 google.script.run
类调用服务器端函数 myFuncti()
,这将返回一个我传递给客户端函数的数组 myfunction()
作为参数。
如果使用Logger.log()
,服务器端函数成功返回数组值!!
这是一个二维数组..我是否以正确的方式将其传递给 myFunction()
?
如果您无法理解问题中的任何内容,请添加评论,我会回复
最佳答案
这就是代码需要的样子,请注意使用 console.log() 在客户端进行调试(CTRL-SHFT-I 通常在浏览器中打开开发人员控制台窗口):
index.html
<div>
<select id="Ultra" onchange="getForm()">
<option value="1.0">1</option>
<option value="2.0">2</option>
<option value="3.0">3</option>
<option value="4.0">4</option>
</select>
<p id="demo"></p>
</div>
<script>
function getForm() {
google.script.run.withSuccessHandler(myFunction).myFuncti();
}
function myFunction(arry) {
var x = document.getElementById("Ultra").value;
var y = parseInt(x, 10);
var i = 0;
var a;
console.log('x (string) = ' + x);
console.log('y (number) = ' + y);
console.log(arry);
for (; i < arry.length; i++) {
a = arry[i];
console.log('a = ' + a);
if (a === y) {
console.log('a === y');
document.getElementById("demo").innerHTML = a;
}
}
}
</script>
代码.gs
function doGet(form) {
return HtmlService
.createHtmlOutputFromFile('index');
}
function myFuncti() {
return [1, 2, 3, 4];
}
这是a working demo ,和the script .
关于javascript - 使用 Google Apps 脚本将数组传递给客户端函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28838768/