Google表格应用程序脚本Html onload无法正常工作

时间:2018-01-10 21:57:34

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

我正在使用谷歌工作表,我正在创建一份文件,将通过不在办公室的员工。我有一个菜单选项来删除员工数据,它打开侧边栏,我有一个HTML表单(Image of my project)。我试图让它生成列表中当前员工的下拉列表。

我已经开发了代码来提取我需要的数据:

function removeAnyone() {
  var html = HtmlService.createHtmlOutputFromFile('RemoveAnyone');
  SpreadsheetApp.getUi()
    .showSidebar(html);
}

function getList() {
  var headerRows = 1;
  var sheet = SpreadsheetApp.getActive().getSheetByName("Data");
  var range = sheet.getRange(headerRows + 1, 1, sheet.getMaxRows() - headerRows, 1);
  var arrayValues = range.getValues();
  return arrayValues;
}

现在我们转到我的html,我只是试图在标题中使用for循环加载下拉列表:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
  <script>
    function addOption_list() {

     document.getElementById("output").innerHTML = "test";

    var options = google.script.run.getList();

    for (var i = 0; i < options.length; ++i;) {
      var optn = document.createElement("OPTION");
      optn.text = options[i];
      optn.value = options[i];
      document.myForm.selectEmployee.options.add(optn);

      }
    }
  </script>
</head>

<body onload="addOption_list()">
  <form id="myForm" onsubmit="submitForm(this)">
    <select id="selectEmployee">
      <option>Choose an employee</option>
     </select>
  </form>
  <div id="output"></div>
</body>

</html>

我在体内抛出了一个div并且在开始时将函数值改为“test”,这只是为了检查函数是否被调用,它看起来不像是。

我也尝试过使用window.onload(如下所示),但这也没有让我到任何地方。:

window.onload = function {

     document.getElementById("output").innerHTML = "test";

    var options = google.script.run.getList();

    for (var i = 0; i < options.length; ++i;) {
      var optn = document.createElement("OPTION");
      optn.text = options[i];
      optn.value = options[i];
      document.myForm.selectEmployee.options.add(optn);

      }
    }

您可以给我的任何指导都非常感谢!

2 个答案:

答案 0 :(得分:0)

google.script.run没有返回值。如果您想从GAS返回值,请使用withSuccessHandler。由getValues()检索的数据是二维数组。那么这个修改怎么样?

修改后的脚本:

<!DOCTYPE html>
<html>

<head>
  <base target="_top">
  <script>
    window.onload = function() {
      google.script.run.withSuccessHandler(addOption_list).getList();
    }
    function addOption_list(options) {
      document.getElementById("output").innerHTML = "test";
      var select = document.getElementById('selectEmployee');
      for ( var i in options) {
        var optn = document.createElement('option');
        optn.value = options[i][0];
        optn.text = options[i][0];
        select.appendChild(optn);
      }
    }
  </script>
</head>

<body>
  <form id="myForm" onsubmit="submitForm(this)">
    <select id="selectEmployee">
      <option>Choose an employee</option>
     </select>
  </form>
  <div id="output"></div>
</body>

</html>

参考:

如果我误解了你的问题,我很抱歉。

答案 1 :(得分:0)

我有一个类似的问题;但是,我只在代码的HTML部分中。请参阅以下内容:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <title>Settings</title>
    <script>

    function onSettingsOpen()
    {

    document.getElementById(propsList[0]).checked = (allPreferences.getProperty(propsList[0]) === "true");
    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");

    }

</script>
</head>
<body onload="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>

所以我的问题是,我无法运行onSettingsOpen()。我不完全确定为什么,当我打开设置窗格(我已将其作为边栏实现到代码中)时,它只是无法运行。如果有用的话,也乐于包含processForm()代码;但是,就我当前的问题而言,这并不重要。预先感谢您的帮助!