首页 > 解决方案 > Google Apps 脚本调用函数,该函数执行模态对话框,然后在用户进行选择后执行另一个函数

问题描述

我在 Google 表格上链接了一个按钮,当用户单击时,该按钮将运行此功能:

function ModalSelection(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('main_gen');
  var cell = ss.getRange(1,1).getValue();
  if(cell == ''){
    checkWindow(); //It runs this function if `cell` is empty.
  //I would like to run another function here...anotherFunction()...but not until
  //the user has made their selections from the Modal Dialog box. 
  }
  else {send();} //send is not relevant to this question.
}

这是checkWindow()功能:

function checkWindow(){
  var ui = SpreadsheetApp.getUi();
  var result = emailList(); //This is a string grabbed from another function.
  //Call the HTML file and set the width and height
  var html = HtmlService.createHtmlOutput(result)
    .setWidth(400)
    .setHeight(450);

  //Display the dialog
  var dialog = ui.showModalDialog(html, "Select your choices here.");
}

checkWindow()函数运行并且用户选择“提交”时,它会运行另一段短代码(见下文),将用户的选择插入到单元格中。我需要等到该单元格不再为空,然后才能继续执行下一个函数。

function form_data(e){
  SpreadsheetApp.flush();
  var value = [e.email];
  var val = value[0].toString();
  for(var i = 1; i<value.length; i++){val += ', ' + value[i];}
  SpreadsheetApp.getActiveSheet().getRange(2, 2).setValue(val);
  //I have tried adding the anotherFunction(); function right here, but the code breaks and the only error I see
  //in the console error log is "Uncaught". It gives me no additional details. 
}

编辑以包含所有 HTML。

HTML 代码如下(EmailCheckBox.html):

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
  </head>
  <body>
    <form id='myform'>
        <table border=0>

           %email

        </table>

    </form><br>
        <input type="button" value="Submit" onclick="google.script.run.withSuccessHandler(google.script.host.close).form_data(document.getElementById('myform'))"/>
        <input type="button" value="Close" onclick="google.script.host.close()"/>
  </body>

它所指的 %email 就是这个 JS 代码:

function checkEmail(List){
  var output = '';
  for(var i=0; i<List.length; i++)
  {
    output += '<tr><td><input type="checkbox" name="email" id="' + List[i] + '" value="' + List[i] + '"/>'+ List[i] + '</td></tr>'; 
  }
  var result = HtmlService.createHtmlOutputFromFile('EmailCheckBox').getContent();
  result = result.replace('%email', output);
  Logger.log(result);

  return result;
}
</html>

结束编辑

有什么想法可以做到这一点吗?谢谢!

标签: javascriptgoogle-apps-script

解决方案


我相信你的目标如下。

  • 您想在 的对话框中sendCheck完成该过程后打开 的对话框checkEmailWindow

对于这个,这个修改怎么样?

修改点:

  • 在您的脚本中,checkEmailWindow()sendCheck()不断运行。这样,打开的对话框就会被打开的checkEmailWindow()对话框覆盖sendCheck()。我认为这是您的问题的原因。
  • 为了sendCheck()在进程完成后在对话框中打开对话框checkEmailWindow,我进行google.script.run.withSuccessHandler(google.script.host.close).form_data(document.getElementById('myform'))了如下修改。
    • google.script.run.withSuccessHandler(google.script.run.sendCheck).form_data(document.getElementById('myform'))
    • 这样,在 的对话框中sendCheck处理完成后打开 的对话框checkEmailWindow

对于send()在 Google Apps 脚本方面:

从:
if(bccSend == ''){
  checkEmailWindow();
  sendCheck();
}
至:
if(bccSend == ''){
  checkEmailWindow();
  // sendCheck();  // Removed
}

HTML & Javascript 方面:

从:
<input type="button" value="Submit" onclick="google.script.run.withSuccessHandler(google.script.host.close).form_data(document.getElementById('myform'))"/>
至:
<input type="button" value="Submit" onclick="google.script.run.withSuccessHandler(google.script.run.sendCheck).form_data(document.getElementById('myform'))"/>
  • sendCheck与运行withSuccessHandler

推荐阅读