首页 > 解决方案 > Google.Script.Run - 不以第二种形式初始化

问题描述

我正在尝试为 Google 表格编写一个容器绑定项目。这将允许我在一列中搜索匹配值,并根据响应指示记录已满,允许我填写缺失值,或者,如果该值根本不存在,则写一个新的排。

实际的 HTML 不是问题,感谢 Stackdriver Logger,我知道初始表单值被正确传递,包括将值设置为缓存,用于决定侧边栏的第二个 HTML 表单是什么。问题是,当我提交第二种形式中的任何一种时,我试图调用以将值添加到表中的函数没有初始化,即使脚本应该是相同的。

作为参考,这是<script> 初始 HTML 文件中标记的内容:

document.querySelector("#cunyID").addEventListener("submit", 
      function(e) {
        e.preventDefault();
        google.script.run.cunyIDQuery(this);
        google.script.run.newSidebar();  
      }
      );

这是<script>第二种形式之一的内容:

gpa.onchange = function setTwoNumberDecimal(event) {
        this.value = parseFloat(this.value).toFixed(2);
      };
      q1.onchange = function setWholeNumber(event) {
        this.value = parseFloat(this.value).toFixed(0);
      };
      q2.onchange = function setWholeNumber(event) {
        this.value = parseFloat(this.value).toFixed(0);
      };
      q3.onchange = function setWholeNumber(event) {
        this.value = parseFloat(this.value).toFixed(0);
      };
      document.querySelector("#noCUNYID").addEventListener("submit", 
      function(e) {
        e.preventDefault();
        google.script.run.noCUNYIDSubmit(this);
        google.script.host.close();  
      }
      );

这是来自第二级形式的第二个:

q1.onchange = function setWholeNumber(event) {
        this.value = parseFloat(this.value).toFixed(0);
      };
      q2.onchange = function setWholeNumber(event) {
        this.value = parseFloat(this.value).toFixed(0);
      };
      q3.onchange = function setWholeNumber(event) {
        this.value = parseFloat(this.value).toFixed(0);
      };
      document.querySelector("#yesCUNYID").addEventListener("submit", 
      function(e) {
        e.preventDefault();
        google.script.run.yesCUNYIDSubmit(this);
        google.script.host.close();  
      }
      );

尽管理论上 google.script.run 应该调用函数,但 Stackdriver 没有报告函数打开,并且 google.script.host.close() 运行正常。有谁知道可能会发生什么?

标签: javascriptgoogle-apps-scriptgoogle-sheets

解决方案


在 script.run 完成执行之前,脚本很可能正在关闭。

您应该使用 withSuccessHandler 回调在脚本完成时运行关闭。

    google.script.withSuccessHandler(function() {
       google.script.host.close();
    }).run.yesCUNYIDSubmit(this);

推荐阅读