[GAS Beginner Series #61] Google Slides Automation: How to Open Presentations and Target Slides with SlidesApp.openById()

Up through the last lesson (Lesson 60), we explored “Phase 6: Google Docs Automation,” learning practical automation skills like generating invoices for our Sapporo office, replacing template text, and exporting PDFs. Being able to manipulate documents freely with Google Apps Script (GAS) dramatically cuts down the time spent creating paperwork!

Starting today, we’re diving into a brand-new chapter: “Phase 7: Google Slides Automation (Lessons 61 to 75)”!

Whether it’s monthly reporting decks for internal meetings, executive presentations, or client pitches, many of you might find yourselves manually creating nearly identical slide decks week after week or month after month. With GAS, you can automatically generate Google Slides from Spreadsheet summaries and update texts or charts in an instant.

In this milestone 61st lesson—our first step into Google Slides automation—we will clearly explain the foundational skills: how to open a target presentation and how to specify and retrieve individual slides (pages) inside it.

We’ll use a sample “Sapporo Office Monthly Performance Report” to walk through each concept step-by-step, including common stumbling blocks for beginners. Let’s master this together!

1. Understanding Google Slides Structure: The House, Rooms, and Furniture Analogy

Before jumping into the code, let’s clarify the overall structure of Google Slides using our familiar analogy of “house, rooms, and furniture.”

Just like Google Sheets in Lesson 2 and Google Docs in Lesson 51, Google Slides follows a distinct hierarchical parent-child structure.

ConceptReal-World AnalogySlides CounterpartPrimary GAS Class / Method
OverallHouse (Entire Building)Entire presentation file (.gslides)SlidesApp.openById() / Presentation
PageRoom (Living Room, etc.)Individual slide (Slide 1, Slide 2, etc.)Presentation.getSlides() / Slide
ElementFurniture & DecorText boxes, shapes, images, tables, charts, etc.Slide.getPageElements() / PageElement

Opening the entire presentation (the house) alone doesn’t tell your script which page (room) or which text box (furniture) to modify.

Therefore, automating Google Slides with GAS always follows these essential steps:

  1. Open the target presentation (house) using SlidesApp.openById()
  2. Target and retrieve the desired slide (room) using methods like presentation.getSlides()
  3. Perform edits (redecorating) on the retrieved slide, such as updating text or adding shapes

Today, we will focus directly on mastering Step 1 and Step 2!

2. Key GAS Methods for Google Slides Covered Today

The methods we will cover fall broadly into two categories: commands to open/target a presentation, and commands to target and retrieve specific slides.

① Methods to Target the Entire Presentation

  • SlidesApp.openById(id)
    Opens a specific Google Slides file (a Presentation object) by specifying its unique file ID.
  • SlidesApp.getActivePresentation()
    Retrieves the presentation currently open when running a container-bound script (via “Extensions” > “Apps Script” inside Google Slides).

② Methods to Target and Retrieve Slides (Pages)

  • presentation.getSlides()
    Retrieves all slides in the presentation as an array.
  • presentation.getSlides()[0]
    Retrieves a specific slide from the array using a zero-based index (e.g., [0] refers to the 1st slide).
  • presentation.getSlideById(slideId)
    Pinpoints and retrieves a specific slide by directly providing its unique slide ID (Page ID).

3. STEP 1: Preparation! Creating the “Sapporo Office Monthly Performance Report Template”

First, let’s prepare a practice Google Slides presentation to control via GAS!

Setup Steps

  1. Create a New Google Slides File
    Open Google Drive in your browser, click “+ New” in the top-left corner, and select “Google Slides” to create a blank presentation.
  2. Rename the File
    Double-click “Untitled presentation” in the upper left and rename it to Sapporo Office Monthly Performance Report Template.
  3. Add 3 Slides
    Press Enter on the left thumbnail sidebar to increase the slide count to three. Add sample titles to each slide so they are easy to identify:
    • Slide 1 (Cover): Enter “Sapporo Office Monthly Performance Report” as the title.
    • Slide 2 (Company Summary): Enter “Monthly Company-wide Performance Summary” as the title.
    • Slide 3 (Department Breakdown): Enter “Departmental Breakdown (Dev & Sales)” as the title.
  4. Find Your Presentation ID
    Check the URL in your browser’s address bar. It looks like this:
    https://docs.google.com/presentation/d/1aB2c3D4e5F6g7H8i9J0kLmA_XYZ12345/edit
    The string between /d/ and /edit (in this example, 1aB2c3D4e5F6g7H8i9J0kLmA_XYZ12345) is your presentation’s unique ID. Copy this down, as we will use it in our script!

4. STEP 2: Hands-on Code: How to Open a Presentation and Select Slides

Once your presentation is set up, let’s open the GAS editor and write some code.

This time, instead of using a bound script from a spreadsheet, let’s create a standalone script from Google Drive (or start a new GAS project) to run this test.

1. Enter the Script

Open the script editor via Google Drive (“New” > “More” > “Google Apps Script”) and paste the following code (make sure to replace YOUR_PRESENTATION_ID with your actual presentation ID from Step 1):

function openAndGetSlideDemo() {
  // ① Open the file (house) by specifying the presentation ID
  var presentationId = "YOUR_PRESENTATION_ID"; // Replace with your actual presentation ID
  var presentation = SlidesApp.openById(presentationId);
  
  // Retrieve and verify the presentation title
  var presentationTitle = presentation.getName();
  Logger.log("■ Opened presentation name: " + presentationTitle);
  
  // ② Retrieve all slides (rooms) inside the presentation as an array
  var slides = presentation.getSlides();
  Logger.log("■ Total number of slides: " + slides.length);
  
  // ③ [Targeting Method A] Select by zero-based array index (1st slide)
  var firstSlide = slides[0];
  Logger.log("■ Slide 1 ID: " + firstSlide.getObjectId());
  
  // ④ [Targeting Method B] Retrieve the 2nd slide
  var secondSlide = slides[1];
  Logger.log("■ Slide 2 ID: " + secondSlide.getObjectId());
  
  // ⑤ [Targeting Method C] Target directly using the slide's unique ObjectId
  var targetSlideId = firstSlide.getObjectId(); // Example using the 1st slide's ID
  var slideById = presentation.getSlideById(targetSlideId);
  Logger.log("■ Slide retrieved by ID is at index position: Slide " + (slides.indexOf(slideById) + 1));
}

2. Detailed Code Walkthrough

  • SlidesApp.openById(presentationId)
    Opens the slide file corresponding to the specified ID and stores it in the presentation variable, allowing programmatic control of the entire presentation.
  • presentation.getSlides()
    Returns all slides contained in the file as an array. You can inspect the slide count using slides.length.
  • slides[0] / slides[1]
    Following standard JavaScript array indexing, the 1st slide is [0] and the 2nd slide is [1]. Remember that it doesn’t start at index 1!
  • slide.getObjectId()
    Each individual slide has a unique identifier (Object ID) assigned to it by Google Slides.
  • presentation.getSlideById(targetSlideId)
    If slide order is subject to change, targeting the page using its slide ID ensures you always access the correct slide regardless of its index position.

5. STEP 3: Initial Execution and Google Authorization

When your code is ready, click the “Save” icon at the top of the editor, ensure openAndGetSlideDemo is selected as the function to run, and click “Run”.

On your first run, a standard Google security Authorization Required popup will appear, just as we saw in Lessons 2 and 51.

Authorization Steps (Quick Review)

  1. Click “Review permissions”.
  2. Select your Google account.
  3. When the “Google hasn’t verified this app” warning appears, click “Advanced” in the lower left.
  4. Click “Go to Untitled project (unsafe)” at the bottom.
  5. Click “Allow” on the permissions screen.

Once authorized, your script will execute immediately!

Checking the Execution Log

If you see output like this in the “Execution log” panel at the bottom, your script ran successfully!

Info    ■ Opened presentation name: Sapporo Office Monthly Performance Report Template
Info    ■ Total number of slides: 3
Info    ■ Slide 1 ID: g123456789_0_0
Info    ■ Slide 2 ID: g123456789_0_1
Info    ■ Slide retrieved by ID is at index position: Slide 1

You can see that GAS opened the specified presentation and accurately recognized and identified each individual slide page!

6. Summary: Methods to Target Presentations vs. Slides

When working with Google Slides in GAS, there are multiple ways to target the entire presentation file and individual slide pages. Let’s summarize them so you can choose the best approach for each scenario.

Methods to Target the Presentation (Whole)

MethodFeatures & Use CasesCode Example
SlidesApp.openById(id)[Standard & Recommended] Open by file ID from anywhere. Best for automated batch routines.SlidesApp.openById("ID...")
SlidesApp.openByUrl(url)Opens a presentation directly by full URL.SlidesApp.openByUrl("https://...")
SlidesApp.getActivePresentation()Used exclusively inside container-bound scripts opened from the presentation itself.SlidesApp.getActivePresentation()

Methods to Target Slides (Pages)

MethodFeatures & Use CasesCode Example
presentation.getSlides()[i][Standard] Target by index (0 = 1st slide). Ideal when processing slides sequentially in loops.var slide1 = presentation.getSlides()[0];
presentation.getSlideById(id)Target directly using the slide’s unique ID. Safe against changes in slide reordering.var slide = presentation.getSlideById("g123...");
presentation.appendSlide()Creates and appends a new blank slide at the end, returning that new slide.var newSlide = presentation.appendSlide();

7. 4 Common Pitfalls for Beginners and How to Fix Them

Here are four frequent mistakes developers encounter when starting out with Google Slides automation, along with their solutions.

Pitfall 1: Passing the Full URL Instead of Just the ID

Passing the entire URL into SlidesApp.openById("https://docs.google.com/presentation/d/1aB2c3...").

  • Symptom: Throws Exception: Invalid argument: id.
  • Solution: openById only accepts the alphanumeric string between /d/ and /edit. If you prefer using the full web address, use SlidesApp.openByUrl() instead.

Pitfall 2: Forgetting the Zero-Based Array Index

Writing slides[1] intending to retrieve the 1st slide.

  • Symptom: Retrieves the 2nd slide instead of the 1st.
  • Solution: JavaScript array indexes always start at 0.
    • Slide 1 ➡ slides[0]
    • Slide 2 ➡ slides[1]
    • Slide N ➡ slides[N - 1]

Pitfall 3: Mixing Up SpreadsheetApp or DocumentApp Methods

Switching between Sheets, Docs, and Slides often leads to muscle-memory typos in service names.

  • Symptom: Throws errors such as TypeError: SlidesApp.getActiveSpreadsheet is not a function.
  • Solution: The root service for presentations is SlidesApp, which returns Presentation and Slide objects. Use auto-complete (Ctrl + Space) to ensure you pick the correct methods.

Pitfall 4: Referencing an Out-of-Bounds Index

Accessing slides[3] (attempting to grab a 4th slide) when the presentation only contains 3 slides.

  • Symptom: Throws TypeError: Cannot read properties of undefined.
  • Solution: Check slides.length prior to access, or keep iteration loops within safe boundaries.

8. Summary and Next Lesson Preview

In this first lesson of “Phase 7: Google Slides Automation,” we learned the fundamentals of targeting presentations (SlidesApp.openById) and selecting specific slides (getSlides()[0] and getSlideById)!

Key Takeaways Today

  1. Google Slides follows a hierarchy: Presentation (House) > Slide (Room) > Page Element (Furniture).
  2. Use SlidesApp.openById("ID") to open the presentation file.
  3. Use presentation.getSlides() to retrieve all slides as an array.
  4. Select the first slide using index 0: slides[0].
  5. Use getSlideById() to pinpoint a slide without being affected by slide order changes.

Now that you have the foundation to open presentations and select slides, our next step is to batch-replace and update text across the deck!

In Lesson 62, we will cover Presentation.replaceAllText(): How to Batch-Update Placeholders like “{Target Month}” or “{Office Name}” Across Your Slides.

We’ll explore how to replace template placeholders (such as {Target Month}) with current data in seconds—a true productivity booster for recurring reports. Stay tuned!