3

I have a function in my server code(.gs) which pops up a dialog for input from user. I should wait for user input before proceeding with the function. To do this, I am using a while loop with sleep and checking for value of a key in PropertiesService. The value will be set on click of OK from the dialog.

Main.gs
function test(){
  DoSomething();
  SpreadsheetApp.getUi().showModalDialog(html, "Title");

  while(value is not set){
    Utilities.sleep(5000);
  }

  DoAnotherthing(value);
}

I am not happy with this solution (use of while and sleep) and was wondering if there is a better way to do this. Any pointers is appreciated.

Ekalavya
  • 39
  • 2
  • I've been trying to do some things with `Promise`. The idea was to save the `resolve` as a global variable, which is called in a function that was called from the dialog. The problem is that when the dialog calls a function, it creates a new execution for that, and my global `resolve` variable doesn't exist there. – Yuval.R Aug 11 '22 at 10:07
  • I agree there should be a better way. In your example, you're waiting 5 seconds in each loop for the prompt - which can be a long time if you're entering in a lot of data fast. At the same time, if you make your loop shorter, then you're using up your processing budget with Google and if this is a script executed by many users many times it could eat up your quota... – Trashman Oct 03 '22 at 14:42

0 Answers0