are you a developer who also gets asked
to create Google Forms regularly or who
has a co-worker who does wish you could
use your coding skills to automate this
or you're in the right place as this dev
byte was made just for you
welcome to the launchpad online I'm your
host Wesley Chun if you've ever created
a forum you know it's a great tool for
conducting a survey at the office or
building a quiz for your students within
its editor you can choose from a variety
of question options like multiple-choice
text field or a linear scale but while
you can create one five or even ten
forms this way editing each one by hand
doesn't scale if you need to create
hundreds or thousands so there must be a
better way that way is a developer tool
called Google Apps Script which lets you
build JavaScript apps hosted in the
cloud that interacts with Google Apps
data and other Google services check out
the link to the intro video if you're
new to Apps Script in a moment we're
gonna look at how Apps Script can be
used to create forms in bulk
programmatically to save you the time of
building them manually the source is
available from the post at the link you
see so if you want to code with me copy
it to your clipboard the most
interesting thing about the code is that
it's the first one we're looking at
that's not bound to a Google sheet or
any other document let's go to the
computer now and see how to create these
standalone scripts there are two ways of
creating standalone scripts the first is
by going to your Google Drive and then
selecting new more and then Google Apps
Script if it's in your pull down if not
then click on connect more apps once the
search bar comes up you can type Google
Apps Script select connect and once it's
added you'll see this dialog box and
then the connect button turns green so
you can close it now we can do it again
new more and then Google Apps Script so
that's one way of doing it another way
of doing it is to just go up to the
address bar in your browser and type
script google.com and this just creates
a blank project and drops you straight
into the editor so all we have to do
here is take the code that you copied
earlier and just paste it directly in
here so walking through the source we
can see that we're creating a form for
conference speakers to fill out their
talk information bio and preferences the
first step is to create the form
and the way you do this is with four map
create provide the required name which
we call speaker information form here
and optionally setting it as a title
which we do here once you have a form
all you need to do is add fields the
user should fill out so the first field
is a single line text item which you can
add to the form with add text item set
title will add the name of that item so
you could see it on the form and then
we're going to require our users to
enter this field so that's why we call
set required with true if you want to
give users multiple lines instead of add
text item you would use add paragraph
text item if you want users to choose
from one out of multiple items
aka radio buttons create a JavaScript
array with all the choices then call add
multiple choice item then call set
choice values with your array so that
users actually get presented with those
choices
finally if instead you want checkboxes
where users can select one or more
options you still create an array with
their choices but you would call add
checkbox item and in this last case we
are not going to require the users enter
this so we leave the set required off or
you could also call set required with
false but that's not necessary
once you're done save it call it
something like conference speaker info
all right so that's done so let's run it
all right now that's running the first
thing you see is the oauth2 dialogue as
you expect so the dialogue just requests
permission to access our Google Drive in
other words it needs to create this form
in your Google Drive account and so we
want to allow that so once you click
allow all all succeeds once the
execution bar disappears go to your
Google Drive and there's our speaker
information form let's click on it and
here's our form which you can continue
editing from here or you can click view
live form to actually see the form that
users will get presented with so you've
created a form with for user input
fields all in just 13 lines of code
which of course we broke out into many
more lines to make it more readable
and then one last thing that you need to
do is actually assign a spreadsheet that
will take all the data for users when
they fill out the form and you can do
that directly from the form by clicking
on view responses where you can either
choose to create a brand new spreadsheet
or create a new sheet in an existing
spreadsheet so this is how you would do
it in the UI but if you wanted to do
everything in code at the very bottom
after you've created your form in all
the fields you would just make another
call to form dot set destination and
point to the sheet where the responses
should be held once you do that all the
form responses will go into that sheet
and you're done
looking for even more of a challenge
well your next mission is to put the
data for multiple Google Forms in a
Google sheet a doc or even an external
database then enhance our code snippet
to read in that data and auto-generate
those forms let me know how you do in
the comments below the link on screen
shows one example of what you can do
where do you go from here the first link
takes you to the Apps Script Docs where
you can see an easier example of
creating a Google form programmatically
if you took my challenge a moment ago
and want to create an even more advanced
forms example that uses sheets Docs
calendar and Gmail go to the next link
finally the resources at the bottom will
point you to more episodes in this and
other developer series so we know Google
Forms is a great tool for building
surveys and quizzes but it's not
scalable if you have to create lots of
forms by letting Google Apps Script do
all the heavy lifting you're no longer
constrained because you can write an app
to auto-generate them I hope you learned
something today and don't forget to
subscribe to our Channel this is Wesley
Chun from Google and we'll see you the
next time on the launchpad online
[Music]