Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Google App script from executing in parallel

I have a Google App script that is being executed by clicking on an floating image on a sheet.

How can I prevent people from running the script AGAIN before it is finished with the first execution?

It seems that you can execute the script multiple times by clicking the image and that the subsequent executions conflict with the previous ones and cause erroneous results.

I was going to temporarily disassociate the script with the floating image while it is running an then restore it before exiting the script but I can't seem to find a way to manipulate the floating image's properties.

like image 423
Dr Dogmar Avatar asked Oct 19 '25 09:10

Dr Dogmar


1 Answers

For client side handling, you can assign a value to a variable to identify the status of function.

index.html

<!DOCTYPE html>
<html>
  <body>
    <img src="http://cdn.soulsplay.com/assets//listing/css/comments/submit-button-2.png" onclick="invokeSubmit();">
  </body>
  <script>

    var image_clicked = 0;

    function invokeSubmit(){
      image_clicked ++;
      if(image_clicked == 1){
        alert("Processing");
        google.script.run.withSuccessHandler(function(){
          image_clicked = 0;
          alert("done");
        }).sleep_func();
      }
    }
  </script>
</html>

code.gs

function doGet() {
  var output = HtmlService.createTemplateFromFile('index').evaluate();
  output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
  output.setTitle('App Script');
  return output;
}

function sleep_func(){
  Utilities.sleep(15000);
  return;
}

You can also prevent the script from concurrent access by using Lock Service.

function test_func(){
  var lock = LockService.getScriptLock();
  lock.waitLock(15000);

  //Some operations

  lock.releaseLock();
}
like image 188
Ritesh Nair Avatar answered Oct 20 '25 21:10

Ritesh Nair