从Google应用脚本中的下拉列表值运行一个函数

时间:2015-10-20 08:18:57

标签: javascript google-apps-script

我是Google App Script的新用户,我尝试使用HtmlService创建一个下拉列表,根据所选项目的值,它会运行一个函数。但是点击OK按钮后没有任何事情发生。这是我的代码,

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>

如何运行功能&amp;在Google应用脚本中点击OK按钮后,将所选项目的值作为参数传递给该功能?急需这个帮助!感谢。

1 个答案:

答案 0 :(得分:1)

在加载之前,您无法使用getElementById访问DOM。 在<body>标记后移动您的脚本标记 有关对话框的更多信息:https://developers.google.com/apps-script/guides/dialogs