简体   繁体   English

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

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

Sorry for English mistakes but I'm not english.抱歉英语错误,但我不是英语。

Starting from this example Form to upload files with sending email address long time ago I've changed the button code from:很久以前从这个示例表单开始上传带有发送电子邮件地址的文件,我已将按钮代码更改为:

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

to:到:

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

the complete HTML page was:完整的 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>

It worked for 1 year, few days ago it stopped.它工作了一年,几天前它停止了。 If I open the console I see the "I'm running message" correctly but nothing happens in the spreadsheet or in the folder.如果我打开控制台,我会正确看到“我正在运行消息”,但电子表格或文件夹中没有任何反应。

Can someone tell me why?有人能告诉我为什么吗? I nedd to run some JS code before executing GS code, it worked but not now, what is changed?我需要在执行 GS 代码之前运行一些 JS 代码,它可以工作但现在不行,有什么变化?

Thanks.谢谢。

When you moved the google.script.run.myFunction() code from the button to the <script> tag, it causes the this keyword to refer to something different.当您将google.script.run.myFunction()代码从按钮移动到<script>标记时,它会导致this关键字引用不同的内容。 So, this.parentNode is no longer referring to the form element with the google.script.run.myFunction() code in the <script> tag.因此, this.parentNode不再引用<script>标记中带有google.script.run.myFunction()代码的表单元素。 You'll need to get the form element with您需要获取表单元素

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

and then use:然后使用:

google.script.run.myFunction(theForm).

OR:或者:

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

You can pass the form element with:您可以通过以下方式传递表单元素:

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

And:和:

function test(theForm){

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

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