Google表格 - 传递表单数据

时间:2018-01-09 14:29:14

标签: google-apps-script google-sheets google-sheets-api

我正在使用谷歌工作表,我正在创建一份文件,将通过不在办公室的员工。我有一个菜单选项来添加新的员工数据,并打开侧栏,我有一个HTML表单(Image of my project)。然后它会在下一个可用行上发布数据。

问题:复选框一直未完成,我无法弄清楚如何检索它们的真/假值。

这是我的HTML:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
  <script>
    function preventFormSubmit() {
      var forms = document.querySelectorAll('form');
      for (var i = 0; i < forms.length; i++) {
        forms[i].addEventListener('submit', function(event) {
          event.preventDefault();
        });
      }
    }
    window.addEventListener('load', preventFormSubmit);

    function submitForm(formObject) {
      google.script.run
        .submitAddEmployee(formObject);
    }
  </script>
</head>

<body>
  <form id="AddEmployee" onsubmit="submitForm(this)">
    Employee Name:<br />
    <input type="text" name="employeename" />
    <hr /><br /> Check boxes that Employee Works:<br />
    <input type="checkbox" name="Workdays" value="sunday"> Sunday<br />
    <input type="checkbox" name="Workdays" value="monday" /> Monday<br />
    <input type="checkbox" name="Workdays" value="tuesday" /> Tuesday<br />
    <input type="checkbox" name="Workdays" value="wednesday" /> Wednesday<br />
    <input type="checkbox" name="Workdays" value="thursday" /> Thursday<br />
    <input type="checkbox" name="Workdays" value="friday" /> Friday<br />
    <input type="checkbox" name="Workdays" value="saturday" /> Saturday
    <hr /><br />
    <input type="checkbox" name="ooo" value="isOOO" />OOO Employee?

    <br /><br /><input type="submit" value="Submit" />
  </form>
  <div id="output"></div>
</body>

</html>

这是我的剧本:

function submitAddEmployee(object) {
  var emName = object.employeename;
  var sunday = object.sunday;
  var monday = object.monday;
  var tuesday = object.tuesday;
  var wednesday = object.wednesday;
  var thursday = object.thursday;
  var friday = object.friday;
  var saturday = object.saturday;
  var isOOO = object.isOOO;

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
  if (sheet != null) {
    var range = sheet.getDataRange();
    var nextRow = range.getLastRow() + 1;
    sheet.getRange(nextRow, 1).setValue(emName);
    sheet.getRange(nextRow, 2).setValue(sunday);
    sheet.getRange(nextRow, 3).setValue(monday);
    sheet.getRange(nextRow, 4).setValue(tuesday);
    sheet.getRange(nextRow, 5).setValue(wednesday);
    sheet.getRange(nextRow, 6).setValue(thursday);
    sheet.getRange(nextRow, 7).setValue(friday);
    sheet.getRange(nextRow, 8).setValue(saturday);
    sheet.getRange(nextRow, 9).setValue(isOOO);
    sheet.getRange(nextRow, 10).setValue("False");
  } else {
    Browser.msgBox('Sheet "Tasks" was not found.');
  }
}

您可以给予我任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

提交的复选框值由字段名称而不是值访问。

因此,在您的情况下,您有一系列具有相同名称的字段“Workdays”,它们将作为object.Workdays访问。 最简单的解决方法是重命名您的复选框以匹配您的Apps脚本:

<input type="checkbox" name="sunday" value="sunday"> Sunday<br />