Exploring Apps Script with Google Slides (with 10 coding examples)

Prashant Agheda
10 min readDec 7, 2023

--

Hello Readers,

Today we will see how we can use Apps script with Google Slides and perform various operations like:

  • Creating new presentation
  • Adding new slide into existing presentation
  • Change background color of specific slide
  • Insert image from URL into specific slide
  • Insert textbox on specific slide
  • Duplicate the slide
  • Delete a slide
  • Insert google sheets table data into slides with its styles and format
  • Insert lines (horizontal and vertical)

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 all the further code examples that I explained above.

1)- Creating new presentation


function test() {
CreateNewPresentationObj.createNewPresentation();
}


var CreateNewPresentationObj = {

createNewPresentation: function() {
try {
var presentation = SlidesApp.create("Newly Created PPT!");
Logger.log("Created presentation with name: " + presentation.getName());
}
catch(err) {
Logger.log("Error creating new presentation: " + err);
}
}

};
  • create() - this method is used to create a new presentation with the name provided inside it using double quotes. In my case it will create a new presentation in google slides with name “Newly Created PPT!”. I have used try catch block to catch the error as well if any.
Newly created presentation highlighted with red border

2)- Adding new slide into existing presentation


function testAddNewSlideObj() {
AddNewSlideObj.addNewSlide();
}


var AddNewSlideObj = {

addNewSlide: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slideLayout = SlidesApp.PredefinedLayout.BLANK;
presentation.appendSlide(slideLayout);
Logger.log("New slide added into presentation...");
}
catch(err) {
Logger.log("Error adding new slide: " + err);
}
}

};
  • openById() - this method is used to open the presentation using the id.
  • appendSlide() - this method is used to append the slide into the newly created presentation that we created. It accepts the layout of the slide that we want to enter. In my case, I have entered the layout of presentation as blank.
Newly created slide added by script

3)- Set BG Color to specific slide


function testSetSlideBGColor() {
SetSlideBGColorObj.setBGColor();
}


var SetSlideBGColorObj = {

setBGColor: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.getSlides()[0];
var bg = slide.getBackground();
bg.setSolidFill(0, 255, 0);
Logger.log("BG color to slide set successfully...");
}
catch(err) {
Logger.log("Error setting BG color to slide: " + err);
}
},

};
  • getBackground() - this method gets the current slide background.
  • setSolidFill() - this method is used to set the BG color as per our need. We can pass the RGB as well as Hex values.
BG Color or 1st slide changed to green

4)- Insert an image from URL


function testInsertImageFromURL() {
var imageUrl = "https://fastly.picsum.photos/id/16/2500/1667.jpg?hmac=uAkZwYc5phCRNFTrV_prJ_0rP0EdwJaZ4ctje2bY7aE";

InsertImageFromURLObj.insertImageFromURL(imageUrl);
}


var InsertImageFromURLObj = {

insertImageFromURL: function(imageUrl) {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.getSlides()[1];
slide.insertImage(imageUrl);
Logger.log("Inserted image from URL successfully...");
}
catch(err) {
Logger.log("Error inserting image from URL: " + err);
}
}

};
  • insertImage() - this method is used to insert an image into the slide.
Image inserted from URL into the slide

5)- Insert textbox in slide


function testInsertTextboxObj() {
InsertTextboxObj.insertTextBox();
}


var InsertTextboxObj = {

insertTextBox: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);

// parameters - text, left, top, width, height
var textBox = slide.insertTextBox("Hello Readers :)", 100, 100, 400, 50);

textBox.getBorder().setDashStyle(SlidesApp.DashStyle.SOLID);
textBox.getText().getTextStyle().setFontSize(28);
textBox.getText().getTextStyle().setFontFamily("Roboto");
textBox.getText().getTextStyle().setForegroundColor("#000000");
textBox.getText().getTextStyle().setBold(true);

Logger.log("Textbox inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting textbox: " + err);
}
}

};
  • insertTextbox() - this method is used to insert textbox in the slide.
  • getBorder() - this method is used to get the current border of the textbox that was added.
  • getTextStyle() - this method is used to get the textstyle of the textbox.
  • setFontSize(), setFontFamily(), setForegroundColor() and setBold(), these are the methods that are used to apply styling to the text inside textbox.
