[MUSIC PLAYING]
CLAUDIO CHERUBINO: Hi, everyone.
Welcome back to Google Developers Live.
My name is Claudio.
And I'm one of the engineers in the Google Drive Developer
Relations Team.
Today I'm going to talk about two of the Google products I
like most, Google Drive and Apps Script.
If you don't know Apps Script, Apps Script is basically
JavaScript on the cloud.
You can write your own JavaScript code and have it
run on the Google Cloud and access our services or third
party services.
It is basically JavaScript, so you probably
know the syntax already.
It's not a new language you have to learn.
What you might need to learn is how to use the services we
have, and we have a service that can be used to access and
manage your Google Drive files.
And this is what I'm going to talk about today.
I'm going to show you how to get started with Apps Script
and how to use Apps Script to create a file in Drive, share
it, check its properties, and so on.
If you want to know more about Apps Script, the good starting
point is our documentation, which is at developers.googl
e.com/apps-script.
And this is the page that I have open right now.
So this is the main page for the Google Apps Script
documentation.
It has get started guides.
It has tutorials.
It has all the news and updates on the new features of
the product.
It has support channels.
And it has a complete documentation set for all the
services and all the functions you can use with Apps Script.
One of those is called DocsList Service, and that's
the one we can use to access Google Drive files.
If we check the documentation for it, which I have it open
here, it exposes some basic classes, including a main
service called DocsList, and an object to represent the
file, a folder, or a file type.
I'm going to show you how to use them.
To write a script, your Google Apps Script, you go to
script.google.com.
Let's do it.
I'm going to open a new tab, go to script.google.com.
And what I see is this first dialogue that asks me if I
want to create a new project.
I want to start with a tutorial.
So if you're new, you might want to start from there.
And if you want to create a new script, you might want to
start with a blank project, so an empty file.
Or there's some templates we have for you
to get started faster.
And one of those is for Drive.
So I'm going to start with this.
And this will make our life much, much easier.
So I select Drive.
And I get a new script and this web IDE, I'm going to
talk about it a bit more in a few minutes.
And we have two functions we can already use.
They're ready.
They do two things.
One of those lists files in a folder.
And the other one moves a file into a folder.
And obviously, we can add our own.
I think the best way to get started is to check one of
these functions first.
Let's check the first one called List Files In Folder.
The Apps Script code is JavaScript.
And it's very, very easy to read, even if you don't know
Apps Script.
So basically, what we're doing here in line 10 is that we are
asking the DocsList service to get a folder by its name.
So if we want to use it in our Drive account, we have to
replace the name Your Folder with the name
of a folder we own.
So let me check what I have in my Drive account.
I have it here.
I have some folders.
Let's say we're going to use TestFolder, which is here,
which contains two super important files.
So, let's go back to Apps Script and replace Your Folder
with TestFolder, which is the name of my folder.
With this line, we get a handle to the folder.
And then the next line, line 11, we get a list of all the
files in that folder.
What the rest of the function does is iterating through all
the documents in this folder.
And for each of them, log--
which is done on line 18--
log the name of the file.
Let's try to run it.
So first, I'm going to save it because I changed something.
So, Save button.
This is easy.
This is the first time I saved this project, so I have to
give it a name.
Default name is Untitled Project.
We can do better than that.
Let's say Drive AppsScript.
Very original.
And this is now saved.
And it is going to be available in my Drive account.
So any time, I can go back to it, open it, edit, use it
again, and so on.
If I want to run it, I have to select the function that I
want to start with.
So I have two.
And we decided we're going to use List Files In Folder.
So I select this.
And now I have these two new buttons that I can use.
One is to run the code.
And the other one is to debug the code.
Because this web IDE also allows you to run code in
debugging mode, which means you can put [INAUDIBLE], you
can inspect the values of the variables, and so on.
I'm not going I show you that.
But that is covered very, very well in the documentation.
And we also have some other videos showing how to use the
degugger for Apps Script.
So let's say we run it.
This is the first time we run this code.
So Apps Script realizes that we never authorized this
script to have access to my documents.
So it automatically checks the code that I'm running and
identifies all the requirements I have.
And the only requirement I have to run this script is to
give access to my docs in a read/write mode.
It asks for read/write mode.
In this script, in this function, we're only reading.
But the other function is also writing.
And since one function can call another, the whole script
has to be authorized for both reading and writing.
So I authorize the script.
This is only required the first time you run the app or
anytime you change something in the code that requires new
authorization.
So I'm going to authorize.
That's it.
I didn't write any authorization code.
This makes Apps Script very, very easy to get started with
and to write scripts.
So this is now authorized.
I can close this window, go back to the IDE, and run it.
As I said, this function will list all files in a folder and
log their names.
So I run it.
I won't see anything unless I check the log.
The log is available anytime.
It's very, very convenient for debugging when you want to run
something and log in what's going on.
And it's available in the View menu and the logs.
So what I'm expecting to see here is two lines with the
names of my files, Really New and Untitled.
Fancy names.
And if we go back to my TestFolder, these are exactly
the files I have.
So this has worked.
Very, very easy.
We did that in less than 10 lines of code.
This is great.
But we can also do a much, much more with
Apps Script and Drive.
All of the methods are documented in the
documentation page that I showed you before.
But the IDE also gives you
autocompletion, syntax aligning.
So it's very easy to write new code.
Let's try to write a new function.
Let's say I want to create a file on Drive and share it
with someone.
So let's say the function--
I'm going to define a function.
This is JavaScript syntax, createFile.
And now you should pass parameters to the functions,
good programming.
But this time we're going to hard code something.
Then, obviously, you can make it better.
With createFile, the first thing we want to do is
creating a file.
So let's say var file equals DocsList dot--
as soon as I type the dot, I get autocompletion.
And I get suggestions on all the methods that I can use.
So there are three overloads for createFile.
Let's say I choose this one that takes the name of the
file and the string for the content.
There are two more.
You can get all the details from the documentation.
So this autocompletion puts the method signature there.
I just have to replace the place holder.
So one of those is name.
Let's say my new drive file.
And the other one is contents, which is another string which
is going to be the text content for the file.
So this is a new Drive file that I
created with Apps Script.
And that's it.
If we run this, this will create a file.
But we want to do more.
I said we want to share it.
So we can do more things in a function.
Obviously, you should separate functionalities in multiple
functions, abstract each one from the other, you know that.
But still, this is an example.
So let's do more in the same function.
The first thing I want to do is to get the file ID from the
file I just created, because the file ID is the unique
identifier that allows me to do more things with this file.
So I'm going to sort it in a variable.
Var fileID equals file.
As you're going to see, I type dot, and I get autocompletion
for file, which is a generic variable.
But Apps Script realizes that as that is the output of the
createFile method, this is going to be a file object.
So this is very smart autocompletion.
I'm going to get multiple various methods I can use.
And the one I want is called getID.
So I have a file ID in the variable.
And if we want to prove it, we can log it.
Logger.log, and I can log file ID.
This code will create a file, get the file ID of the file we
just created, and log it.
But I said we also want to share it.
So let's add an extra step.
Sharing a file with Apps Script is very, very easy.
All you have to do is to call the add viewers or add editors
methods of the file itself.
So I can do file.addViewer.
And I can use an email address or a user.
Or I can add multiple email addresses at the same time.
So let's say I'm going to use an email address.
And let's put another test account I
have on the same domain.
If we want to check if that works, well, we can obviously
go back to the UI and see who the file is shared to.
But we can also do that programmatically.
So let's extend this code to get all the viewers for that
file and log them.
So we can say var viewers equals file.getViewers, which
will give us an array of users.
Viewers is going to be an array of users.
We can use a follow up to print
information for each one.
So we can do for var I equals 0, I less than viewers.length,
I plus plus.
We can do a Logger.log, viewers i dot getEmail.
And we get the email address with that user.
Let's close parentheses, semicolon, Save,
and we can run this.
So, before we do it, let's see what is going to happen now.
I'm going to create a new file called My New Drive File with
some text content.
I'm going to log the file ID.
I'm going to add another user as Viewer.
And I'm going to log the email addresses of all the viewers
for his file.
So I go back to the drop down menu here and select my new
method, which I call createFile, and run it.
I didn't need to give more permissions to the document,
to the app, because I've already given read and write
access to the script to my Docs account.
So even though I added the new function, the same set of
permissions I had before is enough to run the new code.
It's done.
We can check the log.
Let's check logs from View Logs.
Well, first line is the ID, is this Unix string.
This is the unique ID for the file I just created.
And these two are the email addresses of the users who
have read access to the file.
One of those, the first one, is my account.
I'm the owner, so I can read the file.
And the second one is the one that I just added.
Let's see how this gets reflected in the Drive UI.
I go back to Drive.
I created a file in the root.
So I actually see two things.
Drive Apps Script, you can notice the icon is the Apps
Script project that I created.
So this is the project that contains the
code that I just ran.
And my new drive file is the text file we just created with
the Apps Script code.
So if I open it, it's going to be a text file.
So this is the viewer.
This is exactly the content I put in it.
And if I go to the Share dialogue, I can see that this
file is only available to me--
and I'm the owner--
and to this other user, who is a viewer, so
only has read access.
Great.
I think you all realize how easy it is to use Apps Script
to manage your Drive files.
There is a lot more we can do with that.
And before I wrap up, I just want to go back to the Docs
for a second and show you that there's so many
methods you can use.
Let me mention some of those.
You might be interested in knowing what you can do with
Apps Script and Drive.
So with DocsList, which is our main service, you
can create a file.
You can look for files using a query string.
So let's say you know the name but you don't know the ID.
You can get all the files that match that name.
You can get files, give an ID.
Obviously that would mean you know how to get
that specific file.
You can get folders.
And you have some convenient methods to get a specific
folder, including the root folder, which
might be very useful.
Then you have a file type enum, which is used to
categorize each file you have on Google Drive, and then a
class file, which is what we just used.
And that class has methods to add editors, to add viewers,
as we said, as we used, to get all the properties, all the
metadata, from that file, including the date when it was
created, the ID, when it was last updated, what's the name
of the file, who's the owner, if it has any parents, which
means folders it is in, size, URL.
And obviously, as you can add editors and viewers, you can
obviously remove them.
Some of the methods.
You can rename a file.
You can start a file.
You can move a file to trash, and so on.
There's a lot more there.
I'm not going to list them all.
But I strongly advise you to go to this page and check what
you can do with Apps Script.
OK.
Remember, if you want to start playing with it, just go to
script.google.com.
You can start with a blank project, but if you start with
a dry project, it will be much, much easier for you to
learn about it.
Thank you very much.
If you have any questions, please reach out to us on
Google+ and Stack Overflow.
If not, well, let's see you next time on
Google Developers Live.
Thank you.
Bye bye.
[MUSIC PLAYING]