Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
You can create a handler like any other element in the UI by calling
on the UiApp class to create what you need. While you can attach a handler
directly to a widget using the .add statement, we will
create the handler here by loading it into a variable. This is a common
practice because it makes the code more readable, but like most things in
programming it’s not always the best solution. Later in the book you will
learn techniques where attaching the handler directly is preferred.
This is how a handler is created:
var handler = app.createServerHandler('contactMe');
What we have done is created an object, the handler, using the call
createServerHandler. In the past,
Google Script had many types of button handlers, but now these have been
condensed into a single do all server handler. This server handler acts
like a click handler used for submit buttons and requires a full button
cycle (Down and Up) to execute. That way if the user has pressed down, but
then decides she was not ready, she can drag off the button, let go, and
the button will not execute.
Handlers have the ability to take a function argument on creation,
as we have here in specifying 'contactMe', which is the
name of the function we will be creating later in the chapter to perform
the work. You can also add the function later by using:
handler.setCallbackFunction('contactMe');
You might be wondering about that method having the word “callback” in it, which leads us to a very important point in the UiApp.