HTML表单-以编程方式选中复选框

时间:2019-06-30 03:30:59

标签: html forms google-apps-script web-applications

我正在使用Google Apps脚本开发Google加载项,并且尝试制作HTML表单作为侧边栏,其代码在此处:

<body onload="google.script.run.onSettingsOpen()">

<form id="baseSettings" onsubmit="event.preventDefault(); google.script.run.processForm(this); google.script.host.close()">


  <h2>What settings would you like to be on? Please select all that apply.</h2>
  <br>

  <input type="checkbox" name="checks" value="spaces" id="spaces">Double spaces
  <br>

  <input type="checkbox" name="checks" value="punctuation" id="punctuation">Punctuation outside of quotes
  <br>

  <input type="checkbox" name="checks" value="caps" id="caps">Capitilazation at beginning of sentences
  <br>

  <input type="checkbox" name="checks" value="contractions" id="contractions">Contractions
  <br>

  <input type="checkbox" name="checks" value="numbers" id="numbers">Small numbers
  <br>

  <input type="checkbox" name="checks" value="repWords" id="repWords">Repeated words
  <br>
  <br>

  <input type="submit" value="Submit">

</form>

</body>

我正在尝试使用“用户属性”来跟踪应为此表格选中哪些框(这是一个设置面板)。所有用户属性均以true开头,并且只能为true或false-不能有其他值。我编写了以下代码来获取复选框,以反映用户先前针对其偏好选择的内容:

function onSettingsOpen()
{

  populateData ();
  initializePreferences();

  document.getElementById(propsList[1]).checked = (allPreferences.getProperty(propsList[1]) === "true");
  document.getElementById(propsList[2]).checked = (allPreferences.getProperty(propsList[2]) === "true");
  document.getElementById(propsList[3]).checked = (allPreferences.getProperty(propsList[3]) === "true");
  document.getElementById(propsList[4]).checked = (allPreferences.getProperty(propsList[4]) === "true");
  document.getElementById(propsList[5]).checked = (allPreferences.getProperty(propsList[5]) === "true");

}

propsList数组如下:

var propsList = ["spaces", "punctuation", "caps", "contractions", "numbers", "repWords"];

这是processForm函数:

function processForm(formObject)
{
  var ui = DocumentApp.getUi();
  var results = JSON.stringify(formObject.checks);
  for (var i = 0; i < 6; i++)
  {
    if (allPreferences.getProperty(allPros[i]) === "false" && results.includes(allProps[i]))
    {
      allPreferences.setProperty(allProps[i], true)
    }
    else if (allPreferences.getProperty(allPros[i]) === "true" && !results.includes(allProps[i]))
    {
      allPreferences.setProperty(allProps[i], false)
    }
  }

}

这是populateData()函数:

function populateData ()
{
  if (doc == undefined)
  {
    doc = DocumentApp.getActiveDocument();
    bod = doc.getBody();
    docTxt = bod.getText();
    toEdit = bod.editAsText();
    paragraphs = bod.getParagraphs();
    ui = DocumentApp.getUi();
    allPreferences = PropertiesService.getUserProperties();
  }
}

我当前的问题是我收到一条错误消息,内容为:“执行失败:ReferenceError:未定义“文档”。我不确定该怎么办。我也曾在JQuery中尝试过,但无济于事。我要做的就是能够访问表单的“ checked”属性并以编程方式对其进行编辑。任何帮助表示感谢,谢谢

0 个答案:

没有答案