将参数从Dialog传递到服务器端脚本

时间:2019-01-23 15:19:10

标签: google-apps-script

我有一个Google表格模板。每次打开该对话框时,我都想打开一个对话框以获取两个参数,并将这些参数传递给绑定到Google工作表模板的脚本。到目前为止,我已经成功定义并打开了对话框。但是参数不会显示在服务器端脚本中。

当我按下“创建”按钮时,似乎未调用readFormData。如果将readFormData替换为google.script.host.close(),则对话框将关闭。但是不支持readFormData。 close()也有同样的问题。因此,我认为Java脚本无法执行。

编辑:我已经通过解决方法解决了这个问题。我将onclick =“ readFormData”替换为onclick =“ google.script.run.withSuccessHandler(google.script.host.close).getFormData(this.parentNode)”,然后一切正常。 (也需要在GS端进行一些更改)但是,我无法弄清楚为什么我不能调用自己的JavaScript过程readFormData()。使用帮助表单Chrome开发者工具,我可以注意到未定义readFormData,并引发了一个异常“未捕获的ReferenceError:未定义readFormData”。每次我单击按钮时都会触发。因此,我想这肯定是语法错误,愚弄了解析器或类似工具。

GS:

function getFormData(obj) {

  Logger.log(obj);
  return "hello";
}


function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('index');
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
     .showModalDialog(html, 'Create file');
}


function onOpen(e) {

   openDialog(); 
}

HTML:

        <!DOCTYPE html>
        <html>
           <head>
              <base target="_top">
              <link rel="stylesheet" 
                  href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
           </head>
           <body>

           <form id="getSomeData">
             <div class="inline form-group">
               <label for="fname">Destination</label>
               <input type="text" name="fname" style="width: 150px;">
             </div>

             <div class="inline form-group">
                <label for="date">Date</label>
                <input type="text" name="date" style="width: 40px;">
             </div>

             <div>
                <input type="button" class = "action" value= "Create" onclick="readFormData()" >     
             </div>

           </form>

   <!--  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> -->

    <script>

       function success(msg) {        // I have fixed the ((msg) error
         alert(msg);
       }


       function readFormData(){
          console.log("readFormData");
          var form = document.getElementById("getSomeData").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(close).getFormData(obj);
       }

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

1 个答案:

答案 0 :(得分:1)

在我看来,“成功”功能上有误印,可能导致解释问题:

  

成功( msg)。

您尝试不使用此“(”吗?

相关问题