Center images in Google Slides using Apps Script
In this tutorial, I'll show you how to center images in a Google Slides presentation using Apps Script. Consider the following presentation that has three slides in it. Each slide contains an image that is not centered. Our goal is to use Apps Script to center these images.
Slide 1
Slide 2
Slide 3
Prerequisites
This tutorial assumes that you're familiar with:
Here is the code to center images in a Google Slides presentation using Google Apps Script.
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
function centerAllImagesInSlide() {
// Load the presentation
var presentation = SlidesApp.openById("<SLIDES_ID>");
// Get the width and height of the presentation
var pageWidth = presentation.getPageWidth();
var pageHeight = presentation.getPageHeight();
// Load the slides in the presentation
var slides = presentation.getSlides();
// For each slide, center the images found on the slide
// Note: if a slide contains more than a single image, they will
// overlap when you center them.
slides.forEach(function(slide) {
var images = slide.getImages();
images.forEach(function(image) {
image.setLeft(pageWidth/2 -image.getWidth()/2);
image.setTop(pageHeight/2 - image.getHeight()/2);
});
});
}
When you run the centerAllImagesInSlide()
function, the images in your Google Slides presentation will be centered.
Before | After |
---|---|
Conclusion
In this tutorial I showed you how to center images in a Google Slides presentation using Google Apps Script. Hope you found this tutorial helpful. Thanks for reading!