【问题标题】:HTML form is not running a (withSuccessHandler)functionHTML 表单没有运行 (withSuccessHandler) 函数
【发布时间】:2018-11-25 05:45:03
【问题描述】:

我在 Google App Script(Sheets) 中有这个 HTML 表单,它要求用户输入日期值,然后提交该值。 HTML 表单运行。唯一的问题是 obj 不记录。我不明白为什么会这样。

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
   <form id="myForm">

    <input type="date" name="startDate" value="" id="demo" >
    <input type="button" style="font-family: verdana;" value="Submit" onclick="getStartDate()">

   </form> 
    <script>
  function success(msg) {
    alert(msg);
  }

  function getStartDate(){
    var form = document.getElementById("myForm").elements;
    var obj ={};
    for(var i = 0 ; i < form.length ; i++){
        var item = form.item(i);
        obj[item.name] = item.value;
    }
    google.script.run
    .withSuccessHandler(success)
    .getStartDate(obj);

    google.script.host.close();
  };
  </script>
  </body>
</html>

Javascript:

function getStartDate(obj){
  Logger.log(obj)
}

这应该输出对象,但它没有。

感谢您的帮助!

【问题讨论】:

    标签: html google-apps-script


    【解决方案1】:

    这个修改怎么样?

    修改点:

    • google.script.run 通过异步处理工作。所以在你的脚本中,getStartDate()success() 由于google.script.host.close() 而没有运行。
    • GAS 端的getStartDate() 不返回值。所以alert(msg) 显示undefined

    当以上几点反映到你的脚本中时,它变成如下。

    修改脚本:

    HTML:

    请按如下方式修改Javascript。

    function success(msg) {
      alert(JSON.stringify(msg)); // Modified
    }
    
    function getStartDate(){
      var form = document.getElementById("myForm").elements;
      var obj ={};
      for(var i = 0 ; i < form.length ; i++){
        var item = form.item(i);
        obj[item.name] = item.value;
      }
      google.script.run
      .withSuccessHandler(function(e) { // Modified
        success(e);
        google.script.host.close();
      })
      .getStartDate(obj);
    };
    
    气体:
    function getStartDate(obj){
      Logger.log(obj)
      return obj; // Modified
    }
    

    注意:

    • 如果您想将密钥交给Submit,请将name添加到&lt;input type="button" style="font-family: verdana;" value="Submit" onclick="getStartDate()"&gt;
    • 当然,你也可以将google.script.host.close()移动到success()

    参考:

    如果这不是你想要的,请告诉我。我想修改它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多