google-sheetsgoogle-apps-scriptshowmodaldialog

Issue in Auto close Modal Dialog - Google App-Script


I have a function like this

function data_process(){
    var output = HtmlService.createHtmlOutput('<b>Processing the Data!!</b>')
                          .setHeight(150)
                          .setWidth(200);
    SpreadsheetApp.getUi().showModalDialog(output, '⚙️ Processing..');
    Utilities.sleep(1000);  

    /* Here I am Building an chart into new sheet. There is no issue in chart creation 
 and creating an new sheet for charts */

  var output = HtmlService.createHtmlOutput('<script>google.script.host.close();/script>');
  SpreadsheetApp.getUi().showModalDialog(output, 'Loading...');
}

I am referring to this solutions [1]: Auto close modal dialog - After server code is done, close dialog in Google Spreadsheet

Modal box does not shows up and no error as well. Any help is much appreciated. Thanks in advance.


Solution

  • In order to test using this function with Google Sheets, I suggest that you go to the Extensions menu on the Spreadsheet itself, then choose the Apps Script option.

    This will open up a new Apps Script window, where you can use this code:

    function data_process() {
    
        var ui = SpreadsheetApp.getUi(); 
        var output = HtmlService.createHtmlOutput('<b>Processing the Data!!</b>')
                              .setHeight(150)
                              .setWidth(200);
    
        ui.showModalDialog(output, '⚙️ Processing..'); 
    
    // Simulate processing delay
    
      Utilities.sleep(300);
    
    // Perform the data processing and chart creation here
    // ...  
    
    var closeDialog = '<script>google.script.host.close();</script>';
    var closeOutput = HtmlService.createHtmlOutput(closeDialog);
    ui.showModalDialog(closeOutput, 'Loading...');
    
    }
    

    Next, select the data_processing() function and click on Run, then give the permission in order to allow the script to run. Next, go back to the open sheet tab, and the script should automatically run.