Enter the URL of the YouTube video to download subtitles in many different formats and languages.
CLAUDIO CHERUBINO:\nHi, everyone.
Welcome back to Google\nDevelopers Live.
And I'm one of the engineers in\nthe Google Drive Developer
Today I'm going to talk about\ntwo of the Google products I
like most, Google Drive\nand Apps Script.
If you don't know Apps Script,\nApps Script is basically
You can write your own\nJavaScript code and have it
run on the Google Cloud and\naccess our services or third
It is basically JavaScript,\nso you probably
It's not a new language\nyou have to learn.
What you might need to learn is\nhow to use the services we
have, and we have a service that\ncan be used to access and
manage your Google\nDrive files.
And this is what I'm going\nto talk about today.
I'm going to show you how to get\nstarted with Apps Script
and how to use Apps Script to\ncreate a file in Drive, share
it, check its properties,\nand so on.
If you want to know more about\nApps Script, the good starting
point is our documentation,\nwhich is at developers.googl
And this is the page that\nI have open right now.
So this is the main page for\nthe Google Apps Script
It has all the news and updates\non the new features of
And it has a complete\ndocumentation set for all the
services and all the functions\nyou can use with Apps Script.
One of those is called DocsList\nService, and that's
the one we can use to access\nGoogle Drive files.
If we check the documentation\nfor it, which I have it open
here, it exposes some basic\nclasses, including a main
service called DocsList, and\nan object to represent the
file, a folder, or\na file type.
I'm going to show you\nhow to use them.
To write a script, your Google\nApps Script, you go to
I'm going to open a new tab,\ngo to script.google.com.
And what I see is this first\ndialogue that asks me if I
I want to start with\na tutorial.
So if you're new, you might\nwant to start from there.
And if you want to create a new\nscript, you might want to
start with a blank project,\nso an empty file.
Or there's some templates\nwe have for you
So I'm going to start\nwith this.
And this will make our life\nmuch, much easier.
And I get a new script and this\nweb IDE, I'm going to
talk about it a bit more\nin a few minutes.
And we have two functions\nwe can already use.
One of those lists files\nin a folder.
And the other one moves\na file into a folder.
And obviously, we\ncan add our own.
I think the best way to get\nstarted is to check one of
Let's check the first one called\nList Files In Folder.
The Apps Script code\nis JavaScript.
And it's very, very easy to\nread, even if you don't know
So basically, what we're doing\nhere in line 10 is that we are
asking the DocsList service to\nget a folder by its name.
So if we want to use it in our\nDrive account, we have to
replace the name Your\nFolder with the name
So let me check what I have\nin my Drive account.
Let's say we're going to use\nTestFolder, which is here
which contains two super\nimportant files.
So, let's go back to Apps Script\nand replace Your Folder
with TestFolder, which is\nthe name of my folder.
With this line, we get a\nhandle to the folder.
And then the next line, line 11,\nwe get a list of all the
What the rest of the function\ndoes is iterating through all
So first, I'm going to save it\nbecause I changed something.
This is the first time I saved\nthis project, so I have to
Default name is Untitled\nProject.
And it is going to be available\nin my Drive account.
So any time, I can go back to\nit, open it, edit, use it
If I want to run it, I have to\nselect the function that I
And we decided we're going to\nuse List Files In Folder.
And now I have these two new\nbuttons that I can use.
And the other one is\nto debug the code.
Because this web IDE also allows\nyou to run code in
debugging mode, which means you\ncan put [INAUDIBLE], you
can inspect the values of the\nvariables, and so on.
I'm not going I show you that.
But that is covered very, very\nwell in the documentation.
And we also have some other\nvideos showing how to use the
This is the first time\nwe run this code.
So Apps Script realizes that\nwe never authorized this
script to have access\nto my documents.
So it automatically checks the\ncode that I'm running and
identifies all the requirements\nI have.
And the only requirement I have\nto run this script is to
give access to my docs\nin a read/write mode.
In this script, in this\nfunction, we're only reading.
But the other function\nis also writing.
And since one function can call\nanother, the whole script
has to be authorized for both\nreading and writing.
This is only required the first\ntime you run the app or
anytime you change something in\nthe code that requires new
I didn't write any authorization\ncode.
This makes Apps Script very,\nvery easy to get started with
I can close this window, go back\nto the IDE, and run it.
As I said, this function will\nlist all files in a folder and
I won't see anything unless\nI check the log.
It's very, very convenient for\ndebugging when you want to run
something and log in\nwhat's going on.
And it's available in the\nView menu and the logs.
So what I'm expecting to see\nhere is two lines with the
names of my files, Really\nNew and Untitled.
And if we go back to my\nTestFolder, these are exactly
We did that in less than\n10 lines of code.
But we can also do a much,\nmuch more with
All of the methods are\ndocumented in the
documentation page that\nI showed you before.
autocompletion, syntax aligning.
So it's very easy to\nwrite new code.
Let's try to write\na new function.
Let's say I want to create a\nfile on Drive and share it
I'm going to define\na function.
This is JavaScript syntax,\ncreateFile.
And now you should pass\nparameters to the functions
But this time we're going\nto hard code something.
Then, obviously, you\ncan make it better.
With createFile, the first\nthing we want to do is
So let's say var file equals\nDocsList dot--
as soon as I type the dot,\nI get autocompletion.
And I get suggestions on all\nthe methods that I can use.
So there are three overloads\nfor createFile.
Let's say I choose this one\nthat takes the name of the
file and the string\nfor the content.
You can get all the details\nfrom the documentation.
So this autocompletion puts the\nmethod signature there.
I just have to replace\nthe place holder.
And the other one is contents,\nwhich is another string which
is going to be the text\ncontent for the file.
So this is a new Drive\nfile that I
If we run this, this\nwill create a file.
So we can do more things\nin a function.
Obviously, you should separate\nfunctionalities in multiple
functions, abstract each one\nfrom the other, you know that.
So let's do more in\nthe same function.
The first thing I want to do is\nto get the file ID from the
file I just created, because\nthe file ID is the unique
identifier that allows me to do\nmore things with this file.
So I'm going to sort\nit in a variable.
As you're going to see, I type\ndot, and I get autocompletion
for file, which is a\ngeneric variable.
But Apps Script realizes that\nas that is the output of the
createFile method, this is going\nto be a file object.
So this is very smart\nautocompletion.
I'm going to get multiple\nvarious methods I can use.
And the one I want\nis called getID.
So I have a file ID\nin the variable.
And if we want to prove\nit, we can log it.
Logger.log, and I\ncan log file ID.
This code will create a file,\nget the file ID of the file we
But I said we also\nwant to share it.
Sharing a file with Apps Script\nis very, very easy.
All you have to do is to call\nthe add viewers or add editors
And I can use an email\naddress or a user.
Or I can add multiple email\naddresses at the same time.
So let's say I'm going to\nuse an email address.
And let's put another\ntest account I
If we want to check if that\nworks, well, we can obviously
go back to the UI and see who\nthe file is shared to.
But we can also do that\nprogrammatically.
So let's extend this code to get\nall the viewers for that
So we can say var viewers equals\nfile.getViewers, which
will give us an array\nof users.
Viewers is going to be\nan array of users.
We can use a follow up to print
So we can do for var I equals 0,\nI less than viewers.length
We can do a Logger.log, viewers\ni dot getEmail.
And we get the email address\nwith that user.
Let's close parentheses,\nsemicolon, Save
So, before we do it, let's see\nwhat is going to happen now.
I'm going to create a new file\ncalled My New Drive File with
I'm going to add another\nuser as Viewer.
And I'm going to log the email\naddresses of all the viewers
So I go back to the drop down\nmenu here and select my new
method, which I call createFile,\nand run it.
I didn't need to give more\npermissions to the document
to the app, because I've already\ngiven read and write
access to the script\nto my Docs account.
So even though I added the new\nfunction, the same set of
permissions I had before is\nenough to run the new code.
Let's check logs\nfrom View Logs.
Well, first line is the ID,\nis this Unix string.
This is the unique ID for\nthe file I just created.
And these two are the email\naddresses of the users who
One of those, the first\none, is my account.
I'm the owner, so I\ncan read the file.
And the second one is the\none that I just added.
Let's see how this gets\nreflected in the Drive UI.
Drive Apps Script, you can\nnotice the icon is the Apps
So this is the project\nthat contains the
And my new drive file is the\ntext file we just created with
So if I open it, it's going\nto be a text file.
This is exactly the content\nI put in it.
And if I go to the Share\ndialogue, I can see that this
and to this other user,\nwho is a viewer, so
I think you all realize how easy\nit is to use Apps Script
There is a lot more we\ncan do with that.
And before I wrap up, I just\nwant to go back to the Docs
for a second and show you\nthat there's so many
Let me mention some of those.
You might be interested in\nknowing what you can do with
So with DocsList, which is\nour main service, you
You can look for files\nusing a query string.
So let's say you know the name\nbut you don't know the ID.
You can get all the files\nthat match that name.
You can get files, give an ID.
Obviously that would mean\nyou know how to get
And you have some convenient\nmethods to get a specific
folder, including the\nroot folder, which
Then you have a file type\nenum, which is used to
categorize each file you have\non Google Drive, and then a
class file, which is\nwhat we just used.
And that class has methods to\nadd editors, to add viewers
as we said, as we used, to get\nall the properties, all the
metadata, from that file,\nincluding the date when it was
created, the ID, when it was\nlast updated, what's the name
of the file, who's the owner,\nif it has any parents, which
means folders it is\nin, size, URL.
And obviously, as you can add\neditors and viewers, you can
You can move a file to\ntrash, and so on.
I'm not going to\nlist them all.
But I strongly advise you to go\nto this page and check what
Remember, if you want to start\nplaying with it, just go to
You can start with a blank\nproject, but if you start with
a dry project, it will be much,\nmuch easier for you to
If you have any questions,\nplease reach out to us on
If not, well, let's see\nyou next time on