ARUN NAGARAJAN: Hello and welcome.
My name is Arun Nagarajan.
And in this video tutorial, we'll talk about adding custom
menus to spreadsheet.
So menus are the top bar where you're able to inject things,
like where you have file, edit, and whatnot.
And in this tutorial, at the end of it, you'll be able to
add your own custom menus to that bar.
So let's get started.
So you will find the tutorial under the Intermediate
Tutorials and Other Tutorials section.
Just scroll down to the section called User
Interfaces.
Click on Defining Spreadsheet Menus.
And that'll be all the code we'll need.
It actually says five minutes.
But it'll take a lot less than that.
It's actually a very straightforward process,
you'll see.
So in order to do this, the first thing that you need to
do is have a spreadsheet available.
So for this demo, I'll start a new spreadsheet.
But you can also use an existing
spreadsheet if you have one.
I'll just name this--
Custom menus.
And I need to open the Script Editor in the context of the
spreadsheet.
So in other demos, you may have seen us go to
script.google.com directly and enter a code there.
But in this particular example, it's very important
that the script is associated with the spreadsheet.
And when you go to the Script Editor for the first time or
if you have the welcome screen still displayed, you may have
to close out of it to come to the Script Editor here.
So as mentioned before, I'll just copy and paste this code,
just a few lines here.
And I'll replace the stub that we give you.
We'll save it.
We'll call it Custom menus.
And it's ready to go at this point.
So you can see that the code is actually pretty
straightforward.
We'll kind of break this down in a second.
But let's just make sure that we know what we're trying to
achieve here.
So here's the menu bar.
And the goal of this exercise, at the end, is to be able to
have your own menu logic associated in this bar.
So we're going to go in here.
And for this demo, we're going to manually
run the onOpen function.
So it's going to run this.
And let's go ahead, and I'll actually reload.
There it is.
And I'll reload this page real quick.
And the onOpen should run.
So it gives a special meaning to that function.
And there it is.
Now when I go ahead and say, Say Hi, it's
going to run the function.
And all it's doing at this point is showing a dialogue
saying, Hi.
As you can imagine, the other one says, Hello.
Not very creative, but gets you the setup necessary to add
your own custom menus.
Now, let's go take a look at the code real quick.
And you saw that the onOpen had special meaning.
And when I refresh the spreadsheet, I didn't have to
run anything manually.
It just ran by itself after a few seconds after it checked
for the existence of the script.
And what it is is essentially a set of objects in an array.
So this right here is a JavaScript object.
It's a map.
It's got a name and a value.
The value is the important part here.
This is the literal name of the function without any
parameters.
So you don't have to put the open-close parentheses there.
That's very important.
I've seen a few people make that mistake.
And the capitalization is also important--
functionName.
And that's really it.
And you can add any number of menus you want.
In this example, like I said, all we're
doing is show a dialogue.
But you could have your own custom calculation, your other
business processes or workflows that you can have in
JavaScript.
All could be injected in here.
As a simple addition to something that you can do
that's beyond what's in the tutorial, one of the things
that you can do actually-- and you may see this in other
menus that we have--
is that we have this concept of a separator in a few of
these tutorials, in a few of these menus, I should say.
So as a little pro tip, I can just inject null.
And now when I run this method, I go in here, and
there's the separator between the two menu options.
So that's a little nice thing that you can do to really lay
out your menus the way you want to.
And you can have multiple menus as well.
So this ss.addMenu, you can have multiple calls of this.
And it'll just stack left to right in the
order you call it.
So I hope you found this useful.
Take this tutorial and start building your own complex
business processes and interesting things that you
can do with your workflow, with your spreadsheets.
Thank you.