How to split Google Slides automatically

In this post, we’re going to see how to split Google Slides automatically into separate files with Apps Script.

Problem and solution

The reason I wrote this script was to resolve a problem at work, where we have an admin system that produces a document with the certificates for the students that are going to leave the school that week.

The problem is that we want to share these electronically with the students individually, and we of course don’t want to send them other people’s certificates. So, I wrote this little script to create a document for each of the students, which could then be shared with them.

Overview of the process

In this example, each certificate has a front page and a back page, as you can see below.

Split Google Slides automatically

On the front page, we have the student’s name and their Spanish level. They are two text boxes on the slide.

The back of the certificate doesn’t have any individual information. In our school, it contains general information about the levels.

We then need a master Slides document, which we will copy to then add each of the student’s data on to. It has two pages which look the same as the document above we’re going to split.

Instead of the student’s name and level, we have two text boxes with placeholders in them, which we’ll replace with the students’ data.

So, the script will get the folder it’s going to store the student files in, get the master file it’s going to copy to create those individual student files, and get the Slides document it’s going to split.

Then, it’s going to loop through the Slides document and get the student’s name and level on every even-numbered slide, and add it to a copy of the master file.

Split Google Slides automatically – Code

Get the files and folder

1. function splitSlides() {
2.   //Get folder and master file
3.   const folder = DriveApp.getFolderById('FOLDER ID HERE');  //*
4.   const masterFile = DriveApp.getFileById('MASTER FILE ID HERE');
5. 
6.   //Get Slides URL from sheet
7.   const ss = SpreadsheetApp.getActiveSpreadsheet();
8.   const shSlides = ss.getSheetByName('SLIDES');
9.   const slidesURL = shSlides.getRange(1, 2).getValue();

L1: Set up the splitSlides function.

L3: Get the folder you want to store the newly-created slides in. Add your folder ID here.

L4: Get the master slides file. Add your file ID here.

L7: Get the active spreadsheet.

L8: Get the sheet called SLIDES.

L9: Get the URL of the slides you want to split, which is in cell B1.

Get the slides to split

11.   //Get data from current slides
12.   const slidesToSplit = SlidesApp.openByUrl(slidesURL);
13.   const slides = slidesToSplit.getSlides();
14.   const slidesPairs = slides.length / 2;

L12: Open the Slides document you want to split.

L13: Get all the slides in the document.

L14: I want to notify the user on the progress of the splitting, and need to know how many slide documents it’s going to create. As I want to split the slides every two slides, I need to divide the total number of slides by two.

Loop through the slides

16.   //Loop through slides
17.   const copiedSlidesData = [];
18.   slides.forEach((slide, s) => {
19.     if (s % 2 === 0) {
20.       ss.toast(`Making slide ${(s+2)/2} / ${slidesPairs}`)
21.       const text1 = slide.getPageElements()[0].asShape().getText()
22.                          .getParagraphs()[0].getRange().asString().trim();
23.       const text2 = slide.getPageElements()[1].asShape().getText()
24.                          .getParagraphs()[0].getRange().asString().trim();

L17: Set up an empty array to store the filename and file URL of each of the newly-created slide documents.

L18: Then we loop through the slides array to make each of the slides documents.

L19: As I only want to get the text on every two slides, i.e. slides 0, 2, 4, etc, we need to check that the current index, s, is divisible by 2.

L20: If there are lots of slides, the process can take a little while so I think it’s good to let the user know the progress. So, here we let the know which slide document we’re currently making out of the total number we’re going to make.

L21-24: We then get the placeholder texts, which are in page elements 0 and 1. Unfortunately, it’s a little long-winded to get at the text in a text box. We get the page element, get it as a shape, then the text, then get the first paragraph, get its range, and then store it as a string.

I’ve also added the trim method to trim any whitespace in case there was any on the student’s account, and it would mean the name wouldn’t be centred on the certificate if it had any.

Copy the master document

26.       //Copy master, rename, and put in folder
27.       const fileName = `Certificado - ${text1}`;
28.       const fileCopy = masterFile.makeCopy(fileName, folder);

L27: We then set the filename using the student’s name in the first placeholder, text1.

L28: Make a copy of the master file and name it with the filename and place it in the folder you set in line 3.

Get the texts you want to copy

30.       //Get text boxes on copy and replace with current text
31.       const slidesCopyDoc = SlidesApp.openById(fileCopy.getId());
32.       const slidesCopy = slidesCopyDoc.getSlides();
33.       slidesCopy[0].getPageElements()[0].asShape().getText()
34.                    .getParagraphs()[0].getRange().setText(text1);
35.       slidesCopy[0].getPageElements()[1].asShape().getText()
36.                    .getParagraphs()[0].getRange().setText(text2);

L31: Open the newly-created Slides document.

L32: Get the slides in the document.

L33-36: Similar to getting the texts from the main document, we get the page elements in the new document, but this time we set the text we want on the first slide.

Note, instead of getting the specific text boxes, we could use the replaceAllText() method here and pass in the names of the placeholders.

Add the filenames and URLs to the Sheet

38.       //Add copied slide details to sheet
39.       copiedSlidesData.push([fileName, fileCopy.getUrl()]);
40.     }
41.   });

L39: We then push the filename and file URL into the copiedSlidesData array.

L40-41: We repeat the process for all the even slides then close the if statement and close the loop.

43.   //Add to copied slides details to sheet
44.   shSlides.getRange(shSlides.getLastRow() + 1, 1, copiedSlidesData.length,
45.     copiedSlidesData[0].length).setValues(copiedSlidesData);
46. }

