0
votes

I have a button on a sheets similar to the one described in the documentation.

The problem is I want to avoid users clicking it multiple times at once (because the script sends messages).

I noticed that the drawing button don't pass an event to the function. Is there a way to accomplish this? I've been searching but didn't found an answer, any suggestions are welcome.

2
Have you looked into the Lock Service for this?ross
Thanks, this seems the appropriate service! The others answers will do the trick 90% of timepeter_b

2 Answers

2
votes

Yes, it is possible with Script properties

E.g. you set-up a script property within the first call of the script. Within the following calls the script checks if the property already exists, and if it does - it does not run the rest of the code.

Sample:

function onlyOnce(){
  var scriptProperties = PropertiesService.getScriptProperties();
  var myProperty = scriptProperties.getProperty('already_called');
  if (!myProperty){
    showMessageBox();
    scriptProperties.setProperty('already_called', 'YES');
  }
}
function showMessageBox() {
  Browser.msgBox('You clicked it!');
}
1
votes

A Short Time Debouncer

I think I'd try it this way so that it automatically resets itself after a short time.

function onlyOnceInAShortTime() {
  var cache = CacheService..getUserCache();
  var cached = cache.get("already_called");
  if(cached=="YES") {
    Browswer.msgBox('You click it');
    return;
  }
  cache.put('already_called', 'YES' , 20);//remains for twenty seconds
}

Class Cache