[MUSIC PLAYING]
WESLEY CHUN: Hey, developers, and welcome to the G Suite Dev
Show.
I'm your host, Wesley Chun.
Today we're going to explore how to add
text and shapes to presentations using the Google Slides API.
As a bonus, we'll also show you how to add text into shapes.
When you create a brand-new slide deck,
you get one default title site which has got two text boxes--
one for a title and another for a subtitle.
Our sample app will show you how to use the API to fill
in both text boxes.
The core functionality of the app,
however, will be to add a new slide.
We'll use the main point layout, which, as you can see,
gives us a slide with one largish text box
but is otherwise empty.
So we can add some crazy shapes to it.
To complete the demo, we'll also add text
to not only the text box, but also to all the shapes.
Our sample app will be done in Python for brevity.
The client library can be installed
with a single PIP command, or PIP 3
if you're a Python 3 user.
If you're not a Python developer,
just use it as pseudocode to learn with.
Then build your app with any language
supported by the Google API's client libraries.
Check out the link to get the client
library for your language.
Next, create a Google API project
in the developers console with the Slides API enabled,
if you haven't already.
If you want to get a head start before we look at our sample
app, feel free to review the Adding Text and Shapes How-To,
but it's optional.
If you're set, let's go to the computer
and see how to add text and shapes to slides using the API.
We'll start with lines 1 through 16.
This is your standard imports and boilerplate authorization
code, except for line 8 where we define the gen UUID
lambda, which basically calls into a standard library
function to get randomly-created UUIDs, or Unique IDs,
as a string.
We're going to use this down below where
we need unique object IDs.
Line 10 is a read-write scope for slides.
And line 16 is where we create the service end
point for the slides API.
Lines 18 to 24 are where we create
a brand-new presentation, then grabbing the title and subtitle
text box IDs from the default title
slide from the API response that we see from 22 to 24.
Lines 25 to 30 is where we use the function
that we created on line 8 to generate random object IDs.
Now why would you generate your own IDs rather
than getting them from the API?
Because you can use these IDs for the remaining
requests in a single batch.
If you let the API do it, you need
to lay a lot more additional calls--
make a call to create something, then make
another call to get the IDs, then make
another call using those IDs.
All these extra calls chew up your quota.
So it's best to minimize by rolling your own IDs
and using them.
Note that in this app, as we will see,
we only have two API calls total--
one to create the presentation and another one
to do everything else.
If you don't create your own IDs, at least for this app,
you'll need at least five API calls.
Lines 32 to 46 are the requests to create
a new slide for our shapes, as well
as filling in most text boxes.
Specifically, 35 to 42 is where we create the new slide
with a main point layout, passing
in our generated IDs for both the slide and that largish text
box we mentioned earlier.
The placeholder ID mappings field
is an array to provide the IDs for the elements that
are part of that layout.
We only have one item, because the main point slide only
has that one largish text box.
43 to 46 is where we add the title and subtitle
text on that default title slide and the text
for that largish text box on the main point slide.
Now 47 down to 94 is a large block
that creates the three shapes.
The code to create each shape are nearly identical, so let's
chat briefly about them in general.
When placing or manipulating shapes or images on slides,
developers must calculate or otherwise provide the desired
sizes and transforms.
Resizing, rotating, and similar operations
require some basic knowledge of matrix math.
Take a look at the Page Elements page in the documentation,
as well as the Transforms Concept Guide for more details.
But if you're short on time, don't
want to deal with the math, or maybe you're thinking,
I just want to draw a smiley face on a slide,
one common pattern, then, is to bring up the Slides user
interface, create a blank slide, and place your image
or draw your shape the way you want with the size you want,
and putting it exactly where you want.
Once you have that, you can use the API
to do a get call on the presentation
and grab that critical size and transform that you need, just
like what you see here, and then drop them right into your code,
like we did with the Python equivalent
you see here on 47 to 62.
And likewise for the other shapes from 63 to 94.
Since we're showing you how to add text and shapes,
we might as well show you how to add text into shapes.
So that's what the requests on 96 to 98 are for.
Notice that the format is identical as adding text
to the text boxes, like we did above.
And then finally, on lines 100 to 102, send all 10
of these requests in one call to batch update.
Then let the user know everything's done.
Let's double-check that.
One, create slide.
Three, insert text.
1, 2, 3, create shape.
And three more insert text requests make 10 total.
And that's it.
Well, after you've executed the script,
confirm that you've got a new presentation in your Drive
folder that has a slide with the text and shapes as advertised.
So now you know how to add text and shapes to slides,
as well as how to add text into shapes.
If you want to take a closer look at the Python code,
check out the Deep Dive blog post.
Now that you know how to add text and shapes,
perhaps you want to explore formatting that text.
The first link takes you to that blog post and video.
Well, what if you have a bunch of text placeholders in a slide
template so you can generate custom decks for each
of your clients?
Well, the second link points you to that post and video.
Finally, if you're brand new to the Slides API,
check out the overview introducing you
to the API basics concepts that you need to know.
If you're writing applications to generate
slide presentations, there's a good chance
you'll be adding text.
But on the off-chance you want to add
shapes or text in shapes, you now know how to do that, too.
We're excited to share these features from the Google Slides
API with you and invite you to check out
more videos in our library, whether for Slides
or other Google APIs and developer tools.
Be sure to subscribe to our channel
to get the latest updates from all of the Google Developers
teams.
This is Wesley Chun from Google, and we'll see you upstairs
in the G Suite.
Hey, everyone.
Thanks for watching.
Check out these related videos, and we'll see you next time.
[MUSIC PLAYING]