Google Slides is a powerful tool used to create presentations, documentation, and educational materials. With Google Apps Script, we can use its functionality to make automation, reports, and other customizations. However, when it comes to working with Google Slides, we might need to know various information such as the dimensions of a slide, its background color, shape properties, and so on. In this article, we will cover how to find the dimensions of a Google Slide in pixels with Google Apps Script.
Google Apps Script and Google Slides API
Google Apps Script provides a platform to extend and automate Google Workspace products such as Google Sheets, Docs, Slides, and Forms. We can write server-side JavaScript code to interact with these tools using a JavaScript runtime based on the V8 engine (the same engine that powers Chrome and Node.js). Similarly, Google provides a Slides API that enables us to read, write and manipulate the content and elements of a Google Slides presentation programmatically.
Finding the Dimensions of a Google Slide
To find the dimensions of a Google Slide in pixels, we need to use the Slides API method getPageElements. This method returns an array of all the page elements on the slide. We can filter the array to get only the slide, which has a page element type of SLIDE. Then, we can get the width and height of the slide in pixels from its size property.
Here is the code snippet that finds the width and height of the first slide of a presentation.
function getSlideDimensions() {
// Get the current presentation.
var presentation = SlidesApp.getActivePresentation(); // Get the first slide of the presentation.
var slide = presentation.getSlides()[0]; // Get the page elements of the slide.
var pageElements = Slides.Presentations.Pages.getPageElements(presentation.getId(), slide.getObjectId()).pageElements; // Get the slide element from the page elements.
var slideElement = pageElements.filter(function (element) {
return element.shape && element.shape.shapeType == "SLIDE";
})[0]; // Get the dimensions of the slide in pixels.
var width = slideElement.size.width.magnitude;
var height = slideElement.size.height.magnitude; // Print the dimensions of the slide.
Logger.log("Slide dimensions: " + width + "x" + height + " pixels");
}
The above code will first retrieve the active presentation using SlidesApp service. Then, it will get the first slide from the presentation, which is an object of the Slide class. After that, it will use the presentation and slide IDs to get the page elements of the slide using the Slides API method getPageElements. The Slides API response is a JSON object containing an array of page element objects. We can filter this array to get the slide object that represents the slide. Finally, we can access the width and height of the slide using the size property of the slide element object’s length object. The size property is an object that contains the width and height of the page element in a LengthUnit object. We can access the magnitude property of the LengthUnit object to get the value in pixels.