Empowering Google Slides with Apps Script: A Comprehensive Guide to Inserting Shapes
Hello readers,
Today we will see how we can insert various types of shapes using Apps Script in Google Slides, so let’s get started.
If you are new to apps script and don’t know how and where to write the script for the same, refer to my previous article. In that article I have shown how to write the script in sheets file so instead of that you can create a new presentation (google slides) file and write the code inside its editor.
Initially when you run the script, it will ask for some kind of permissions as well so that all I have explained in the above article. Once you know how to go further, let’s proceed further and dive into coding.
I have created a new presentation file and inside that I have opened my apps script code editor, so from here I would be executing the code.
Below is the complete code for inserting various shapes in google slides using apps script. In this article, I have covered 5 shapes (Circle, Rectangle, Rounded Rectangle, Square and Triangle). There are more number of shapes available in slides so you can try it out once you get the idea about how we can insert shapes.
Complete Code:
function testInsertShapesObj() {
// left and top positions are kept same in our case
let leftAndTopPos = 80;
// Inserting CIRCLE
InsertShapesObj.insertShape(SlidesApp.ShapeType.ELLIPSE, leftAndTopPos, leftAndTopPos, 200, 200);
// Inserting RECTANGLE
InsertShapesObj.insertShape(SlidesApp.ShapeType.RECTANGLE, leftAndTopPos, leftAndTopPos, 300, 180);
// Inserting ROUNDED RECTANGLE
InsertShapesObj.insertShape(SlidesApp.ShapeType.ROUND_RECTANGLE, leftAndTopPos, leftAndTopPos, 300, 180);
// Inserting SQUARE
InsertShapesObj.insertShape(SlidesApp.ShapeType.RECTANGLE, leftAndTopPos, leftAndTopPos, 200, 200);
// Inserting TRIANGLE
InsertShapesObj.insertShape(SlidesApp.ShapeType.TRIANGLE, leftAndTopPos, leftAndTopPos, 300, 200);
}
var InsertShapesObj = {
insertShape: function(shapeType, x, y, width, height) {
try {
let pptId = "YOUR_PPT_ID";
let presentation = SlidesApp.openById(pptId);
let slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
/**
* insertShape() - Inserts shape into google slide.
* @params {shape_type, left_pos, top_pos, width, height}
* @returns {shape} - The shape as required.
*/
var shape = slide.insertShape(shapeType, x, y, width, height);
// Can apply various styles as well to the shapes
shape.getFill().setSolidFill("#3498db");
Logger.log("Shape: " + shape.getShapeType() + " inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting shapes: " + err);
}
}
};
insertShape() - this method is the one that will help us to add any kind of shape into our slides. this accepts 5 parameters:
- shapeType: the type of shape that we want to add like Rectangle, Circle, etc. By default, apps script provides various shape types that you can see in the auto-suggestions box when you type in the editor.
- x: the position of shape from left.
- y: the position of shape from top.
- width: the width of the shape.
- height: the height of the shape.
Basically the code will create new slide for each shape and each shape will get added into a slide accordingly. Total 5 slides will be created as we are calling the method 5 times to insert 5 different shapes.
Output:
So that was all about how we can insert various shapes in Google Slides using Apps script and make our presentations shape-ful ( I mean beautiful :) ).
Do let me know your thoughts in the comments section and feel free to provide suggestions if any.