简体   繁体   中英

Google Sheets App Script Html onload not working

I'm using google sheets and I'm creating a document that will pull through employees that are out of the office. I have a menu option to remove employee data, and it opens the sidebar where I have an HTML form ( Image of my project ). I'm trying to have it generate a dropdown list of current employees on the list.

I've developed the code to pull through the data I need:

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;
}

Now we move over to my html, where I am simply trying to load the dropdown list using a for loop in the header:

<!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>

I threw a div in the body and have the function changing the value to "test" at the start, this was just to check and see if the function was even being called, which it doesn't seem like it is.

I also tried using window.onload (as shown below), but that didn't get me anywhere either.:

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);

      }
    }

Any guidance you can give me would be really appreciated!

google.script.run doesn't return values. When you want to return values from GAS, please use withSuccessHandler . And the data retrieved by getValues() is 2 dimensional array. So how about this modification?

Modified script :

<!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>

Reference :

If I misunderstand your question, I'm sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM