google-sheetsgoogle-apps-scriptweb-applicationsdialog

showModalDialog in SpreadsheetApp Won't Close


I tried to create a script to run the Spreadsheet APP, by utilizing showModalDialog, Yes - No Button, and ui.Alert.

Unfortunately, I'm having trouble getting showModalDialog to close after I select the Yes Button.

This is my script :

  function showDialog() {
    
    var htmlOutput = HtmlService
                     .createHtmlOutput('<p><b>TEXT HTML  HERE</b></p><br><br><br><br><button onclick="google.script.run.clickYes()">YES</button><button onclick="google.script.host.close()">NO</button>');
                    
     SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'CONFIRMATION');
     } ;

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

    function setSheetMaster() {

      var ui          = SpreadsheetApp.getUi();
      var response2    = ui.alert( 'Please Select "NO", to Cancel, and "YES" to Continue' ,
     ui.ButtonSet.YES_NO
      );

     if (response2 == ui.Button.YES) {
         Logger.log('The user clicked "Yes."');
        } else {
          return
        }
    };

showModalDialog in SpreadsheetApp Won't Close

As shown in the image, showModalDialog should close, while ui.alert response2 displays. I've used the additional

google.script.host.close();

but it did'nt work to close showModalDialog. Any suggestions for improvement? Your help would be greatly appreciated.


Solution

  • In your showing script, how about the following modification?

    Modified script 1:

    In this modification, google.script.run.clickYes() and google.script.host.close() are run with an asynchronous process.

    function showDialog() {
      var htmlOutput = HtmlService
        .createHtmlOutput('<p><b>TEXT HTML  HERE</b></p><br><br><br><br><button onclick="google.script.run.clickYes();google.script.host.close();">YES</button><button onclick="google.script.host.close()">NO</button>');
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'CONFIRMATION');
    }
    
    function clickYes() {
      setSheetMaster();
    }
    
    function setSheetMaster() {
      var ui = SpreadsheetApp.getUi();
      var response2 = ui.alert('Please Select "NO", to Cancel, and "YES" to Continue',
        ui.ButtonSet.YES_NO
      );
      if (response2 == ui.Button.YES) {
        Logger.log('The user clicked "Yes."');
      } else {
        return
      }
    }
    

    Modified script 2:

    In this modification, google.script.run.clickYes() and google.script.host.close() are run with a synchronous process.

    First, google.script.run.clickYes() is run. And, the script of clickYes() is run. Then, when the dialog opened with clickYes() is closed, google.script.host.close() is run.

    function showDialog() {
      var htmlOutput = HtmlService
        .createHtmlOutput('<p><b>TEXT HTML  HERE</b></p><br><br><br><br><button onclick="google.script.run.withSuccessHandler(google.script.host.close).clickYes()">YES</button><button onclick="google.script.host.close()">NO</button>');
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'CONFIRMATION');
    }
    
    function clickYes() {
      setSheetMaster();
    }
    
    function setSheetMaster() {
      var ui = SpreadsheetApp.getUi();
      var response2 = ui.alert('Please Select "NO", to Cancel, and "YES" to Continue',
        ui.ButtonSet.YES_NO
      );
      if (response2 == ui.Button.YES) {
        Logger.log('The user clicked "Yes."');
      } else {
        return
      }
    }
    

    Modified script 3:

    In this modification, after google.script.run.clickYes() is run, google.script.host.close() is run with a new dialog. And, setSheetMaster() is run.

    function showDialog() {
      var htmlOutput = HtmlService
        .createHtmlOutput('<p><b>TEXT HTML  HERE</b></p><br><br><br><br><button onclick="google.script.run.clickYes()">YES</button><button onclick="google.script.host.close()">NO</button>');
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'CONFIRMATION');
    }
    
    function clickYes() {
      SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput('<script>google.script.host.close()</script>').setWidth(10).setHeight(10), 'sample');
      setSheetMaster();
    }
    
    function setSheetMaster() {
      var ui = SpreadsheetApp.getUi();
      var response2 = ui.alert('Please Select "NO", to Cancel, and "YES" to Continue',
        ui.ButtonSet.YES_NO
      );
      if (response2 == ui.Button.YES) {
        Logger.log('The user clicked "Yes."');
      } else {
        return
      }
    }