0

I have a problem with my google apps script. I have created a dropdown select element that gets values from json and Im trying to pass the selected value back to code.gs within a variable. How can i accomplish this? The json data comes from the function "join". Im trying to get the "id" variable to be used in the api call.

Here's my html file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <select id="droplist">
      </select>
      <button type="button"  onclick="values()" >Hae</button>
      <script>
      (function () {
        google.script.run.withSuccessHandler(
          function(people) {
            var select = document.getElementById("droplist");
            for( var a of people) {
              var option = document.createElement("option");
              option.text = a[0] + " " + a[1];
              select.add(option);
            }
          }
        ).getPeople();
      }());
      function values() {
        var x = []
        x.push.document.getElementById("droplist").selectedIndex;
        alert(document.getElementsByTagName("option")[x].value);
        Logger.log(x)

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

And here's code.gs function where im trying to get the data:

function join(x) {

  var id = [];
  id.push(x)
  Logger.log(x)
  Logger.log(id);

1 Answer 1

2

I'm assuming that you want multiple values as you names the function values. Then to do so, you can get the selected values by using selectElement.selectedOptions and extracting the data from them. Then you can call the join function with google.script.run like you do with getPeople.

Here is a minimum code example: Code.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('page')
}

function join(values) {
  Logger.log(values)
}

Page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
      function values() {
        // Get the values
        const selected = document.getElementById("droplist").selectedOptions
        const values = Array.from(selected).map(el => el.value)

        // Call the Apps Script function
        google.script.run
          .withSuccessHandler(_ => console.log('success'))
          .withFailureHandler(error => console.error(error))
          .join(values)
      }
    </script>
  </head>
  <body>
    <select id="droplist" multiple>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <br>
    <button onclick="values()">Send values!</button>
  </body>
</html>

References

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment, but i figured this out myself. I'll post the solution tomorrow to my original question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.