如何从Google App脚本中的“自定义对话框”窗口获取数据?

时间:2018-10-21 09:49:58

标签: html google-apps-script

我使用如下所示的Google App脚本在Google Spreadsheet中创建了一个Custom Dialog Window

enter image description here

如何从窗口获取数据到电子表格。

这是我的HTML代码:

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

<form action="google.script.run.testing143()">
  Task Number:<br>
  <input type="text" name="taskNumber" value="">
  <br><br>
   Task Date:<br>
  <input type="text" name="taskDate" value="">
  <br><br>
   Customer Name:<br>
  <input type="text" name="customerName" value="">
  <br><br>
   Customer Site:<br>
  <input type="text" name="customerSite" value="">
  <br><br>
  <select>
  <option value="status">Status</option>
  <option value="complete">Complete</option>
  <option value="scheduled">Scheduled</option>
  <option value="reschedule">Reschedule</option>
  </select>
  <br><br>
   Status Date:<br>
  <input type="text" name="statusDAte" value="">
  <br><br>
   Location:<br>
  <input type="text" name="location" value="">
  <br><br>
   Description:<br>
  <input type="text" name="description" value="">
  <br><br>
  <input type="submit" value="Submit">
</form> 

  <script>
  function testing143(){
  Logger.log('With Success')  ///Doesn't work
  var lmnt = document.getElementByName('usrname'); //Wrong

  }
  </script>
  </body>
</html>

当我单击“提交”时,将带我进入错误400页面。 我可以创建一个将数据作为参数的函数,例如:

function getNewTask(number,date,status...) 

我可以使用document.getElement吗?如果是这样,那怎么办?

衷心感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我已经接受了您的代码,并进行了修改。

在Code.gs中

function testing143(obj) {
  Logger.log(obj);
  return "hello";
}

在HTML文件中

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

<form id="myForm">
  Task Number:<br>
  <input type="text" name="taskNumber" value="">
  <br><br>
   Task Date:<br>
  <input type="text" name="taskDate" value="">
  <br><br>
   Customer Name:<br>
  <input type="text" name="customerName" value="">
  <br><br>
   Customer Site:<br>
  <input type="text" name="customerSite" value="">
  <br><br>
  <select name="status">
  <option value="status">Status</option>
  <option value="complete">Complete</option>
  <option value="scheduled">Scheduled</option>
  <option value="reschedule">Reschedule</option>
  </select>
  <br><br>
   Status Date:<br>
  <input type="text" name="statusDate" value="">
  <br><br>
   Location:<br>
  <input type="text" name="location" value="">
  <br><br>
   Description:<br>
  <input type="text" name="description" value="">
  <br><br>
  <input type="button" value="Submit" onclick="testing143()">
</form> 

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

  function testing143(){
    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).testing143(obj);
  }
  </script>
  </body>
</html>