https://github.com/lsvekis/Apps-Script-Code-Snippets
If you’ve ever worked on a long Google Doc with lots of images, you know the pain:
some images are too large, some too small, and manually resizing each one is tedious and inconsistent.
The good news? Google Apps Script can handle this automatically.
In this post, you’ll learn how to use a simple Apps Script to resize every image in a Google Doc to exactly 50% of the document’s usable width, based on the page layout and margins.
Why Resize Images Programmatically?
Manual resizing causes several common problems:
- Images don’t align consistently across pages
- Margins are ignored when resizing by hand
- Updates take forever in long documents
- Different contributors use different sizes
By using Apps Script, you get:
- ✅ Consistent image sizing
- ✅ Automatic respect for page margins
- ✅ Aspect ratios preserved
- ✅ One-click execution
What This Script Does
This script:
- Reads the document’s page width
- Subtracts the left and right margins
- Calculates 50% of the usable width
- Loops through all paragraphs
- Finds every inline image
- Resizes each image proportionally
⚠️ Important: Google Docs only allows scripts to resize inline images, not floating or drawing-based images.
The Apps Script Code
Copy and paste the following code into your Apps Script editor:
function resizeImagesToHalfPageWidth() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
// Get page size and margins (points)
const pageWidth = body.getPageWidth();
const marginLeft = body.getMarginLeft();
const marginRight = body.getMarginRight();
// Usable content width
const usableWidth = pageWidth - marginLeft - marginRight;
// Target width = 50%
const targetWidth = usableWidth * 0.5;
let imageCount = 0;
// Walk through all elements
const numChildren = body.getNumChildren();
for (let i = 0; i < numChildren; i++) {
const element = body.getChild(i);
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
const paragraph = element.asParagraph();
const numElements = paragraph.getNumChildren();
for (let j = 0; j < numElements; j++) {
const child = paragraph.getChild(j);
if (child.getType() === DocumentApp.ElementType.INLINE_IMAGE) {
const image = child.asInlineImage();
// Maintain aspect ratio
const originalWidth = image.getWidth();
const originalHeight = image.getHeight();
const scale = targetWidth / originalWidth;
image.setWidth(targetWidth);
image.setHeight(originalHeight * scale);
imageCount++;
}
}
}
}
Logger.log(`Resized ${imageCount} images to 50% page width.`);
}
How to Run the Script
- Open your Google Doc
- Click Extensions → Apps Script
- Paste the code above
- Save the project
- Run
resizeImagesToHalfPageWidth - Approve permissions when prompted
That’s it. Every inline image in your document is now resized consistently.
Common Questions
Does this affect image quality?
No. The script only changes display size, not the underlying image file.
Will this resize drawings or floating images?
No. Google Docs scripting only supports inline images.
Can I change the width percentage?
Absolutely. Just change this line:
const targetWidth = usableWidth * 0.5;
For example:
0.6→ 60% width1.0→ full-width images
Why This Matters for Automation
This pattern is useful beyond image resizing:
- Formatting cleanup before publishing
- Enforcing brand/layout standards
- Preparing documents for export to PDF
- Automating editorial workflows
Once you start using Apps Script this way, Google Docs becomes a programmable editor, not just a word processor.