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