Google Apps Script to modify a Google Docs document so that any paragraph starting with a certain word is changed to a Heading 1

Creating a Google Apps Script to modify a Google Docs document so that any paragraph starting with a certain word is changed to a Heading 1 is a useful way to automatically format your document. Here’s a basic script to get you started:

  1. Open your Google Docs document: Open the document you want to apply this script to.
  2. Open the Script Editor:
    • Go to Extensions > Apps Script.
    • A new tab will open with the Google Apps Script editor.
  3. Write the Script:
    • In the script editor, you can write a function to check each paragraph and update it to Heading 1 if it starts with a specified word.

Here’s an example script for this purpose:

function updateParagraphsToHeading() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();
  
  var specificWord = "YourWord"; // Replace with the word you're looking for

  paragraphs.forEach(function(paragraph) {
    var text = paragraph.getText();
    if (text.startsWith(specificWord)) {
      paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING1);
    }
  });
}

In this script, replace "YourWord" with the word that you want to trigger the change to Heading 1.

  1. Save and Run the Script:
    • Click the disk icon or File > Save to save your script.
    • Give your project a name.
    • Run the function updateParagraphsToHeading by clicking the play button.
  2. Authorize the Script:
    • The first time you run the script, you’ll need to authorize it.
    • Follow the prompts to allow your script to access your Google Doc.
  3. Using the Script:
    • Each time you want to apply this formatting, run the updateParagraphsToHeading function in the script editor.

This script will automatically update all paragraphs that start with the specified word to Heading 1. Keep in mind that this script checks for the specified word at the very beginning of the paragraph and is case-sensitive. If you need a case-insensitive check, you can modify the script accordingly.