Home > Blockchain >  GAS function won't run when executed from htmlService?
GAS function won't run when executed from htmlService?

Time:09-14

Im trying to switch my current sheet after 5s of the htmlService modalDialog opening. For some reason SwitchToSheet1() does not work...

.gs

function openModal() {
  var ui = SpreadsheetApp.getUi();
  var html = HtmlService.createTemplateFromFile('BarcodeLoadingHTML')
  .evaluate()
  .setWidth(400)
  .setHeight(250);
  ui.showModalDialog(html, "‎");
}

function SwitchToSheet1() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Bestandsliste'), true);
};

BarcodeLoadingHTML.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= include('jQuery'); ?>
    <?!= include('Stylesheet'); ?>
    <?!= include('lottieplayer'); ?>
  </head>
  <body>
    <div id="loading">
        <lottie-player id="lottie-load" src="https://static.staticsave.com/lottie/cloudupload.json"  
          background="transparent"  
          speed="1" 
          loop 
          autoplay>
        </lottie-player>
        <lottie-player id="lottie-success" src="https://assets3.lottiefiles.com/private_files/lf30_qXYuJE.json"  
          background="transparent"  
          speed="1">
        </lottie-player>
        <p  id="text">Bitte warten<span>.</span><span>.</span><span>.</span></p>
    </div>
    <script>
        $(window).ready(setTimeout (function() {
            $('#lottie-load').hide()
            $('#text').text('Upload Erfolgreich!')
            $('#lottie-success').fadeIn()
            $('#lottie-success').get(0).play();
        }, 2500));
        
        $(window).ready(setTimeout (function() {
            google.script.run.SwitchToSheet1();
            google.script.host.close();
        }, 5000));

    </script>
  </body>
</html>

CodePudding user response:

Replace

google.script.run.SwitchToSheet1();
google.script.host.close();

by

google.script.run
.withSuccessHandler(() => google.script.host.close())
.SwitchToSheet1();

The above because google.script.host.close() is executing before google.script.run.SwitchToSheet1(). This happens because calls to server code using google.script.run are asynchronous, in other words, the Google servers not all the time responds instantly when they are called by run, so google.script retry until the server responds, but if the dialog is closed, there aren't more attempts to call the server.

Related

CodePudding user response:

FWIW, assuming I have a spreadsheet with two sheets named Sheet1 and Sheet2, and Sheet2 is the active, the following code works for me:

// Code.gs
function showDialog() {
  const html = HtmlService.createHtmlOutputFromFile('page')
   .setWidth(400)
   .setHeight(400);
  SpreadsheetApp.getUi().showModalDialog(html, 'My custom dialog'); 
}

function SwitchToSheet1() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sh = spreadsheet.getSheetByName('Sheet1');
  spreadsheet.setActiveSheet(sh, true);
};

And the HTML page:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>            
    <script>
      window.addEventListener('DOMContentLoaded', () => {
        setTimeout (function() {
            google.script.run.SwitchToSheet1();
            google.script.host.close();
        }, 5000)
      })
    </script>
  </body>
</html>
  • Related