Textbox inserted into slide using script

6)- Duplicate the slide


function testDuplicateSlideObj() {
DuplicateSlideObj.duplicateSlide();
}


var DuplicateSlideObj = {

duplicateSlide: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.getSlides()[2];

slide.duplicate();

Logger.log("Duplicated the slide...");
}
catch(err) {
Logger.log("Error duplicating the slide: " + err);
}
}

};
  • openById() - this method is used to open the presentation by its id.
  • duplicate() - this method is used to duplicate the slide. In our case, we are duplicating the 2nd slide.
Duplicated the 3rd slide (highlighted on sidebar)

7)- Delete an specific slide


function testDeleteSlideObj() {
DeleteSlideObj.deleteSlide();
}


var DeleteSlideObj = {

deleteSlide: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.getSlides()[0];

slide.remove();

Logger.log("Deleted the slide...");
}
catch(err) {
Logger.log("Error deleting the slide: " + err);
}
}

};
  • remove() - this method is used to delete/ remove the specific slide from the presentation.
The 1st slide with Green BG color got deleted by script

8)- Insert google sheets table data into slides with its styles and format

function testInsertTableData() {
InsertTableDataObj.insertTableData();
}


var InsertTableDataObj = {

insertTableData: function() {
try {
var spreadsheetId = "YOUR_SPREADSHEET_ID";
var sheetName = "YOUR_SHEET_NAME";
var pptId = "YOUR_PPT_ID";

var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
var sheet = spreadsheet.getSheetByName(sheetName);
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var data = range.getValues();

var bgColors = range.getBackgrounds();
var fgColors = range.getFontColors();

var presentation = SlidesApp.openById(pptId);
var slide = null;


if(slide == null) {
slide = presentation.insertSlide(0, SlidesApp.PredefinedLayout.BLANK);
}
else {
slide = presentation.appendSlide();
}


var table = slide.insertTable(data.length, data[0].length);


for(var i = 0; i < data.length; i++) {
for(var j = 0; j < data[0].length; j++) {

var cell = table.getCell(i, j);

cell.getText().setText(data[i][j]);
cell.getFill().setSolidFill(bgColors[i][j]);
cell.getText().getTextStyle().setForegroundColor(fgColors[i][j]);

if(i == 0 && j <= 3) {
cell.getText().getTextStyle().setBold(true);
}

}
}

Logger.log("Table data inserted from sheet into slide successfully...");
}
catch(err) {
Logger.log("Error inserting table data from sheet into slide: " + err);
}

}

};
  • insertSlide() - this method is used to insert the slide at specified index.
  • appendSlide() - this method is used to append the slide at last.
  • insertTable() - this method is used to insert table into the slide.
  • getCell() - this method is used to get the cell of specific row and column in from the sheet.
Sample Data in sheet
Table from sheet inserted into slide with format and styling

9)- Insert Horizontal Line


function testInsertHorizontalLineObj() {
InsertHorizontalLineObj.insertHorizontalLine();
}


var InsertHorizontalLineObj = {

insertHorizontalLine: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);

// parameters - lineType, start_left_pos, start_top_pos,
// end_left_pos, end_top_pos
var line = slide.insertLine(SlidesApp.LineCategory.STRAIGHT,
100, 100, 600, 100);

line.setDashStyle(SlidesApp.DashStyle.SOLID);
line.setWeight(5);
line.getLineFill().setSolidFill("#FF0000");

Logger.log("Horizontal line inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting horizontal line: " + err);
}
}

};
  • insertLine() - this method is used to insert the line.
  • setDashStyle() - this method is used to set the dash style of line.
  • setWeight() - this method is used to set the weight of the line.
  • getLineFill() - this method is used to get the line fill and by using this we can set the fill to line.
  • setSolidFill() - this method is used to give the fill color to the line.
Horizontal line added into the slide

10)- Insert Vertical Line


function testInsertVerticalLineObj() {
InsertVerticalLineObj.insertVerticalLine();
}


