In our previous session (Episode 70), we learned how to use Slide.insertImage() to neatly insert activity photos from the Sapporo office into designated positions. Adding images instantly transforms presentations into engaging, persuasive slide decks!
Once you can automate populating departmental data and photos to generate slides, the next challenge you will face is finalizing and organizing your slide deck.
- You want to delete “input tutorial” or “draft memo” slides left behind in your template
- You want to eliminate blank pages generated dynamically when there was no data
- You want to reorder the deck so the “Monthly Performance Highlights (Summary)” generated at the end is moved right after the title slide (to Page 2)
While you could always drag and drop or hit the Delete key manually, doing that for recurring weekly or monthly reports is far from efficient.
In this 71st tutorial, we will take a deep dive into two essential Google Apps Script methods: Slide.remove() for deleting slides, and moveSlide() for rearranging slide positions effortlessly!
1. Two Key Methods to Master Today
First, let’s examine the roles and basic syntax of these two new commands.
① Slide.remove() (Deleting Slides)
Completely deletes the specified slide (Slide object) from the presentation.
// Remove the retrieved slide
targetSlide.remove();
- Role: Deletes the target slide
- Return value: None (
void)
② Slide.moveSlide(insertionIndex) (Moving Slides)
Moves the specified slide to a target page position (index) within the presentation.
// Move target slide to the very top (Page 1)
targetSlide.moveSlide(0);
- Role: Reorders the slide to a designated position
- Argument:
insertionIndex(The destination position. A zero-based integer)
2. [Critical] Page Numbers vs. Zero-Based Indexing
When managing slide order in Google Apps Script, the golden rule you must never forget is that index numbers start at 0.
| Actual Page Number | Index (insertionIndex) | Description |
| Page 1 | 0 |
The very beginning of the presentation (Cover slide, etc.) |
| Page 2 | 1 |
Directly after the cover (Executive summary, etc.) |
| Page 3 | 2 |
The 3rd slide |
| Page N | N - 1 |
The last slide position |
For instance, if you want to place a slide right after the cover (Page 2), specify moveSlide(1). Setting it to moveSlide(2) would move it to Page 3, so be careful!
3. STEP 1: Preparation – Setup of the “Sapporo Office Monthly Performance Report”
For this guide, let’s assume we have an automatically generated “Sapporo Office Monthly Performance Report” containing the following 5 slides:
[Current Slide Structure]
Slide 1 (index 0) : [Cover] Sapporo Office Monthly Performance Report
Slide 2 (index 1) : [Draft] *Creation Notes & Guidelines (Unneeded, want to delete!)
Slide 3 (index 2) : [Results] Development Team Performance
Slide 4 (index 3) : [Results] Sales Team Performance
Slide 5 (index 4) : [Summary] Company-wide Highlights & Summary (Important, move to Slide 2!)
Using GAS, we will automate the following two finishing tasks:
- Delete the 2nd slide (“[Draft] Notes”)
- Move the “Company-wide Highlights” slide from the end to right after the cover (Page 2)
4. STEP 2: [Hands-on] Deleting and Reordering Slides
Now, let’s write code and run the cleanup process!
1. Enter the Code
Open your Apps Script editor bound to a Google Sheet or standalone script, and paste the following snippet:
*Be sure to replace YOUR_PRESENTATION_ID with your actual Google Slides ID.
function cleanupAndReorderSlides() {
// 1. Open presentation
var presentationId = "YOUR_PRESENTATION_ID";
var presentation = SlidesApp.openById(presentationId);
// 2. Retrieve all slides as an array
var slides = presentation.getSlides();
Logger.log("Slide count before processing: " + slides.length);
// ==========================================
// Step 1: Remove unneeded draft slide (Slide 2 = index 1)
// ==========================================
var draftSlide = slides[1]; // Get 2nd slide
draftSlide.remove();
Logger.log("Draft slide deleted successfully.");
// Since a slide was removed, re-fetch the updated list of slides
slides = presentation.getSlides();
Logger.log("Slide count after deletion: " + slides.length);
// ==========================================
// Step 2: Move Highlights slide (originally Slide 5 -> currently Slide 4 = index 3) to Page 2 (index 1)
// ==========================================
var summarySlide = slides[3]; // Get current 4th slide (index 3)
summarySlide.moveSlide(1); // Move directly after the cover slide (index 1)!
Logger.log("Highlights slide moved to Page 2 successfully!");
}
2. Code Walkthrough
slides[1].remove();
By calling.remove()on index1(the 2nd slide) of theslidesarray, that slide is cleanly deleted from the presentation.- Re-fetching with
presentation.getSlides()
Deleting a slide shifts the total count and index numbers of subsequent slides. Therefore, callinggetSlides()again to refresh the slide array before subsequent operations is a safe and reliable practice. summarySlide.moveSlide(1);
Inserts the slide into the “index1(Page 2)” position. The slides originally sitting at that position and beyond are automatically shifted backward by one slot.
5. STEP 3: [Practical Application] Dynamically Removing and Moving Slides by Text Search
In business scenarios, you might not know in advance exactly which slide index contains a draft.
Let’s create a more flexible script that searches for specific text (such as keywords in a title) inside each slide to automatically delete or relocate them!
function smartOrganizeSlides() {
var presentation = SlidesApp.openById("YOUR_PRESENTATION_ID");
var slides = presentation.getSlides();
// -------------------------------------------------------------
// Application 1: Automatically delete slides containing "下書き" or "DRAFT"
// *Rule of thumb: Always loop backwards from the end when deleting elements!
// -------------------------------------------------------------
for (var i = slides.length - 1; i >= 0; i--) {
var slide = slides[i];
var pageElements = slide.getPageElements();
// Scan text elements within the slide
for (var j = 0; j < pageElements.length; j++) {
if (pageElements[j].getPageElementType() === SlidesApp.PageElementType.SHAPE) {
var text = pageElements[j].asShape().getText().asString();
if (text.indexOf("下書き") !== -1 || text.indexOf("DRAFT") !== -1) {
Logger.log("Automatically deleting slide " + (i + 1) + " (Draft).");
slide.remove();
break; // Exit element loop since this slide is removed
}
}
}
}
// -------------------------------------------------------------
// Application 2: Find slide containing "ハイライト" (Highlights) and move to Page 2 (index 1)
// -------------------------------------------------------------
// Refresh slides array after deletion
var updatedSlides = presentation.getSlides();
for (var k = 0; k < updatedSlides.length; k++) {
var targetSlide = updatedSlides[k];
var shapes = targetSlide.getShapes();
for (var m = 0; m < shapes.length; m++) {
var shapeText = shapes[m].getText().asString();
if (shapeText.indexOf("ハイライト") !== -1) {
Logger.log("Found 'Highlights' slide. Moving to Page 2.");
targetSlide.moveSlide(1);
break;
}
}
}
}
6. 3 Common Traps for Beginners and How to Solve Them
Here are three common pitfalls developers face when deleting or reordering slides, along with straightforward solutions.
Trap 1: Looping forward (i = 0) during deletion causes index shifts
When looping through slides to delete them using for (var i = 0; i < slides.length; i++), the remaining elements shift forward, throwing off the index count and resulting in skipped slides or out-of-range errors.
- Delete Slide 1 ➡ What was Slide 2 now becomes the new Slide 1 (index 0)
- Next iteration
i = 1➡ Inspects the new Slide 2 (originally Slide 3), completely skipping the newly shifted Slide 1!
[Solution]
Always loop backwards from the end (for (var i = slides.length - 1; i >= 0; i--)) when removing items! Deleting from the end leaves the indices of preceding elements untouched and intact.
Trap 2: Assuming moveSlide() uses 1-based page numbers
If you want to move a slide to Page 3 and write moveSlide(3), it will actually move to Page 4.
[Solution]
Make it a habit to specify the target page number minus 1!
- Page 1 (Beginning) =
moveSlide(0) - Page 2 =
moveSlide(1) - Page 3 =
moveSlide(2)
Trap 3: Trying to manipulate a deleted Slide object
Calling methods like .insertTextBox() or .moveSlide() on a slide variable immediately after executing slide.remove() will throw the following error:
Exception: The object has been deleted.
[Solution]
Once .remove() is executed, that slide becomes inaccessible in memory. Never reference a deleted slide variable again. When manipulating other slides afterward, refresh your array using presentation.getSlides().
7. Summary and Next Episode Preview
In this session, we explored how to use Slide.remove() and moveSlide() to put the finishing touches on your presentations.
Key Takeaways
slide.remove()deletes unneeded drafts and blank slidesslide.moveSlide(index)reorders slides to any desired position- Slide index numbers are 0-based (Page 1 = 0)
- When deleting multiple slides, looping in reverse (from the end) is the golden rule!
With unnecessary slides removed and the critical summary placed right on Page 2, our Sapporo Office Monthly Report presentation looks sharp and professional!
In the next episode (Episode 72), we will explore how to get the most out of Presentation.getSlides().
