Merge images in Google Slides using Apps Script
In this tutorial, I will show you how to merge an image into a Google Slides presentation using Google Apps Script.
Automatically adding images to a presentation can be useful in many situations. For example, if you're a salesperson who likes to personalize the presentations that you create for your clients, you might want to add their logos to the presentation. It can be cumbersome to manually add these each time you make a presentation. You can become a lot more productive by automating this process and I will show you how to do that in this tutorial.
Prerequisites
This tutorial assumes that you're familiar with:
Here is what we want to do. Let's imagine that we have a presentation template that we've created in Google Slides. Maybe you have a title slide where you've created a placeholder for the logo. This placeholder is simply a rectangular shape inserted into the presentation with the text {{logo_image}}
in it.
What we want to do is replace that placeholder with the actual logo image using Apps Script.
Here is the code to do that:
Note
Remember to replace <SLIDES_ID>
in the code with the actual Id of your Google Slides presentation. You can get the Id from the URL of the presentation.
https://docs.google.com/presentation/d/<SLIDES_ID>/edit
Also, please replace <LOGO_URL>
in the code with the URL of the image you want to merge into the presentation. This assumes that your company (or your client's company) has made their logo available at a publicly accessible URL. Also, please ensure that you have the rights to use logos or any other images that you plan to merge using this script.
function mergeImageIntoGoogleSlides() {
// Id of the presentation
var templateId = "<SLIDES_ID>";
//Logo url
var logoUrl = "<LOGO_URL>";
// Create the request to replace shapes in the
// presentation with the logo. Any shape with the text
// {{logo_image}} in it will be replaced with the image.
var mergeImageRequests = [{
replaceAllShapesWithImage: {
imageUrl: logoUrl,
containsText: {
text: '{{logo_image}}'
}
}
}];
// Send the request to merge the logo into the presentation
Slides.Presentations.batchUpdate({
requests: mergeImageRequests
}, templateId);
}
Important: Activate the Slides API
The code to merge images relies on an advanced API that is OFF by default in Apps Script. If you run the above code without turning it ON, you will receive an error saying "Slides is not defined".
Here are the steps to turn ON the advanced Slides API:
Click the + next to Services on the sidebar
Select Google Slides API and click Add
Here is a video demonstrating the above steps:
When you run the above function, the placeholder will be replaced with the image that you specified via logo_url
.
Conclusion
In this tutorial, I show you how to merge an image into a Google Slides presentation using Apps Script. Thanks for reading!
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!