var InsertVerticalLineObj = {

insertVerticalLine: function() {
try {
var pptId = "YOUR_PPT_ID";
var presentation = SlidesApp.openById(pptId);
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);

var slideWidth = presentation.getPageWidth();
var slideHeight = presentation.getPageHeight();

var x = slideWidth / 2;
var startTop = 50;
var endTop = slideHeight - 50;

// x position will be same as we need vertical straight line
var line = slide.insertLine(SlidesApp.LineCategory.STRAIGHT,
x, startTop,
x, endTop);

line.setDashStyle(SlidesApp.DashStyle.SOLID);
line.setWeight(Config.lineProperties.WEIGHT);
line.getLineFill().setSolidFill(Config.lineProperties.COLOR);

Logger.log("Vertical line inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting vertical line: " + err);
}
}

};
  • getPageWidth() - this method gives the slide width.
  • getPageHeight() - this method gives the slide height.
Vertical line added into the slide

I have explained the whole 10 questions with code separately but if you want to do it in a single file, then below is the complete code for the same. There are 2 code files (SlidesUtility file and Config file) to keep the changes configurable.

SlidesUtility file


function testSlidesUtilityObj() {
// SlidesUtilityObj.createNewPresentation();

// SlidesUtilityObj.addNewSlide();

// SlidesUtilityObj.setSlideBgColor();

// SlidesUtilityObj.insertImageFromURL(Config.IMAGE_URL);

// SlidesUtilityObj.insertTextBox();

// SlidesUtilityObj.duplicateSlide();

// SlidesUtilityObj.deleteSlide();

// SlidesUtilityObj.insertTableData();

// SlidesUtilityObj.insertHorizontalLine();

// SlidesUtilityObj.insertVerticalLine();
}


var SlidesUtilityObj = {


// Create a new presentation
createNewPresentation: function() {
try {
var presentation = SlidesApp.create(Config.PRESENTATION_NAME);
Logger.log("Created presentation with name: " + presentation.getName());
}
catch(err) {
Logger.log("Error creating new presentation: " + err);
}
},


// Add a new slide to an presentation
addNewSlide: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slideLayout = SlidesApp.PredefinedLayout.TITLE_ONLY;
presentation.appendSlide(slideLayout);
Logger.log("New slide added into presentation...");
}
catch(err) {
Logger.log("Error adding new slide: " + err);
}
},


// Set the background color of a specific slide
setSlideBgColor: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.getSlides()[0];
var bg = slide.getBackground();
bg.setSolidFill(255, 0, 0);
Logger.log("BG color to slide set successfully...");
}
catch(err) {
Logger.log("Error setting BG color to slide: " + err);
}
},


// Insert an image from a given URL onto a slide
insertImageFromURL: function(imageUrl) {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.getSlides()[1];
slide.insertImage(imageUrl);
Logger.log("Inserted image from URL successfully...");
}
catch(err) {
Logger.log("Error inserting image from URL: " + err);
}
},


// Add a text box with specified content and styling to a slide
insertTextBox: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);

var textBox = slide.insertTextBox("Hello Readers :)",
Config.textBoxPosValues.LEFT, Config.textBoxPosValues.TOP,
Config.textBoxPosValues.WIDTH, Config.textBoxPosValues.HEIGHT);


textBox.getBorder().setDashStyle(SlidesApp.DashStyle.SOLID);
textBox.getText().getTextStyle().setFontSize(Config.textStyles.FONT_SIZE);
textBox.getText().getTextStyle().setFontFamily(Config.textStyles.FONT_FAMILY);
textBox.getText().getTextStyle().setForegroundColor(Config.textStyles.COLOR);
textBox.getText().getTextStyle().setBold(Config.textStyles.IS_BOLD);

Logger.log("Textbox inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting textbox: " + err);
}
},


// Duplicate a specific slide
duplicateSlide: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.getSlides()[0];

slide.duplicate();

Logger.log("Duplicated the slide...");
}
catch(err) {
Logger.log("Error duplicating the slide: " + err);
}
},


// Delete a specific slide
deleteSlide: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.getSlides()[0];

slide.remove();

