javascript - 使用 Google Apps 脚本将数组传递给客户端函数?

标签 javascript html google-apps-script

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/

相关文章:

javascript - 对象未在 ionic 滑动盒中创建

javascript - 在 Google 脚本中显示进度

javascript - 是否可以更改 Angular Material 的 mdSwitch 的假状态颜色?

javascript - 文本分组算法

html - 表格单元格问题中的图像?

JavaScript 二维碰撞异常

google-apps-script - 如何从 Google Apps Script Web App 返回图像?

javascript - 在 Google 电子表格数据库中以编程方式监视编辑

javascript - 如何将现有产品作为 Rails 中的嵌套属性添加到发票中?

javascript - 在 JavaScript 中访问 table.id