In the previous installment (#71), we explored techniques to delete unneeded draft slides and reorder final report pages seamlessly using Slide.remove() and moveSlide(). You likely experienced firsthand how comfortable it is to reorganize presentation decks programmatically in a flash!
Now, for Part 72, our topic is Presentation.getSlides()—one of the most practical and essential methods in Google Apps Script (GAS) for automating Google Slides workflows.
When preparing decks, have you ever found yourself burdened with tedious tasks like these?
- “I need to update the year label from ‘2025’ to ‘2026’ across every single slide…”
- “I have to insert a confidential footer notice saying ‘Sapporo Office Confidential Document’ at the bottom of all 30 slides…”
- “I want to check each slide for specific keywords and change the styling accordingly…”
Flipping through slides manually to apply corrections one page at a time is not only time-consuming, but also a recipe for missed updates and human errors.
By leveraging Presentation.getSlides(), you can retrieve all slides in a presentation deck at once as an array (list). Combining this array with standard programming loop constructs allows you to update dozens or hundreds of slides in an instant!
In this guide, we will use our familiar “Sapporo Office Monthly Performance Report” as a sample project. I will walk you through everything from the fundamentals to practical applications in an approachable, step-by-step manner!
1. What Is Presentation.getSlides()?
Presentation.getSlides() is a Google Apps Script method that fetches all slide pages (represented as Slide objects) contained in a specified presentation as an ordered JavaScript array.
Basic Syntax
// Retrieve all slides in the presentation as an array
var slides = presentation.getSlides();
Think of It as a Photo Album and Individual Pictures
Throughout this series, we have used analogies like houses, rooms, and furniture to organize GAS concepts. When working with Google Slides, imagining a “photo album (presentation)” and “photos (individual slides)” makes it super easy to understand:
- The Entire Photo Album =
PresentationObject (the overall file opened viaSlidesApp.openById(), covered in Part 61) - A Single Photo Inside =
SlideObject (each individual page) - A Stack of Photos = The Array returned by
Presentation.getSlides()
When you call getSlides(), GAS pulls every photo out of the album and hands it to you in an ordered box (array): [1st photo, 2nd photo, 3rd photo, ...].
Once you have this array of slides, you can use JavaScript loops like for or forEach to process them continuously: “Process Slide 1 ➡ Process Slide 2 ➡ Process Slide 3…” completely on autopilot!
2. Key Differences and Use Cases: Presentation.replaceAllText() vs. getSlides()
Attentive readers might have a quick question at this point:
“Wait, if we just want to replace text across all slides, didn’t we learn
Presentation.replaceAllText()back in Part 62? Why do we need a loop?”
Great observation! That is a very sharp point.
If your objective is purely simple global text replacement across the entire deck, Presentation.replaceAllText() does the job nicely in one line. However, in practical workplace automation, many advanced tasks can only be achieved by iterating through slides using Presentation.getSlides().
Let’s compare the two approaches side by side:
| Comparison Item | Presentation.replaceAllText() (Part 62) | Presentation.getSlides() + Loops (This Post) |
| Scope of Execution | Entire presentation (batch) | Slide by slide (individual control) |
| Simple Text Replacement | Yes (super concise, one line) | Yes (can be executed inside a loop) |
| Conditional Logic | No (replaces uniformly across all pages) | Yes (e.g., target page 3 only, or only pages containing a keyword) |
| Dynamic Shape / Footer Insertion | No (text substitution only) | Yes (insert text boxes, shapes, or images at specific coordinates) |
| Progress Logging | No (cannot track slide-level modification details) | Yes (detailed logs like “Slide X updated successfully”) |
In short, professional developers choose the right tool for the job: use Part 62’s replaceAllText() for quick, global string replacements, and use getSlides() with a loop whenever you need slide-by-slide conditions, dynamic footer placement, or detailed logging!
3. STEP 1: Preparation – Create the Sapporo Office Monthly Performance Report
Let’s prepare a sample Google Slides deck to test our code together!
Instructions
- Open Google Drive in your browser and create a new Google Slides presentation.
- Set the presentation title to “Sapporo Office Monthly Performance Report 2025”.
- Add slides so that you have 3 (or 4) pages, and enter some test text on each page.
【Sample Slide Structure】
- Slide 1 (Title Slide)
- Title:
Sapporo Office 2025 Performance Review - Subtitle:
Prepared by: Sapporo Office Operations Management Dept.
- Title:
- Slide 2 (Development Dept. Results)
- Title:
Development Dept. 2025 Highlights - Body:
In 2025, regional map application development based in Sapporo progressed smoothly.
- Title:
- Slide 3 (Sales Dept. Results)
- Title:
Sales Dept. 2025 Revenue Performance - Body:
Target achievement rate for 2025 Q4 reached 120%.
- Title:
- Once configured, copy the Presentation ID from the browser URL bar.
- URL pattern:
https://docs.google.com/presentation/d/[YOUR_PRESENTATION_ID_HERE]/edit
- URL pattern:
4. STEP 2: [Hands-On] Retrieve All Slides and Run a Basic Loop
Let’s write a foundational script that fetches all slides using Presentation.getSlides() and logs the total slide count alongside each slide’s unique ID.
1. Open the Google Apps Script Editor
Open a standalone script (or the Apps Script editor attached to your project) and paste the code below. Be sure to replace YOUR_PRESENTATION_ID_HERE with your actual presentation ID.
function checkAllSlides() {
// 1. Open presentation by ID (refer to Part 61)
var presentationId = "YOUR_PRESENTATION_ID_HERE";
var presentation = SlidesApp.openById(presentationId);
// 2. Fetch all slides in the presentation as an array
var slides = presentation.getSlides();
// 3. Log total number of retrieved slides
Logger.log("■ Total number of slides: " + slides.length);
// 4. Iterate over slides one by one using a for loop
for (var i = 0; i < slides.length; i++) {
var slide = slides[i]; // Extract the i-th Slide object
var slideNumber = i + 1; // 1-based page number for human readability
// Output slide ID to the execution log
Logger.log("• Slide " + slideNumber + " ID: " + slide.getObjectId());
}
}
2. Check Execution Results
Save the script and run the checkAllSlides function. If the Execution Log at the bottom of the editor displays output similar to this, you are in great shape:
Info ■ Total number of slides: 3
Info • Slide 1 ID: g123456789_0_1
Info • Slide 2 ID: p2
Info • Slide 3 ID: p3
Key Code Points Explained
slides.length- A standard JavaScript array property that counts the number of elements (i.e., slides) in the array.
for (var i = 0; i < slides.length; i++)- The classic
forloop. Because array indexes are zero-based,istarts at0and increments up toslides.length - 1.
- The classic
var slide = slides[i];- Extracts the individual
Slideobject stored at indexiof theslidesarray and assigns it to the variableslide. You can now execute slide-level methods on this variable!
- Extracts the individual
5. STEP 3: [Practical Application 1] Batch Replace Text Across All Slides (“2025” to “2026”)
Now that you have the basic loop down, let’s tackle batch text replacement across all slides.
Imagine preparing for a new fiscal year where every occurrence of “2025” in your report needs to be updated to “2026”.
1. Enter the Code
Paste the following snippet into your GAS editor and run it:
function updateYearInAllSlides() {
// Open the presentation
var presentationId = "YOUR_PRESENTATION_ID_HERE";
var presentation = SlidesApp.openById(presentationId);
// Retrieve all slides as an array
var slides = presentation.getSlides();
// Define search and replacement strings
var targetText = "2025";
var replacementText = "2026";
Logger.log("=== Starting year update process ===");
// Loop through all slides and replace text page by page
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var pageNum = i + 1;
// Execute replaceAllText on the individual Slide object
var replacedCount = slide.replaceAllText(targetText, replacementText);
Logger.log("Slide " + pageNum + ": Replaced '" + targetText + "' in " + replacedCount + " place(s).");
}
Logger.log("=== All slides updated successfully! ===");
}
2. Verify Execution and Slide Changes
Once executed, your Execution Log will present a summary like this:
Info === Starting year update process ===
Info Slide 1: Replaced '2025' in 1 place(s).
Info Slide 2: Replaced '2025' in 2 place(s).
Info Slide 3: Replaced '2025' in 2 place(s).
Info === All slides updated successfully! ===
Check your Google Slides document! From the title slide through slides 2 and 3, all instances of “2025” have been rewritten to “2026” in a fraction of a second.
Why Is the Return Value of slide.replaceAllText() So Useful?
The Slide.replaceAllText() method returns an integer showing how many text occurrences were replaced on that specific slide.
By storing this count in a variable (e.g., var replacedCount = ...), you can log exact counts per slide. This provides full auditability and ensures that nothing gets overlooked during bulk updates.
6. STEP 4: [Practical Application 2] Automatically Insert a Common Footer on Every Slide
Next, let’s explore where getSlides() truly shines: automatically inserting standardized footer elements across all slides in a deck.
We will programmatically insert a small footer text box reading “Sapporo Office Confidential Document | 2026” in the bottom-right corner of every page.
1. Enter the Code
This snippet combines Slide.insertTextBox() (covered in Part 66) with TextRange styling methods (covered in Part 67):
function addFooterToAllSlides() {
var presentationId = "YOUR_PRESENTATION_ID_HERE";
var presentation = SlidesApp.openById(presentationId);
// Retrieve all slides
var slides = presentation.getSlides();
// Define footer text
var footerText = "Sapporo Office Confidential Document | 2026";
// Specify position coordinates and dimensions (in points)
var positionLeft = 450; // Distance from left edge
var positionTop = 360; // Distance from top edge
var width = 250; // Width of text box
var height = 25; // Height of text box
Logger.log("=== Starting footer insertion across all slides ===");
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var pageNum = i + 1;
// 1. Create a new text box on the slide (refer to Part 66)
var textBox = slide.insertTextBox(footerText, positionLeft, positionTop, width, height);
// 2. Style font size and color discreetly (refer to Part 67)
var textRange = textBox.getText();
textRange.setFontSize(9); // Set to 9pt
textRange.getRuns().setFontColor("#888888"); // Subtle gray font
Logger.log("Inserted footer onto Slide " + pageNum + ".");
}
Logger.log("=== Completed footer placement across all slides! ===");
}
2. Confirm Execution
Run the function, and an aligned, clean footer will appear at the exact same coordinates across every slide in your presentation!
Manually copying, pasting, and aligning text boxes across a lengthy presentation is exhausting, but Google Apps Script completes it with pinpoint precision in seconds.
7. Advanced Technique: Writing Clean Code with forEach Loops
While standard for (var i = 0; i < slides.length; i++) loops work reliably, JavaScript offers the cleaner forEach method tailored specifically for arrays.
Using forEach eliminates manual counter index management and makes your script more readable and modern.
Refactoring with forEach
function addFooterModern() {
var presentation = SlidesApp.openById("YOUR_PRESENTATION_ID_HERE");
var slides = presentation.getSlides();
// Iterate cleanly using forEach
// (slide, index) automatically receives the Slide object and zero-based index
slides.forEach(function(slide, index) {
var pageNum = index + 1;
// Perform slide-level operations
slide.replaceAllText("Old Company Name", "New Company Name");
Logger.log("Completed processing for Slide " + pageNum + ".");
});
}
Both for and forEach perform identically under the hood, so feel free to choose whichever style feels more comfortable and readable to you!
8. Common Traps for Beginners and How to Fix Them
Here are four common pitfalls beginners encounter when looping through slides with Presentation.getSlides(), along with actionable fixes.
Trap 1: Calling Slide Methods Directly on the Array
This is the single most frequent mistake when starting out:
// ✕ Incorrect example (causes a TypeError)
var slides = presentation.getSlides();
slides.replaceAllText("2025", "2026");
// Error: slides.replaceAllText is not a function
【Cause & Solution】
The variable slides returned by getSlides() is an array containing slides—not an individual slide itself. An array does not possess slide methods like replaceAllText().
Always extract an individual Slide object first using var slide = slides[i]; inside a loop before calling slide methods.
Trap 2: Confusing Zero-Based Indexes with Human Page Numbers
Programming arrays are zero-indexed:
slides[0]➡ Slide 1slides[1]➡ Slide 2slides[2]➡ Slide 3
If you log Logger.log("Slide " + i), your first slide will be labeled “Slide 0”, which is confusing when sharing logs with non-engineers.
Remember to calculate user-facing page numbers by adding 1: var pageNum = i + 1;.
Trap 3: Forgetting Trailing Parentheses () on getSlides
GAS methods require parentheses () to trigger execution:
// ✕ Missing parentheses (causes unexpected behavior or runtime failure)
var slides = presentation.getSlides;
// ◯ Correct syntax
var slides = presentation.getSlides();
Omitting parentheses assigns the function definition itself to the variable, causing slides.length to evaluate to undefined and preventing your loop from executing.
Trap 4: Hitting the 6-Minute Execution Timeout on Massive Decks
Standard Google accounts have a maximum execution runtime limit of 6 minutes per script execution.
If you process massive presentations with over 100 slides—especially when creating complex shapes or manipulating heavy images on each slide—the script may hit this timeout ceiling.
【Solutions】
- Process slides in batches (e.g., first 50 slides in one run, remaining slides in another).
- Eliminate redundant logging statements and unnecessary API calls inside the loop.
9. Summary & Next Steps
In this tutorial, we thoroughly covered how to fetch and loop over every slide in a presentation using Presentation.getSlides().
Key Takeaways
Presentation.getSlides()returns all slides as a JavaScript Array- Pairing
getSlides()withfororforEachloops enables full automation across decks of any size - Use
replaceAllText()(Part 62) for simple global text swaps, and usegetSlides()loops for dynamic additions or slide-by-slide logic - Remember that
slidesis an array; extract individual slides viaslides[i]before calling slide-specific methods
Being able to iterate through entire slide decks programmatically elevates your presentation automation capabilities to a professional level!
What’s Next?
In Part 73, we will explore Slide.getNotesPage() to manage speaker notes programmatically!
We will cover high-value automation scenarios like automatically populating executive talking points and supporting data sources into speaker notes across your deck. Stay tuned!
