Managing large documents in Google Docs can be challenging, especially when dealing with multiple sections under Heading 1. Manually splitting these sections into separate documents is time-consuming and prone to errors.
With Google Apps Script, we can automate this process by identifying each Heading 1 section and creating a new document for each. This script helps in scenarios where you need to:
✅ Extract sections into standalone documents
✅ Organize content automatically
✅ Improve workflow efficiency
Let’s explore how this works!
🚀 The Problem: Managing Large Google Docs with Multiple Sections
Imagine you have a long Google Docs file structured like this:
Heading 1: Introduction
Some text here...
Heading 1: JavaScript Basics
More content...
Heading 1: Advanced Concepts
More content...
Manually copying and pasting each Heading 1 section into a new document is inefficient. What if we could automate this?
💡 The Solution: Google Apps Script to Split Google Docs
The following Google Apps Script automates the process by:
✅ Scanning the document for Heading 1 elements
✅ Identifying section start positions
✅ Copying each section into a new Google Doc
✅ Retaining text, images, tables, and formatting
📜 Google Apps Script Code
function splitDocumentByHeading1() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();
var docName = doc.getName();
var sectionStartIndices = [];
// Identify all Heading 1 positions
for (var i = 0; i < paragraphs.length; i++) {
if (paragraphs[i].getHeading() == DocumentApp.ParagraphHeading.HEADING1) {
sectionStartIndices.push(i);
}
}
if (sectionStartIndices.length < 2) {
Logger.log("Not enough Heading 1 sections found.");
return;
}
// Split content into separate documents
for (var j = 0; j < sectionStartIndices.length; j++) {
var startIdx = sectionStartIndices[j];
var endIdx = j + 1 < sectionStartIndices.length ? sectionStartIndices[j + 1] : paragraphs.length;
var newDoc = DocumentApp.create(docName + " - Section " + (j + 1));
var newBody = newDoc.getBody();
newBody.appendParagraph(docName + " - Section " + (j + 1))
.setHeading(DocumentApp.ParagraphHeading.TITLE);
// Copy content between Heading 1 sections
for (var k = startIdx; k < endIdx; k++) {
copyElement(newBody, paragraphs[k]);
}
Logger.log("Document created: " + newDoc.getUrl());
}
}
/**
* Copies an element from the original document to the new document body.
*/
function copyElement(targetBody, element) {
var type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
targetBody.appendParagraph(element.copy());
} else if (type == DocumentApp.ElementType.LIST_ITEM) {
targetBody.appendListItem(element.copy());
} else if (type == DocumentApp.ElementType.TABLE) {
targetBody.appendTable(element.copy());
} else if (type == DocumentApp.ElementType.INLINE_IMAGE) {
targetBody.appendInlineImage(element.copy());
} else {
Logger.log("Skipping unsupported element: " + type);
}
}
🔍 How the Script Works
1️⃣ Retrieves the active document using DocumentApp.getActiveDocument()
.
2️⃣ Loops through all paragraphs to identify Heading 1 sections.
3️⃣ Stores the positions of all Heading 1 elements.
4️⃣ Creates a new document for each section and names it accordingly.
5️⃣ Copies text, images, tables, and lists while maintaining formatting.
6️⃣ Logs the URLs of the newly created documents for easy access.
📌 Example Usage
Before Running the Script
Your Google Doc contains:
Heading 1: Introduction
Some text...
Heading 1: JavaScript Basics
More content...
Heading 1: Advanced Concepts
More content...
After Running the Script
The script creates three separate Google Docs with titles like:
🔹 Document Name - Section 1
🔹 Document Name - Section 2
🔹 Document Name - Section 3
Each contains the respective Heading 1 section and its content.
🚀 How to Run the Script in Google Docs
1️⃣ Open your Google Docs document.
2️⃣ Click Extensions > Apps Script.
3️⃣ Delete any existing code and paste the script into the editor.
4️⃣ Click Run ▶️ to execute the script.
Within seconds, all Heading 1 sections will be split into separate documents.
🎯 Why Use This Script?
✅ Saves Time – No more manual copy-pasting.
✅ Ensures Consistency – Each section remains properly formatted.
✅ Works Automatically – Handles large documents with multiple sections effortlessly.
This script is especially useful for educators, writers, and project managers who need to split large documents into smaller, more manageable sections.
🔗 Final Thoughts
With Google Apps Script, you can automate tedious tasks and streamline your document management process. Whether you’re dealing with long reports, course materials, or structured content, this script makes it easy to split Google Docs effortlessly.