L44-45: We then add the filenames and URLs of all the slides we’ve made to the SLIDES sheet in one go.

Now, let’s set up the menu to run the script.

Create the menu

48. function onOpen() {
49.   SpreadsheetApp.getUi().createMenu('MAGIC') 
50.     .addItem('🪚Split Slides', 'splitSlides')
51.     .addToUi();
52. }

L48: Set up the onOpen function.

L49: Create the menu called “MAGIC”.

L50: Add the menu item, “Split Slides”.

L51-52: Add it to the UI and close the function.


Run the script

Enter the URL of the slides you want to split in cell B1.

Then, run the script from the MAGIC menu.

This will make a Slides document for each of the students in the folder you set up.

It also lists the files it’s made along with their URLs on the Sheet.

Copy the files

There are three files to use if you want to use the example above:

Google Sheet – Contains the code above

Google Slides -> Slides Splitter – Master Slide: Master slide which is used to create each of the documents

Google Slides -> Slides to split – Example: Slides document to split

For each one you will be prompted to make a copy of the file. I would also make a folder on your Drive to store these and then add the IDs to the script.

Final thoughts

One of the limitations with the Slides service is that there isn’t a copyTo() method like there is with Sheets. Meaning you can’t just grab a slide and copy it to another Slides document.

There are different options to get around this.

For example, you can:

  • reconstruct it (which can be a lot of work)
  • you can copy certain parts and add them to a copied-master, like we did above
  • or you could copy the entire file and delete the slides you don’t need.

The reason I didn’t go with the latter is that I don’t want students being able to go into the revision history and returning the document back to having all the students on it.

A couple of further ideas would be to create URLs that would download the documents as PDFs and these could be emailed to the student, or you could create the PDF on the fly and add it to the email.

Update – I’ve added a second sheet and a second piece of code, to create links which will download a PDF of the certificate. See the script file SlidesSplitterPDF.

As a final note, if you are going to share the URLs with others, make sure they have at least read access to the file.


Find more posts on Apps Script here.

Want to learn more about Google Workspace and Apps Script? The books below are available on Amazon. Just click on a book! (Affiliate links).

JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2