0

I have a button that when pressed, calls a function that modifies up to 9 CalendarEvents. Since calls to the CalendarApp are slow, and execution to make changes to 9 events takes about 20-30 seconds, I'm trying to implement something like an "updating" message, so that the user of the UI knows that changes are actually being made.

I'm having trouble figuring out how I can update the UI while my function, saveChanges, runs in the background. I know that there is a way to accomplish this, but I can't figure it out.

In order to the popup or label to become visible to the user, the UI instance needs to be returned. But if I return the UI instance, execution of my function stops entirely. How can I work around that?

Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75

2 Answers2

2

The motivation behind What happens when I "sleep" in GAS ? (execution time limit workaround) is different than yours, but the code in the question would do what you're looking for.

You'd need to adapt it to the specific UI you're using (you don't say whether you're in a container-bound script or not...), but the basic idea is to set up a (hidden) checkbox in your UI that has a handler attached to it. The handler is invoked whenever the checkbox state changes, and it is the handler that does the real work - modifying calendar events, in your case.

But with a hidden checkbox, how would the handler get invoked, you ask? Trickery! The original UI app kicks things off by using setValue() to 'click' the checkbox the first time. After that, the handler 'clicks' itself at the end of every work cycle.

Also at the end of a work cycle, the handler updates an element in the UI (which it gets a handle on via getElementById()) with some new value. This is how you get around that problem of a "static" UI. In Serge's example he's running a clock display, but you could easily present a %-complete value, or specific status messages.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • This looks to be a bit more complicated than what I was looking for, but definitely effective, and the preferable solution in the end :) – Chris Cirefice Dec 17 '13 at 04:10
1

Look at client handlers. Where you already have a server handler added (a button maybe) add also a client click handler that simply sets "updating..." to a label. Your server handler also sets it to "ok" when done. To be more fancy you can instead show an animated progress gif and hide it from the server handler.

Zig Mandel
  • 19,571
  • 5
  • 26
  • 36
  • Though your answer does what I was asking for, Mogsdad's answer is actually a better fit - this way I can update the UI with a message like... "Event X out of 9 processed". – Chris Cirefice Dec 17 '13 at 04:03
  • Yes, its more complete because you can give progress, Ive done that too, but you wont get an inmediate first-status, because it requires a server round-trip. If you use the client handler its inmediate when the user clicks your button. Combine both. – Zig Mandel Dec 17 '13 at 13:06
  • Yes, you're right. I added the client handler, now I'm working on reworking my event processing to incorporate Mogsdad's answer. Thanks to the both of you! – Chris Cirefice Dec 17 '13 at 23:14