【问题标题】:Running Google Script Function in HTML JavaScript Header section在 HTML JavaScript Header 部分运行 Google Script 函数
【发布时间】:2016-05-04 01:07:37
【问题描述】:

我有一个在我的 Google 应用脚本 (code.gs) 中定义的名为 myFunction() 的函数,它可以访问来自 Google 表格的信息。我希望能够从生成 HTML 表单的 html 文件中的脚本头调用此函数。我尝试在 HTML 页面的脚本头中运行 google.script.run.myFunction() 但是它不返回任何变量,只是未定义。下面是 HTML 标头的代码 sn-p。

<script>
function getArray(){
    var equipment = google.script.run.myFunction();
    console.log(equipment);
  } 
</script>

查看 console.log 时,输出未定义。我尝试将 myFunction 的输出更改为许多其他类型的输出,当我在 GS 中使用 Logger.log 函数时,它会输出正确的变量,但它根本不会转移到 HTML 表单。有没有人有什么建议?

我这样做是因为我使用这个变量在 HTML 表单中动态创建复选框,这是我能想到的最简单的方法。

谢谢!

【问题讨论】:

    标签: javascript html google-apps-script


    【解决方案1】:

    接收返回值的唯一方法是:

    .withSuccessHandler(myHandlerFunction)
    

    完整的代码是:

    <script>
    function getArray(){
       google.script.run
         .withSuccessHandler(myHandlerFunction)
         .myFunction();
      }
    
      function myHandlerFunction(equipment) {
        console.log('equipment: ' + equipment);
      };
    </script>
    

    Apps Script Documentation - run methods

    【讨论】:

    • 谢谢!这非常有效。我没有意识到 SuccessHandler 是必需的。真的很感激!
    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 2020-04-23
    • 2015-06-07
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多