使“提交”按钮关闭对话框,然后将焦点返回到电子表格Google App脚本

时间:2019-06-28 11:29:41

标签: google-apps-script web-applications

当我单击“提交”时,焦点将返回到电子表格并加载数据,但对话框不会关闭。我使用了google.script.host.close(),但不会将焦点传递回电子表格。

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
<form >
  Select Client:
  <div>
  <select id="optionList" name="client">   
  </select>
  </div> 
   Select Action:
  <div>
  <select id="actionList" name="action">   
  </select>
  </div> 
  Username:
  <input type="text" name="username"> <br />
  Password:
  <input type="password" name="password"> <br />
  <input type="button" value="OK" onclick="google.script.run.callNumbers(this.parentNode);google.script.host.editor.focus();" />
</form>
  </body>

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
    // The code in this function runs when the page is loaded.
    $(function () {
        google.script.run.withSuccessHandler(buildOptionList)
            .getClients();
            google.script.run.withSuccessHandler(buildActionList)
            .getActions();
    });

    function buildOptionList(clients) {
        var list = $('#optionList');
        list.empty();
        for (var i = 0; i < clients.length; i++) {
            list.append(new Option(clients[i]));
        }
    }

     function buildActionList(actions) {
        var list = $('#actionList');
        list.empty();
        for (var i = 0; i < actions.length; i++) {
            list.append(new Option(actions[i]));
        }
    }
</script>


</html>
    function html(){
       var html = HtmlService.createHtmlOutputFromFile('load');
       SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
       .showModalDialog(html, 'Input API Creddentials');
    }

我想在关闭对话框后将焦点重新回到电子表格。我尝试在沙盒模式下使用google html模板代替对话框,但这没有用。

当我同时使用google.script.host.editor.focus()google.script.host.close()时,它没有将焦点发送回工作表。

1 个答案:

答案 0 :(得分:0)

在调用函数后,我仅使用成功处理程序来关闭对话框。

<input type="button" value="OK" onclick="google.script.run.withSuccessHandler(google.script.host.close).callNumbers(this.parentNode);google.script.host.editor.focus();" />

这将关闭对话框窗口,并将焦点返回到电子表格上。