Is 'chaining' functions available in GAS?

2019-09-14 22:15发布

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.

2条回答
地球回转人心会变
2楼-- · 2019-09-14 22:33

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.

查看更多
▲ chillily
3楼-- · 2019-09-14 22:41

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.

查看更多
登录 后发表回答