When working with Google Docs, formatting inconsistencies can sometimes cause unnecessary indentations, making your document look uneven or misaligned. If you frequently deal with documents where paragraphs have unwanted indentations, manually adjusting each one can be a time-consuming task.
Luckily, Google Apps Script allows us to automate this process and remove all paragraph indentations with just one click. In this blog post, we’ll explore a simple yet powerful script that helps you align all text to the left by resetting paragraph indentations.
Why Remove Paragraph Indentations?
Indentations in Google Docs serve various purposes, such as distinguishing paragraphs or structuring a document. However, there are instances where you may want to remove them:
✅ Standardize document formatting – Ensure consistency across paragraphs.
✅ Prepare text for export – Some publishing platforms may require left-aligned text.
✅ Improve readability – Remove distracting indentations in reports, articles, or essays.
✅ Save time – Automate repetitive formatting tasks instead of adjusting each paragraph manually.
With that in mind, let’s dive into the script that makes it all happen!
Google Apps Script to Remove Indentations
The following Google Apps Script function iterates through all paragraphs in your Google Docs document and removes their left indentation and first-line indentation, aligning everything to the left.
function removeParagraphIndentations() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var numParagraphs = body.getNumChildren();
for (var i = 0; i < numParagraphs; i++) {
var element = body.getChild(i);
// Check if the element is a paragraph
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
var paragraph = element.asParagraph();
// Remove indentations by setting the indentation attributes to 0
paragraph.setIndentStart(0); // Removes left indentation
paragraph.setIndentFirstLine(0); // Removes first-line indentation
}
}
Logger.log("All paragraph indentations have been removed.");
}
How This Script Works
🏗️ Step-by-Step Breakdown
- Access the Active Document:
- The script starts by retrieving the currently open Google Docs document.
var doc = DocumentApp.getActiveDocument(); var body = doc.getBody();
- Iterate Through All Elements:
- The script counts the number of elements (such as paragraphs, tables, images, etc.) in the document and loops through each one.
var numParagraphs = body.getNumChildren(); for (var i = 0; i < numParagraphs; i++) {
- Identify Paragraphs:
- The script checks whether each element is a paragraph before applying any formatting changes.
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
- Remove Indentations:
- If the element is a paragraph, the script removes the left indentation (
setIndentStart(0)
) and removes the first-line indentation (setIndentFirstLine(0)
).
paragraph.setIndentStart(0); paragraph.setIndentFirstLine(0);
- If the element is a paragraph, the script removes the left indentation (
- Log the Action:
- A message is logged when the script completes, indicating that all indentations have been removed.
Logger.log("All paragraph indentations have been removed.");
How to Use This Script in Google Docs
Follow these simple steps to run the script in your Google Doc:
1️⃣ Open Google Apps Script
- Open a Google Docs document where you want to remove indentations.
- Click on Extensions > Apps Script in the menu.
2️⃣ Add the Script
- Delete any existing code and paste the script into the editor.
3️⃣ Run the Script
- Click the Run button at the top of the Apps Script editor.
- If prompted, grant the necessary permissions.
4️⃣ Check Your Document
- All paragraphs will now be left-aligned with no indentations! 🎉
When to Use This Script
📌 Cleaning up pasted text – Sometimes, when you paste text from other sources, unwanted indentations appear. This script helps fix that instantly.
📌 Formatting bulk documents – If you work with long reports, articles, or manuscripts, this script saves hours of manual formatting.
📌 Ensuring consistency in shared documents – Perfect for teams collaborating on documents with different formatting styles.
Final Thoughts
With just a few lines of Google Apps Script, you can automate the tedious task of removing indentations from a Google Doc. Whether you’re formatting an essay, cleaning up notes, or standardizing a report, this script ensures a consistent, professional look with minimal effort.
