简体   繁体   English

在 HTML JavaScript Header 部分中运行 Google Script 函数

[英]Running Google Script Function in HTML JavaScript Header section

I have a function that is defined in my Google App Script (code.gs) called myFunction() which accesses information from a Google Sheet.我在我的 Google App Script (code.gs) 中定义了一个名为 myFunction() 的函数,它从 Google Sheet 访问信息。 I want to be able to call this function from the script header in the html file where a HTML form is generated.我希望能够从生成 HTML 表单的 html 文件中的脚本头调用此函数。 I have tried running google.script.run.myFunction() in the script header of the HTML page however it doesn't return any variable, just undefined.我尝试在 HTML 页面的脚本标题中运行 google.script.run.myFunction() 但是它不返回任何变量,只是未定义。 A code snippet for the header of the HTML is below. HTML 标题的代码片段如下。

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

When looking at the console.log the output is undefined.查看 console.log 时,输出未定义。 I have tried changing the output of myFunction to many other types of outputs and when I use the Logger.log function in the GS, it outputs the correct variable however it simply doesn't transfer over to the HTML form.我曾尝试将 myFunction 的输出更改为许多其他类型的输出,当我在 GS 中使用 Logger.log 函数时,它会输出正确的变量,但它根本不会转移到 HTML 表单。 Does anyone have any suggestions?有没有人有什么建议?

I am doing it in this way as I am using this variable to dynamically created checkboxes in the HTML form and this is the simplest way I can think of doing this.我这样做是因为我使用这个变量在 HTML 表单中动态创建复选框,这是我能想到的最简单的方法。

Thanks!谢谢!

The only way to receive a return value is with the:接收返回值的唯一方法是:

.withSuccessHandler(myHandlerFunction)

The complete code would be:完整的代码将是:

<script>
function getArray(){
   google.script.run
     .withSuccessHandler(myHandlerFunction)
     .myFunction();
  }

  function myHandlerFunction(equipment) {
    console.log('equipment: ' + equipment);
  };
</script>

Apps Script Documentation - run methods Apps 脚本文档 - 运行方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Html 和 Javascript 与谷歌脚本 function 错误 - Html and Javascript with google script function error 当 Javascript 函数放置在 HTML 标题部分时,Javascript 事件“onclick”不呈现结果 - Javascript event 'onclick' not rendering result when the Javascript function is placed in HTML header section 从 google 应用程序脚本中的 html 文件调用 javascript 函数 - Call javascript function from html file in google apps script Google Docs应用脚本将节的标题放入标题中 - Google Docs App Script Get Put Heading of Section into Header 单击以展开html部分的javascript函数 - javascript function to expand html section on click Google Apps脚本-将来自应用脚本功能的输出返回给html文件javascript函数 - Google Apps Script - return output from apps script function back to the html file javascript function 通过JavaScript动态在标头或HTML中包含脚本元素 - Dynamically include script element in header or HTML by JavaScript 如何在标头部分中将标头功能与javascript一起使用? - How to use header function together with javascript in head section? Google Script HTML / CSS / JS代码未运行 - Google Script HTML/CSS/JS code not running 点击时没有运行javascript的部分 - Section of javascript not running on click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM