How to Split your Google Doc into separate new Docs separate with H3

Automating Google Docs Management with Apps Script: Splitting Documents by Headings

In today’s fast-paced digital environment, efficiency and automation are key to handling documents and data. Google Apps Script offers a powerful way to automate tasks within Google Workspace, including Docs, Sheets, and more. One common task is the management of large documents, particularly when you need to split a comprehensive document into smaller, more manageable pieces. This post will explore a practical Google Apps Script that splits a Google Doc into separate documents based on H3 heading markers, a useful approach for educators, researchers, and professionals who work with extensive reports or educational materials.

function splitDocumentByH3Headings() {
  var originalDocId = 'YOUR_ORIGINAL_DOCUMENT_ID_HERE'; // Replace with your original document ID
  var originalDoc = DocumentApp.openById(originalDocId);
  var body = originalDoc.getBody();
  var elements = body.getNumChildren();
  
  var folderId = DriveApp.getFileById(originalDocId).getParents().next().getId();
  
  var newDocNameBase = "quiz";
  var newDocCount = 10; // Starting count
  
  var newDoc = null;
  var newDocBody = null;
  
  for (var i = 0; i < elements; i++) {
    var element = body.getChild(i);
    var type = element.getType();
    
    // Check if the element is a Paragraph and styled as Heading 3
    if (type === DocumentApp.ElementType.PARAGRAPH) {
      var paragraph = element.asParagraph();
      if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
        if (newDoc !== null) {
          // Move the created document to the correct folder
          moveFileToFolder(newDoc.getId(), folderId);
        }
        // Start a new document for the next section
        newDoc = DocumentApp.create(newDocNameBase + newDocCount++);
        newDocBody = newDoc.getBody();
        newDocBody.clear(); // Clear the automatically created first paragraph
      }
    }
    
    // If newDoc is not null, it means we are currently filling a section
    if (newDoc !== null) {
      copyElementToDoc(element, newDocBody);
    }
  }
  
  // Move the last document to the desired folder, if any
  if (newDoc !== null) {
    moveFileToFolder(newDoc.getId(), folderId);
  }
  
  DocumentApp.getUi().alert('Documents have been created successfully.');
}

function copyElementToDoc(element, targetBody) {
  // Function to copy various types of elements to the new document
  var type = element.getType();
  var copiedElement;
  
  switch(type) {
    case DocumentApp.ElementType.PARAGRAPH:
      copiedElement = element.copy();
      targetBody.appendParagraph(copiedElement);
      break;
    case DocumentApp.ElementType.TABLE:
      copiedElement = element.copy();
      targetBody.appendTable(copiedElement);
      break;
    case DocumentApp.ElementType.LIST_ITEM:
      copiedElement = element.copy();
      targetBody.appendListItem(copiedElement);
      break;
    // Add more cases as necessary for other element types
  }
}

function moveFileToFolder(docId, folderId) {
  var file = DriveApp.getFileById(docId);
  var folder = DriveApp.getFolderById(folderId);
  folder.addFile(file);
  DriveApp.getRootFolder().removeFile(file);
}

The Challenge

Consider a scenario where you have a lengthy Google Doc containing various sections marked by H3 headings. Each section is intended to be a standalone document, perhaps for distribution to different teams or for focusing on specific topics in separate meetings. Manually copying and pasting each section into a new Google Doc is time-consuming and prone to errors.

The Solution: splitDocumentByH3Headings Function

The splitDocumentByH3Headings function automates the process of splitting the document. It iterates through the document, identifies sections based on H3 headings, and creates new documents for each section. Here’s a step-by-step breakdown of how the script works:

  1. Identify the Original Document: The script starts by opening the original document using its unique ID.
  2. Iterate Through Document Elements: It then goes through each element in the document, looking for paragraphs styled with the H3 heading.
  3. Create New Documents for Each Section: When an H3 heading is found, the script creates a new Google Doc for that section. It copies all content from that heading to the next into the new document, preserving the formatting.
  4. Organize the New Documents: Each new document is moved to the same folder as the original document, keeping your Drive organized.
  5. Repeat the Process: The script continues until all sections defined by H3 headings are copied into their new documents.

Benefits

  • Efficiency: Saves time by automating the splitting process.
  • Accuracy: Reduces manual errors in copying and formatting.
  • Organizational: Keeps related documents in the same folder for easy access.

Implementation

To use the splitDocumentByH3Headings function, you’ll need to insert the script into the script editor within Google Docs. Replace 'YOUR_ORIGINAL_DOCUMENT_ID_HERE' with the ID of your document. The script does the rest, creating a new document for each H3 section.