Google Apps Script that removes the starting numbers from any H3 paragraph

  1. Open your Google Docs document.
  2. Click on Extensions in the menu.
  3. Choose Apps Script.
  4. Delete any code in the script editor and replace it with the following code:
function removeNumbersFromH3() {
  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();

  // Regular expression to match numbers at the start of the string followed by a dot and space
  var numberPattern = /^\d+\.\s+/; 

  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
      var text = paragraph.getText();
      var updatedText = text.replace(numberPattern, '');

      // Only update the paragraph text if the updatedText is not empty
      if (updatedText !== '') {
        paragraph.setText(updatedText);
      } else {
        // If updatedText is empty, you could decide to set it to a default text
        // or perform some other logic. If you're okay with just leaving the paragraph
        // as it is when it turns out empty, you can leave this part out or log a message.
        // Example:
        // paragraph.setText('Placeholder text'); // Or simply log a warning
        Logger.log('Updated text is empty for a paragraph originally containing: ' + text);
      }
    }
  }
}
  1. Save the script with a name, for example, Remove H3 Numbers.
  2. Close the Apps Script window.
  3. To run the script, click on Extensions, navigate to Macros, and select your saved script (Remove H3 Numbers).

What this script does:

  • It gets all the paragraphs in the document.
  • It checks if a paragraph is styled as Heading 3.
  • It uses a regular expression (/^\d+\.\s+/) to find any number followed by a period and a space at the beginning of the paragraph.
  • If it finds a number at the start, it removes it and updates the paragraph text.

Remember, every time you want to remove numbers from H3 headings, you have to manually run the script from the Extensions > Macros menu.