Automating Google Docs: Splitting Headings for Better Structure

apps script for a doc that will select h3 paragraphs, if they contain a colon split the content at the first colon, keep the first part as h3 move the second part of the string to a new paragraph following the first one

To create a Google Apps Script that processes paragraphs in a Google Document, specifically targeting those formatted as Heading 3, and splits them at the first colon into two separate paragraphs, follow these instructions:

  1. Open your Google Document.
  2. Go to Extensions > Apps Script.
  3. In the Apps Script editor, clear any existing code and copy-paste the following script:
function splitH3Paragraphs() {
  // Access the active document.
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();
  // Get all the paragraphs in the document.
  var paragraphs = body.getParagraphs();
  // Iterate over all paragraphs in reverse to avoid indexing issues after splitting.
  for (var i = paragraphs.length - 1; i >= 0; i--) {
    var paragraph = paragraphs[i];
    // Check if the paragraph is a Heading 3.
    if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
      var text = paragraph.getText();
      var colonIndex = text.indexOf(':'); // Find the first colon in the text.
      // If a colon is found and it's not the last character.
      if (colonIndex > -1 && colonIndex < text.length - 1) {
        var firstPart = text.substring(0, colonIndex);
        var secondPart = text.substring(colonIndex + 1).trim(); // Remove leading spaces.
        // Update the current paragraph with the first part of the split.
        // Create a new paragraph for the second part of the split right after the current one.
        var newParagraph = body.insertParagraph(i + 1, secondPart);
        newParagraph.setHeading(DocumentApp.ParagraphHeading.NORMAL); // Set new paragraph as normal text.
  1. Save your project with a meaningful name, for example, “Split H3 Paragraphs”.
  2. Close the script editor.
  3. Reload your Google Document for the changes to take effect.
  4. To run the script, go back to Extensions > Apps Script, select the splitH3Paragraphs function from the dropdown, and click the run icon.

The first time you run the script, you will need to authorize it. Follow the on-screen prompts to grant the necessary permissions. This script examines all paragraphs styled as Heading 3, and if they contain a colon, it splits the paragraph at that point. The text before the colon remains in the original paragraph (still styled as Heading 3), and the text following the colon is moved to a new normal-styled paragraph directly below.

Please note, if you add more Heading 3 paragraphs later, you will need to run the script again to apply the same logic to new content.

In the realm of digital documentation, Google Docs stands out for its simplicity and collaboration features. However, even with the most straightforward tools, repetitive tasks can become tedious. One such task is formatting large documents, particularly when dealing with specific structures like questions or statements followed by explanations. This is where Google Apps Script comes in handy, allowing us to automate these mundane tasks and focus on our content. Today, I’ll walk you through an interesting use case: splitting Heading 3 paragraphs at the first colon to improve document readability and structure.

Why Split Headings?

In many documents, particularly instructional or informational materials, headings often contain two parts: a statement or question and an accompanying clarification or expansion. Typically, these parts are separated by a colon. While this format is compact, it can hinder readability and scanning efficiency. By splitting these into a heading and a subsequent paragraph, we enhance clarity and make the text more approachable.

How to Automate the Process

The manual process involves going through each Heading 3, finding the colon, and then splitting the text. This is time-consuming and prone to errors, especially in lengthy documents. Automation through Google Apps Script provides a seamless solution. Here’s how to set it up:

  1. Open Your Google Document: The first step is, of course, to have your document ready. This script will be particularly useful for those who work with structured documents like reports, guides, or educational materials.
  2. Access Google Apps Script: Navigate to Extensions > Apps Script from within your Google Document. This opens the script editor, where magic happens.
  3. Write the Script: You’ll enter a specific script (provided below) that tells Google Docs to find all Heading 3 paragraphs, check for a colon, and then split the text appropriately. Don’t worry; you don’t need to be a programmer to use this script. Simply copy and paste!
  4. Save and Run: After pasting the script, save your project and run the script. You’ll need to grant permissions the first time around, but it’s a straightforward process guided by prompts.
  5. Check Your Document: Return to your document and see the transformation. Each Heading 3 that contained a colon will be split, improving the document’s structure and readability.

The Script Explained

The core of this automation lies in the script you paste into Google Apps Script. Here’s a simplified breakdown:

  • Get All Paragraphs: The script first gathers all paragraphs in your document.
  • Identify and Split: It then identifies paragraphs styled as Heading 3 and containing a colon. These paragraphs are split at the colon’s position.
  • Reformat: The first part (before the colon) remains as Heading 3, while the second part becomes a new, normal-styled paragraph.

This automation is particularly useful for educators, technical writers, or anyone who frequently works with structured documents.


By automating mundane formatting tasks, we can save time and reduce the potential for errors. This particular Google Apps Script offers a straightforward way to enhance document structure, making information more accessible and easier to digest. Automation in Google Docs is not just about saving time; it’s about creating documents that communicate more effectively. Whether you’re a seasoned coder or a complete novice, Google Apps Script opens up a world of possibilities for improving your workflow and document quality.