Is 'chaining' functions available in GAS?

2019-09-14 21:57发布

问题:

Is it possible to 'chain' functions? Say, I have a serverclickhandler and then would like to invoke another function after it right away. How do I go about it?

Thanks.

回答1:

To have a second handler executed after the first one you just need to call it directly from your first handler, e.g.

var app = null;
function firstHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  //do your thing

  //now, instead "return app;" you return the second handler
  return secondHandler(e);
}

function secondHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  //do your job
  return app;
}

I placed the app var on a global scope so you can get it only when necessary (sharing it between the functions without explicit passing it), saving a possibly costly and unknown behavior (to me at least) of a second getActiveApplication() call.



回答2:

Not sure I understood exactly your question but serverhandlers to which you refer are precisely made for that... (see doc) Note that you can assign multiple handlers to UI elements to trigger different functions and eventually chain them.