Apps Script that updates paragraphs with specific search terms to H3 types instead of paragraphs

To create a Google Apps Script that finds any element in a Google Docs document that starts with a number and ends with a colon “:” and updates that element to an H3 type, you can use the following script:

function updateElementsToH3() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var text = body.getText();
  var elements = body.getParagraphs();
  
  // Regular expression to match elements starting with a number and ending with ":"
  var regex = /^\d+.*:$/;
  
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var elementText = element.getText();
    
    // Check if the element matches the regex pattern
    if (regex.test(elementText)) {
      // Create a new H3 element and set its text
      var h3 = body.insertParagraph(i + 1, '');
      h3.setText(elementText);
      h3.setHeading(DocumentApp.ParagraphHeading.HEADING3);
      
      // Remove the original element
      element.removeFromParent();
    }
  }
}

Here’s how to add and run this script in Google Docs:

  1. Open your Google Docs document.
  2. Click on “Extensions” in the top menu.
  3. Select “Apps Script” to open the Google Apps Script editor.
  4. Paste the provided script into the editor.
  5. Save the script with a name, e.g., “Update Elements to H3.”
  6. Close the script editor.

Now, you can run the script by following these steps:

  1. Click on “Extensions” again in the top menu.
  2. Select your script name, e.g., “Update Elements to H3,” from the list.
  3. Click the play button (▶️) to execute the script.

The script will scan your document for elements that start with a number and end with “:” and update them to H3 headings. Please make sure to save your document before running the script, as it will make changes to the document’s content.