按钮onclick,执行JS功能然后执行GS代码

时间:2016-05-10 12:04:19

标签: javascript google-apps-script onclick

对不起英语错误,但我不是英语。

从这个例子开始Form to upload files with sending email address很久以前我已经改变了按钮代码:

  <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>

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

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

感谢。

1 个答案:

答案 0 :(得分:1)

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

获取表单元素
var theForm = document.getElementById("myForm");

然后使用:

google.script.run.myFunction(theForm).

OR:

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

您可以使用以下内容传递表单元素:

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

function test(theForm){