[GAS Beginner Series #70] Insert Images into Google Slides: Slide.insertImage() Tutorial

“Copying and pasting site photos or screenshots into my monthly presentation slides one by one is such a chore…”

“I wish I could automatically insert images into Google Slides and have them positioned perfectly in an instant!”

One of the most unexpectedly time-consuming tasks when building pitch decks and slide presentations is inserting images and adjusting their position and size. When you align them manually by dragging and dropping, millimeter misalignments can be frustrating, and the more images you have, the more tedious it becomes.

The solution to this headache is Google Apps Script (GAS) and its method: Slide.insertImage()!

With this method, you can automatically read image files stored in Google Drive and insert them onto a specified slide at exact coordinates and dimensions.

In this 70th article of our beginner series, we will build a practical script that automatically places Sapporo Office activity photos onto presentation slides. Along the way, we will cover everything from the fundamentals of Slide.insertImage() to techniques for positioning and sizing, as well as common pitfalls to watch out for!

1. What Is Slide.insertImage() in Google Apps Script?

Slide.insertImage() is a method used to insert image data into a specific slide (Slide object) within Google Slides.

You invoke it after opening your presentation with SlidesApp.openById() (which we learned in Episode #61) and retrieving the slide you want to edit.

Basic Code Structure

// 1. Get the target slide
var slide = presentation.getSlides()[0];

// 2. Insert the image into the slide
var image = slide.insertImage(blob);

How to Pass Image Data (What Is a Blob?)

The most important detail about Slide.insertImage() is that the argument inside the parentheses is not an image filename or a URL string.

When embedding images into Google Slides or Google Docs using GAS, you need to provide the image data as a binary chunk known as a Blob.

To extract a Blob from an image file stored in Google Drive, combine it with DriveApp (covered in Episode #41) like this:

// Get the file from Google Drive and retrieve its Blob (raw image data)
var file = DriveApp.getFileById("YOUR_IMAGE_FILE_ID");
var imageBlob = file.getBlob();

// Insert the image by passing the Blob to the slide!
slide.insertImage(imageBlob);

It might sound a bit technical at first, but once you remember the 3-step pattern—“Fetch file with DriveApp ➡ Convert to data with .getBlob() ➡ Insert with slide.insertImage()”—it’s super simple!

2. STEP 1: Preparation! Get Your Slides and Sapporo Office Photo Ready

Now let’s get hands-on and write some code!

We will prepare a monthly Sapporo Office activity report presentation and set up test image data in Google Drive to embed automatically into Google Slides.

Step 1: Create a Google Slides Presentation

  1. Open Google Drive, click “New” > “Google Slides” to create a new presentation.
  2. Rename the presentation to Sapporo Office Monthly Activity Report.
  3. Check and copy the Presentation ID from the URL in your browser’s address bar.
    • Example: https://docs.google.com/presentation/d/[ALPHANUMERIC_ID_HERE]/edit

Step 2: Save a Test Image to Google Drive

  1. Upload any image from your computer to Google Drive (e.g., a photo of Sapporo scenery, an office snapshot, or team event photo).
  2. Select the uploaded image file and copy its File ID from the URL.
    • Example: https://drive.google.com/file/d/[ALPHANUMERIC_ID_HERE]/view

That’s all the preparation we need!

3. STEP 2: Hands-On! Insert an Image Using Slide.insertImage()

With our setup complete, open the Google Apps Script editor and write the script.

First, we’ll run a minimal script to fetch the photo from Google Drive and insert it onto the slide.

1. Enter the Code

Open the Apps Script editor (either standalone or container-bound to your presentation) and paste the following code.

* Make sure to replace YOUR_PRESENTATION_ID and YOUR_FILE_ID with the IDs you copied earlier.

function insertSapporoPhoto() {
  // 1. Get the presentation and target slide (first slide)
  var presentationId = "YOUR_PRESENTATION_ID"; // Your presentation ID
  var presentation = SlidesApp.openById(presentationId);
  var slide = presentation.getSlides()[0]; // The first slide
  
  // 2. Fetch the Sapporo Office activity photo Blob from Google Drive
  var imageFileId = "YOUR_FILE_ID"; // Your image file ID
  var imageFile = DriveApp.getFileById(imageFileId);
  var imageBlob = imageFile.getBlob();
  
  // 3. Insert the image onto the slide
  var insertedImage = slide.insertImage(imageBlob);
  
  // 4. Output a completion log
  Logger.log("Inserted the Sapporo Office activity photo into the slide!");
}

2. Execution and Authorization

  1. Click the Save button (floppy disk icon) at the top of the editor.
  2. Confirm the selected function is set to insertSapporoPhoto, then click Run.
  3. On your first run, an “Authorization Required” dialog will pop up asking for access permissions to Drive and Slides. Follow the prompts to authorize the script.

3. Verify the Result

Switch over to your Google Slides tab.

If your Sapporo Office photo appears right on the first slide, congratulations! The script succeeded!

4. STEP 3: Advanced Techniques to Position and Resize Images Perfectly

When you insert an image with the default parameters, it might end up at the top-left corner or overflow the slide boundaries if the original image resolution is too high.

To generate clean presentation decks, you’ll want to specify both display coordinates (X, Y) and dimensions (width, height) via script.

Methods for Positioning and Resizing

Calling slide.insertImage() returns an Image (or PageElement) object. You can call the following methods on this returned object to adjust its layout precisely:

Method Description Unit
setLeft(x) Sets horizontal distance from the left edge of the slide Points (pt)
setTop(y) Sets vertical distance from the top edge of the slide Points (pt)
setWidth(width) Sets the width of the image Points (pt)
setHeight(height) Sets the height of the image Points (pt)

* Standard 16:9 Google Slides dimensions are 720pt wide by 405pt high.

Code Example: Specifying Coordinates and Dimensions

Let’s place our Sapporo Office activity photo on the right side of the slide, resized to a width of 300pt!

JavaScript

function insertAndAlignSapporoPhoto() {
  var presentation = SlidesApp.openById("YOUR_PRESENTATION_ID");
  var slide = presentation.getSlides()[0];
  
  // Fetch image Blob from Google Drive
  var imageFile = DriveApp.getFileById("YOUR_FILE_ID");
  var imageBlob = imageFile.getBlob();
  
  // 1. Overload syntax specifying coordinates and size directly
  // Parameters: (blob, left, top, width, height)
  var left = 380;   // X-coordinate: slightly to the right of center
  var top = 80;     // Y-coordinate: 80pt down from top edge
  var width = 300;  // Width: 300pt
  var height = 200; // Height: 200pt
  
  // Insert and apply positioning in one call!
  slide.insertImage(imageBlob, left, top, width, height);
  
  Logger.log("Image inserted with specified position and dimensions!");
}

💡 Pro Tip: Preserve Image Aspect Ratio While Resizing

If you hardcode fixed numbers for both width and height, the original aspect ratio might get distorted, stretching the photo unnaturally.

To resize an image cleanly while keeping its aspect ratio intact, insert the image first, read its original dimensions with getWidth() and getHeight(), and then calculate and apply a scaling factor.

JavaScript

function insertImageWithAspectRatio() {
  var presentation = SlidesApp.openById("YOUR_PRESENTATION_ID");
  var slide = presentation.getSlides()[0];
  var imageBlob = DriveApp.getFileById("YOUR_FILE_ID").getBlob();
  
  // First, insert the image as-is
  var img = slide.insertImage(imageBlob);
  
  // Get original width and height
  var origWidth = img.getWidth();
  var origHeight = img.getHeight();
  
  // Calculate scale factor for a target width of 300pt
  var targetWidth = 300;
  var ratio = targetWidth / origWidth;
  
  // Set dimensions preserving aspect ratio, then set position
  img.setWidth(targetWidth);
  img.setHeight(origHeight * ratio); // Automatically scaled height
  img.setLeft(380);
  img.setTop(100);
}

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

Here are common issues beginners encounter when using Slide.insertImage(), along with their solutions.

Trap 1: Passing the Full URL to DriveApp.getFileById()

// ✕ Incorrect (passing the full URL)
var file = DriveApp.getFileById("https://drive.google.com/file/d/12345/view");

// ◯ Correct (passing only the ID string)
var file = DriveApp.getFileById("12345");

The argument for getFileById() must be only the alphanumeric file ID string found between d/ and /view, not the entire URL.

Trap 2: Passing a File Object Directly Without .getBlob()

// ✕ Incorrect (passing the File object directly)
var file = DriveApp.getFileById("FILE_ID");
slide.insertImage(file); // Exception: Invalid argument

// ◯ Correct (convert to binary image data with .getBlob())
var blob = DriveApp.getFileById("FILE_ID").getBlob();
slide.insertImage(blob);

insertImage() expects a Blob (or BlobSource). Always call .getBlob() to convert the file into binary data first.

Trap 3: Attempting to Insert an Image Directly from a Web URL (https://…)

If you want to fetch and embed an image directly from a web URL rather than Google Drive, use UrlFetchApp.fetch(url).getBlob() (covered in detail in Episode #94).

// Inserting an image directly into a slide from a public web URL
var imageUrl = "https://example.com/sapporo-office.jpg";
var response = UrlFetchApp.fetch(imageUrl);
var imageBlob = response.getBlob();

slide.insertImage(imageBlob);

Trap 4: The Image Gets Hidden Behind Other Shapes or Text Boxes

If the slide already has text boxes or shapes (from Episode #64), newly inserted images may overlap awkwardly or hide behind other elements.

In that case, adjust the stacking order on the image object using img.bringToFront() or img.sendToBack().

6. Summary and What’s Coming Next

In this tutorial, we learned how to automatically insert and layout images in Google Slides using Slide.insertImage().

Key Takeaways

  1. Slide.insertImage() is the method used to insert images into Google Slides.
  2. Images must be converted into a Blob using DriveApp.getFileById().getBlob().
  3. You can specify position and dimensions simultaneously using insertImage(blob, left, top, width, height).
  4. To maintain aspect ratio, calculate the scaling factor from original dimensions and set them via setWidth() / setHeight().

By combining this with chart insertion from Episode #69 (insertSheetsChart) and image placement, you can fully automate monthly KPI reports and team activity presentations!

In Episode #71, we’ll dive into Slide.remove() and moveSlide()!

We’ll explore how to delete draft slides and rearrange slide order dynamically to assemble your final report deck with ease. See you next time!