How to Automatically Remove Double Spaces in Google Docs Using Apps Script

If you regularly work with long documents, you’ve probably encountered one of the most annoying formatting problems:

Double spaces scattered throughout your text.

This often happens when content is:

  • Copied from PDFs
  • Imported from Microsoft Word
  • Generated using AI tools
  • Edited by multiple collaborators
  • Converted from scanned documents

Manually fixing these spacing issues can take a significant amount of time — especially in large documents.

The good news? You can solve this problem instantly using Google Apps Script.


Why Automate Spacing Cleanup?

Double spaces may seem like a small issue, but they can:

  • Make documents look unprofessional
  • Break formatting consistency
  • Interfere with publishing workflows
  • Create extra work during editing

Automation allows you to clean your entire document in seconds instead of manually searching and replacing each instance.


The Solution: Automated Double-Space Removal

With a simple Apps Script, you can:

✅ Scan your entire Google Doc
✅ Detect all double spaces
✅ Replace them with single spaces
✅ Apply changes instantly

This script works across the full document body and preserves all formatting.


The Apps Script Code

function removeDoubleSpaces() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();

  // Replace double spaces until none remain
  let text = body.getText();

  while (text.indexOf("  ") !== -1) {
    body.replaceText("  +", " ");
    text = body.getText();
  }

  Logger.log("Double spaces removed!");
}

How to Install and Run the Script

Step 1 — Open Apps Script

  1. Open your Google Doc
  2. Click Extensions → Apps Script

Step 2 — Add the Code

  1. Remove any existing placeholder code
  2. Paste the script above

Step 3 — Run the Script

  1. Click Run
  2. Grant permissions when prompted

Your document will immediately be cleaned of all double spaces.


What This Script Handles

The automation safely:

  • Fixes multiple consecutive spaces
  • Works across the entire document
  • Preserves formatting and styles
  • Handles large documents efficiently

When This Automation Is Most Useful

You’ll benefit most if you frequently:

  • Import external content
  • Work with AI-generated drafts
  • Edit collaborative documents
  • Prepare content for publishing
  • Maintain professional formatting standards

Why This Is a Perfect Automation Example

This is a great illustration of how small automation tasks create big productivity gains.

Instead of spending time on repetitive formatting corrections, you can focus on what truly matters:

  • Writing
  • Editing
  • Designing content
  • Creating value

Possible Enhancements

You can extend this script to perform more advanced cleanup tasks, such as:

  • Removing extra line breaks
  • Standardizing punctuation spacing
  • Fixing inconsistent quotation marks
  • Cleaning formatting across multiple documents
  • Adding a custom “Cleanup” menu button

Final Thoughts

Repetitive formatting issues are some of the easiest tasks to automate — yet they consume an enormous amount of time when handled manually.

Google Apps Script empowers you to turn these everyday annoyances into one-click solutions.

Once you start automating small tasks like this, you’ll quickly discover how much time you can save in your daily workflow.