繁体   English   中英

按钮onclick,执行JS函数然后GS代码

[英]Button onclick, executing JS funtion then GS code

抱歉英语错误,但我不是英语。

很久以前从这个示例表单开始上传带有发送电子邮件地址的文件,我已将按钮代码更改为:

  <input type="button" value="Submit"
      onclick="google.script.run
          .withSuccessHandler(updateUrl)
          .withFailureHandler(onFailure)
          .uploadFiles(this.parentNode)" />

到:

<input type="button" value="Submit"
      onclick="test()" />

完整的 HTML 页面是:

<form id="myForm">
  <input type="text" name="myName" placeholder="Your full name..."/>
  <input name="myFile" type="file" />
  <input type="button" value="Submit"
      onclick="test()" />
</form>

<div id="output"></div>

<script>
    function test(){
    console.log ("I'm running");
    
    google.script.run
          .withSuccessHandler(updateUrl)
          .withFailureHandler(onFailure)
          .uploadFiles(this.parentNode)
          
    }

    function updateUrl(url) {
        var div = document.getElementById('output');
        div.innerHTML = '<a href="' + url + '">Got it!</a>';
    }
    function onFailure(error) {
        alert(error.message);
    }
</script>

<style>
  input { display:block; margin: 20px; }
</style>

它工作了一年,几天前它停止了。 如果我打开控制台,我会正确看到“我正在运行消息”,但电子表格或文件夹中没有任何反应。

有人能告诉我为什么吗? 我需要在执行 GS 代码之前运行一些 JS 代码,它可以工作但现在不行,有什么变化?

谢谢。

当您将google.script.run.myFunction()代码从按钮移动到<script>标记时,它会导致this关键字引用不同的内容。 因此, this.parentNode不再引用<script>标记中带有google.script.run.myFunction()代码的表单元素。 您需要获取表单元素

var theForm = document.getElementById("myForm");

然后使用:

google.script.run.myFunction(theForm).

或者:

而不是使用var theForm = document.getElementById("myForm");

您可以通过以下方式传递表单元素:

<input type="button" value="Submit" onclick="test(this.parentNode)" />

和:

function test(theForm){

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM