How to Merge Specific Paragraphs in Google Docs Using Apps Script

In document editing and formatting, there are often specific rules or formats that need to be applied for clarity and consistency. One common requirement might be to merge a standalone header or label with its corresponding content in the paragraphs that follow. This tutorial will guide you through the process of creating a Google Apps Script that automates the merging of a paragraph containing only the label “Explanation:” with the subsequent paragraph. This is particularly useful in documents where explanations or descriptions are prefaced by a labeled paragraph.

Why Merge Paragraphs Automatically?

Manual editing can be time-consuming, especially in lengthy documents or when dealing with repeated patterns that need to be reformatted. Automating such tasks not only saves time but also reduces the risk of human error and ensures consistency throughout the document.

Step-by-Step Guide to Creating the Script

Step 1: Open the Script Editor

First, open the Google Docs document where you need this functionality. Then, access the Apps Script editor by going to the Extensions menu and selecting Apps Script.

Step 2: Write the Apps Script

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

function mergeExplanationParagraphs() {
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
var totalElements = body.getNumChildren();

// Iterate through the document elements in reverse order
for (var i = totalElements - 1; i >= 0; i--) {
var element = body.getChild(i);
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
var text = element.getText().trim();
if (text === "Explanation:") {
if (i + 1 < totalElements) {
var nextElement = body.getChild(i + 1);
if (nextElement.getType() === DocumentApp.ElementType.PARAGRAPH) {
var nextText = nextElement.getText();
body.removeChild(element);
nextElement.setText("Explanation: " + nextText);
}
}
}
}
}
}

This script checks each paragraph from the end of the document to the beginning. If a paragraph contains only the text “Explanation:”, it merges this paragraph with the one following it.

Step 3: Save and Run the Script

Save the script by clicking the floppy disk icon or selecting File > Save. Name your project. To execute the script, click the play button next to mergeExplanationParagraphs. Google will prompt you to authorize the script the first time it runs.

Step 4: Add a Custom Menu (Optional)

To make this feature easily accessible from within your Google Docs, add a custom menu by including this function in your script:

function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Merge Explanation Paragraphs', 'mergeExplanationParagraphs')
.addToUi();
}

After saving this update and reloading your document, you will see a new ‘Custom Menu’ in the toolbar, providing easy access to your script.

Conclusion

Using Google Apps Script to automate document formatting tasks like merging paragraphs can significantly enhance your productivity. This script ensures that every instance of the label “Explanation:” in your document is neatly combined with the following paragraph, maintaining a clean and professional appearance. Explore further possibilities with Apps Script to customize and automate your Google Docs even more efficiently.