WESLEY CHUN: Hey, everybody.
This is Wesley Chun, an engineer here at Google.
Today, I'm going to introduce you to one of our best
kept secrets-- Google Apps Script.
You're kidding, right?
Another Google secret?
All right, so it's not our best kept secret nor is it really
a secret at all.
But if you haven't heard of it yet,
you'll be wondering why you haven't by the time we're done.
So, first things first.
What is Google Apps Script?
Well, it's a tool based on JavaScript,
but it's a little less like using APIs and more like using
services or built-in access to APIs.
And finally, there's less OAuth2 authorization code for you
to write.
All right, to help with this understanding,
let's start with what Apps Script is not.
All right, the good news here is that there's really nothing new
to learn.
It's just JavaScript.
Even a Python guy like me knows some JavaScript.
But it's not your daddy's JavaScript.
Aside from one exception which is out
of the scope of this episode, Apps Script code
is not client-side and does not run in the browser,
meaning there's no DOM manipulation like you
see behind me.
The other thing you do in the browser
is edit your Apps Script code.
Now, if you're familiar with Node.js,
it's like Node being server-side and all, only you're
not running asynchronous event-driven code executing
on Node servers.
Instead, your Apps Script code runs
on the same servers in the same data centers that
run Google services, like Search, YouTube, and Gmail,
giving you access to resources that
don't come with Node servers.
All right, based on its name, you
can probably guess that Apps Script will give you access
to Google Apps data and services,
but it doesn't stop there.
You can access other Google services
beyond Apps, such as Google Maps, YouTube,
Google Analytics, BigQuery, and Google Translate.
Here's a more complete list, and we're
continuing to look at what to add next that benefits users.
You may be asking what you can use Apps Script for
and there are many use cases, but a good rule of thumb
is to think a programmatic way to access data in Google Apps,
possibly using other Google services.
Oh, by the way, if you look down at the bottom,
those in the Other Services section
allow your app to communicate with outside world
to go beyond Google services.
Here are some sample applications.
Apps like teacher tools that use data in Google Docs and Sheets.
Mail merge apps are popular too.
You know, form letters in Google Docs
merging with email addresses in Google Sheets
and sent by Gmail.
Now, school districts and enterprises
using Google Apps for their domain
have many use cases, such as workflow and resource
management, such as this calendaring app.
Now, there are three types of Apps Script apps.
Standalone scripts that run by themselves, as you can guess,
add-ons, and document-bound scripts, which
are tied to a specific document, such as Docs or Sheets.
Finally, Apps Script can do simple websites as well as
gadgets bound to Google sites.
OK, that's enough talk.
Let's do some coding.
Your Hello World will be a Sheets-bound script
that writes Hello World to the first cell in a Google Sheet,
all right?
Let's follow me to the computer right now so we can do that.
All righty.
You guys all set to do your first Apps Script App?
Me too.
I'm happy to have you guys join me.
So why don't you go to your Google Drive
and create a brand new spreadsheet.
And once you're done, come join me back here
and we'll pick up from here.
All right.
Let's go and write some Apps Script code.
So, the way you do that is you go to your spreadsheet's Tools
menu item.
You scroll down and select Script Editor.
And what that will do is it will bring up a little dialog
box that has a bunch of boilerplate code
already written for you.
But since we're only going to do a simple one-liner,
we're just going to pick Blank Project.
If you're going to do something a little bit more complicated,
you may consider choosing one of these other skeleton
bits of code that have prebuilt functionality for you.
But for now, let's just do Blank Project.
And what this will do is it will pop you into the Apps Script
JavaScript editor that's in your web browser.
And it's already started the first function for you.
So all we're going to do is go to our spreadsheet
and stick Hello World as a string
into the first cell in our spreadsheet, which
is located at A1.
So, column A, row number 1.
So the way you do that is you connect to the spreadsheet app
by typing SpreadsheetApp.
And once you press dot, you have a selector
where you can choose from specific methods
that you can call.
So what we're going to do is we're going to call
the getActiveSheet() method to get the current sheet that
we're on.
And from here we're going to get the first cell, or the cell
in the upper left-hand corner, and use getRange for that.
And we're going to use a1Notation,
and we're going to specify the string that
says we want cell A1.
And there we go.
And then once we have the cell, let's
write the Hello World string to it.
And the method you use for that is setValue.
So there we go there.
And now we can enter Hello World and Save and Run.
Here's the Run button here.
It's a little triangle.
Once you click it, it asks you to save it and name it.
So let's do that here.
We'll call it Hello World.
And once it's saved, we can run it again.
Now, notice that none of this code
had any OAuth in it, right?
So, one of the advantages you get from using Apps Script
is that you don't have to build all this in yourself like you
would if you were using a traditional REST-based API.
Apps Script handles all this for you.
And you can see that here where it says, hey, you know what?
This script needs authorization before it
can get access to your data.
So once you click on this, you'll
get your very familiar OAuth2 dialogue
that basically says you as a user need to give permission
to access your Google Sheet.
See, it says here "View and Manage Your Spreadsheets
in Google Drive."
Obviously, we want that to happen because we're
trying to automate things for ourselves.
So we click Accept.
And once that's done, it's actually
going to run your script, and you can see the little dialog
box up here that says it's running.
And once it's done, it disappears.
Hopefully, when you go to your spreadsheet
you'll see that we have Hello World in cell A1,
just as we had planned.
All right.
Now, if for some reason your browser window is not that wide
or you want to make your code a little easier to read,
you can do the JavaScript shortcut
way of putting each method call on a separate line.
It's perfectly fine.
I can do that here.
Let's just change our script a little bit
because I want to run it again to show that it did work.
All right.
So let's click on Run again.
And once that's done, we'll go back to the spreadsheet
and see, hey, we've got Hello World 2.
So that's it.
You've just done your first Hello World program
using Google Apps Script.
Congratulations.
All right.
Now that you've got Hello World under your belt,
I'm sure you're anxious to script some more.
Here you'll find a link to the Apps Script documentation,
which includes tutorials as well as Quick Start projects
to get you going.
Also, stay tuned for more Apps Script episodes coming soon.
If you need help, go to Stack Overflow
and search for Google Apps Script with hyphens
in between each word.
All right.
Now, can you believe you haven't heard of Google Apps Script
before?
Yeah, neither can I, but now the secret's really out of the bag,
isn't it?
All right.
This is Wesley Chun from Google, and I look forward
to seeing some awesome Apps Scripts from you.
Thanks for tuning in to this episode of the Launchpad
Online.