首页 > 解决方案 > Google Apps 脚本 HTML 服务功能未运行 onclick

问题描述

我在 Google Apps 脚本 HTML 服务页面上有一个表单。当我单击提交按钮时,什么也没有发生。

按钮上的代码是

<input type="submit" value="Submit" class="submit" 
    onclick="google.script.run
                       .withSuccessHandler(google.script.host.close)
                       .updateText(this.parentNode)"/>
<button class="button cancel" onclick="google.script.host.close()">Cancel</button>

提交按钮的代码是从另一个项目中提取的,它运行良好。取消按钮的代码工作正常。

目前,当我单击提交按钮时没有任何反应(窗口未关闭且updateText()功能未运行。

updateText()如果我在脚本编辑器中手动调用它,运行良好(它只是将一个值放在一个固定字段中以进行调试)。

function updateText(form){
  var ss = SpreadsheetApp.openById("sheet_id");
  ss.getActiveSheet().getRange(3,3).setValue("Test"); 
}

标签: htmlgoogle-apps-script

解决方案


试试这样:

HTML:

<form>
<!-- along with the rest of your form -->
<input type="button" value="Submit" class="submit" onclick="updateText(this.parentNode);"/>
<input type="button" value="Cancel" onclick="google.script.host.close()" />
</form>
<script>
  function updateTest(obj) {
  google.script.run
.withSuccessHandler(function(){google.script.host.close();})
.updateText(obj);
  }
  </script>

GS:

function updateText(form){
  var ss = SpreadsheetApp.openById("sheet_id");
  ss.getActiveSheet().getRange(3,3).setValue("Test"); 
  return;
}

推荐阅读