Andrew M
Andrew M

Reputation: 11

Google Apps Script - returning the value from a drop down html prompt

I have constructed a code that creates a pop up window with a drop down selector. That is working fine. The issue is coming from trying to pass the selection the user makes back out of the html file and into a different function as an argument.

The pass back is only returning "null" in the logger.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <span>Select your most perferred remote control comms. protocol from the list below.</span>
    <form>
      <select name="Protocols" onchange="parseBackFunc()">
        <option value=17>Modbus</option>
        <option value=18>SCPI</option>
        <option value=19>CAN open</option>
        <option value=20>Profibus</option>
        <option value=21>Profinet</option>
        <option value=22>EtherCAT</option>  
      </select>
    </form>
    <br>
    <input type="button" onClick="google.script.host.close();" value="Submit" />
  </body>
</html>

<script type="text/javascript">
  function parseBackFunc() {
    google.script.run.htmlReturn(document.forms[0].value);
  }
</script>

At one point I manually put in some numbers in the argument of the htmlReturn function and that actually worked, those numbers showed up in the logger. Could someone please point out how I can properly reference the users selection?

Upvotes: 0

Views: 924

Answers (1)

Wicket
Wicket

Reputation: 38284

Instead of

google.script.run.htmlReturn(document.forms[0].value);

use

const select = document.querySelector('select[name="Protocols"]');
const value = select.options[select.selectedIndex].value;
const text = select.options[select.selectedIndex].text;
google.script.run.htmlReturn(value);

Related

Upvotes: 2

Related Questions