首页 > 解决方案 > 关闭谷歌应用脚​​本中的modalDialogue框后运行代码

问题描述

在单击 ID 为“submitButton”的按钮后,我试图关闭模态对话框并在函数对象中运行代码。

var FuncObj = {
checker:checker
checker2:checker2
};

以下是我的 HTML

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<link rel="stylesheet" 
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js">. 
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<style>
.padd5{
  padding: 5px 0;
}

</style>
</head>
<body>

<div class="continue-button">
    <button class='blue' id="submitButton">Continue </button>
  </div>
  
</body>
<script>
$("#submitButton").click( 
google.script.run.withSuccessHandler(close).runThatFunction(checker);`

function close(){
  google.script.host.close();
 }

</script>
</html>

下面是呈现 HTML 的代码,HTML 被正确呈现,但当我单击按钮时没有任何反应:

function test(functionName) {
if (condition1){
FuncObj[functionName]();
} 
if(condition2) {
  var html = HtmlService
  .createTemplateFromFile('unpaid')
  .evaluate()
  .getContent();
var finalHtml = HtmlService.createTemplate(html).evaluate().setWidth(300).setHeight(560);

SpreadsheetApp.getUi() 
.showModalDialog(finalHtml, 'test');
} 
else {
  SpreadsheetApp.getActive().toast("Exhausted");  
}
}

这是调用该函数的代码:

function runThatFunction(functionName){ FuncObj[functionName]();}

代码顺序如下 -

  1. 上面的第一个“test(functionName)”将运行。
  2. 如果它进入条件 2,则 HTML 中的模态对话框将显示给用户
  3. 一旦用户单击 ID 为“submitButton”的按钮,模式对话框应该关闭并运行作为参数传递给上面“1”中的测试函数的函数。

我被困在上面序列的#2上。无法关闭 modalDialogue 框并运行它后面的任何函数。

标签: javascriptgoogle-apps-script

解决方案


要在关闭后运行代码,请添加要在之后运行的代码google.script.host.close();。请记住,这只能通过调用close()函数完成关闭。


推荐阅读