Home > Blockchain >  How to capture if a google.script.run is being executed?
How to capture if a google.script.run is being executed?

Time:10-19

I want to catch google.script.run while it is running, so I can block the user from clicking and activating the function multiple times.

Is something like this possible?

while(google.script.run.function()){
alert("Wait for the function to finish")
}

CodePudding user response:

Disabling a button:

function lfunko() {
  return "Button is disabled"
}

function launchMyDialog() {
  const html = '<input type="button" id="btn" value="Click Me" onclick="myfunk();" /><div id="msg"></div><script>function myfunk(){document.getElementById("btn").disabled = true;google.script.run.withSuccessHandler((msg) => {document.getElementById("msg").innerHTML = msg;}).lfunko();}console.log("Code");</script>';
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html),"Sample Dialog")
}

Demo:

enter image description here

CodePudding user response:

I used @Cooper suggestion and deactivated the client click button, and after the function ends I activate it again. Getting like this:

HTML

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button id="btn">BUTTON</button>
     <?!= include('javascript'); ?>
  </body>
</html>

javascript.html

document.getElementById("btn").addEventListener("click",click);

function click(){
google.script.run.withSuccessHandler(table).doSomething();
document.getElementById("btn").disabled=true;
}

function table() {
  document.getElementById("btn").disabled=false;
}

code.gs

 function doSomething(){ 
  Logger.log("Hello");
}
  • Related