Find H3 in Docs update to text value after the colon in the text

Creating a Google Apps Script to process Google Docs documents in the manner you’ve described involves using the Google Docs API provided within Apps Script. The script will:

  1. Search for all paragraphs with Heading 3 (H3) styling.
  2. For each of these paragraphs, if the text contains a colon (:), the script will split the text at the colon.
  3. It will then trim any whitespace from the second part of the split text.
  4. Finally, it will replace the original paragraph text with this trimmed, second part.

Here’s a basic script that accomplishes this:

function processH3Paragraphs() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var numChildren = body.getNumChildren();

  for (var i = 0; i < numChildren; i++) {
    var child = body.getChild(i);
    // Check if the child element is a paragraph and has Heading 3 style
    if (child.getType() == DocumentApp.ElementType.PARAGRAPH &&
        child.asParagraph().getHeading() == DocumentApp.ParagraphHeading.HEADING3) {
      var text = child.asParagraph().getText();
      // Check if the text contains a colon
      if (text.includes(':')) {
        // Split the text at the colon, get the second part, trim it, and set it as the new text
        var parts = text.split(':');
        if (parts.length > 1) { // Ensure there is a second part after the split
          var newText = parts[1].trim(); // Trim whitespace from the second part
          child.asParagraph().setText(newText);
        }
      }
    }
  }
}

To use this script:

  1. Open your Google Doc.
  2. Go to Extensions > Apps Script.
  3. Delete any code in the script editor and paste the above script.
  4. Save the script with a name you’ll remember.
  5. Run processH3Paragraphs function by clicking on the play button next to the function name or through the custom menu that you might add via Script.

Please note, when running scripts that modify documents for the first time, Google will ask for authorization to access your document. Follow the prompts to allow the necessary permissions.

This script does not create an undo point automatically, so it’s a good idea to make a copy of your document before running the script, just in case you need to revert the changes.