Logger.log("Deleted the slide...");
}
catch(err) {
Logger.log("Error deleting the slide: " + err);
}
},


// Insert table data from sheets with format into slides
insertTableData: function() {
try {
var spreadsheet = SpreadsheetApp.openById(Config.SPREADSHEET_ID);
var sheet = spreadsheet.getSheetByName(Config.SHEET_NAME);
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var data = range.getValues();

var bgColors = range.getBackgrounds();
var fgColors = range.getFontColors();

var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = null;


if(slide == null) {
slide = presentation.insertSlide(0, SlidesApp.PredefinedLayout.BLANK);
}
else {
slide = presentation.appendSlide();
}


var table = slide.insertTable(data.length, data[0].length);


for(var i = 0; i < data.length; i++) {
for(var j = 0; j < data[0].length; j++) {

var cell = table.getCell(i, j);

cell.getText().setText(data[i][j]);
cell.getFill().setSolidFill(bgColors[i][j]);
cell.getText().getTextStyle().setForegroundColor(fgColors[i][j]);

if(i == 0 && j <= 3) {
cell.getText().getTextStyle().setBold(true);
}

}
}

Logger.log("Table data inserted from sheet into slide successfully...");
}
catch(err) {
Logger.log("Error inserting table data from sheet into slide: " + err);
}

},


// Insert horizontal straight line with styling
insertHorizontalLine: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);

var line = slide.insertLine(SlidesApp.LineCategory.STRAIGHT,
Config.lineProperties.START_LEFT_POS,
Config.lineProperties.START_TOP_POS,
Config.lineProperties.END_LEFT_POS,
Config.lineProperties.END_TOP_POS);

line.setDashStyle(SlidesApp.DashStyle.SOLID);
line.setWeight(Config.lineProperties.WEIGHT);
line.getLineFill().setSolidFill(Config.lineProperties.COLOR);

Logger.log("Horizontal line inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting horizontal line: " + err);
}
},


// Insert vertical straight line with styling
insertVerticalLine: function() {
try {
var presentation = SlidesApp.openById(Config.PRESENTATION_ID);
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);

var slideWidth = presentation.getPageWidth();
var slideHeight = presentation.getPageHeight();

var x = slideWidth / 2;
var startTop = 50;
var endTop = slideHeight - 50;

// x position will be same as we need vertical straight line
var line = slide.insertLine(SlidesApp.LineCategory.STRAIGHT,
x, startTop,
x, endTop);

line.setDashStyle(SlidesApp.DashStyle.SOLID);
line.setWeight(Config.lineProperties.WEIGHT);
line.getLineFill().setSolidFill(Config.lineProperties.COLOR);

Logger.log("Vertical line inserted successfully with styling...");
}
catch(err) {
Logger.log("Error inserting vertical line: " + err);
}
},

};

Config file


var Config = {

PRESENTATION_NAME : "Newly Created PPT!",
PRESENTATION_ID : "YOUR_PPT_ID",

SPREADSHEET_ID : "YOUR_SPREADSHEET_ID",
SHEET_NAME : "YOUR_SHEET_NAME",

IMAGE_URL : "https://fastly.picsum.photos/id/16/2500/1667.jpg?hmac=uAkZwYc5phCRNFTrV_prJ_0rP0EdwJaZ4ctje2bY7aE",

textBoxPosValues : {
TOP : 100,
LEFT : 100,
WIDTH : 400,
HEIGHT : 50
},

textStyles : {
FONT_SIZE : 28,
FONT_FAMILY : "Roboto",
COLOR : "#000000",
IS_BOLD : true
},

lineProperties : {
START_LEFT_POS : 100,
START_TOP_POS : 100,
END_LEFT_POS : 600,
END_TOP_POS : 100,
WEIGHT : 5,
COLOR : "#FF0000",
},

};

So that was all about how we can work upon Apps script using Google slides explained with 10 examples to get started with.

Do let me know your thoughts in the comments section and feel free to provide suggestions if any.

That’s it for this article, hope you got to learn something new 🙂

Stay tuned for more articles like this one!!!

--

--

Prashant Agheda
Prashant Agheda

No responses yet