How to Resize All Images in a Google Doc to 90% of Their Page Size Using Google Apps Script

Google Apps Script is a powerful tool that allows you to automate tasks across Google Workspace. In this tutorial, we will walk you through the process of resizing all images in a Google Doc to 90% of their page size using Google Apps Script. This can be particularly useful for ensuring consistency and optimizing the layout of your document.

Getting Started

Before we begin, make sure you have a Google Doc with some images in it. We’ll write a script that resizes these images to 90% of their page width.

Step-by-Step Guide

Step 1: Open Google Apps Script

  1. Open the Google Doc you want to modify.
  2. Click on Extensions > Apps Script to open the Google Apps Script editor.

Step 2: Write the Script

In the Apps Script editor, delete any existing code and paste the following script:

function resizeImages() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var images = body.getImages();
var pageWidth = body.getPageWidth() * 0.9; // 90% of the page width

for (var i = 0; i < images.length; i++) {
var image = images[i];
var originalWidth = image.getWidth();
var originalHeight = image.getHeight();
var aspectRatio = originalHeight / originalWidth;

var newWidth = pageWidth;
var newHeight = newWidth * aspectRatio;

image.setWidth(newWidth);
image.setHeight(newHeight);
}

Logger.log('All images resized to 90% of the page width.');
}

Step 3: Save and Run the Script

  1. Save your script by clicking on the floppy disk icon or by pressing Ctrl + S.
  2. To run the script, click on the play button (triangle icon) in the toolbar.

Step 4: Grant Permissions

The first time you run the script, you will need to grant it permission to access your Google Docs. Follow the prompts to authorize the script.

Step 5: Check Your Document

Once the script has run, check your Google Doc to see the resized images. They should now be 90% of the page width, maintaining their original aspect ratio.

Explanation of the Script

  • DocumentApp.getActiveDocument(): Gets the active Google Doc.
  • body.getImages(): Retrieves all images in the document body.
  • body.getPageWidth(): Gets the page width of the document.
  • image.getWidth() / image.getHeight(): Gets the current width and height of the image.
  • image.setWidth(newWidth) / image.setHeight(newHeight): Sets the new width and height of the image based on 90% of the page width while maintaining the aspect ratio.

Conclusion

Using Google Apps Script to resize images in a Google Doc can save you a lot of time, especially if you are dealing with documents that contain multiple images. This script ensures all images are resized consistently, improving the overall layout and presentation of your document.