Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a function from dropdownlist value in Google app script

I'm new to Google App Script and I'm trying to make a dropdownlist using HtmlService where it'll run a function depending on the value of the selected item. But nothing is happening after clicking the OK button.

Here are my codes:

CODE.GS

function getList()
{
    var myHtml = HtmlService.createHtmlOutputFromFile('index').setSandboxMode(HtmlService.SandboxMode.IFRAME);
    SpreadsheetApp.getUi().showModalDialog(myHtml, 'Select Your Option');
}

HTML

<head>
    <base target="_top">
    <script>
        document.getElementById("btnGet").onclick = function() {
            var e = document.getElementById("menu");
            var getMenu = e.options[e.selectedIndex].value;
            if (getMenu == "inbox") {
                google.script.run.my_scrapper(getMenu);
            } else {
                return 0;
            }
        }
    </script>
</head>

<body>
    <select id="menu">
        <option value="inbox">Inbox</option>
        <option value="drafts">Drafts</option>
    </select>
    <br />
    <button id="btnGet">OK</button>
</body>

How can I run the function & pass the selected item's value as parameter in the function after I click OK button in Google App script?

like image 543
Shihan Khan Avatar asked Jul 27 '26 00:07

Shihan Khan


1 Answers

You can't access the DOM using getElementById before it has loaded. Move your script tag after <body> tag.
For more about dialogs : https://developers.google.com/apps-script/guides/dialogs

like image 102
niwox Avatar answered Jul 29 '26 14:07

niwox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!