Apps Script to Bold element within a doc that starts with a word

To create a Google Apps Script that bolds any text in a Google Document starting with the word “Question”, you will need to iterate through all the elements of the document and check the text content of these elements. If an element’s text starts with “Question”, the script will apply bold formatting to that text.

Here’s a basic script to achieve this:

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

  for (var i = 0; i < numChildren; i++) {
    var child = body.getChild(i);
    if (child.getType() === DocumentApp.ElementType.PARAGRAPH) {
      var paragraph = child.asParagraph();
      var text = paragraph.getText();
      if (text.startsWith("Question")) {
        // Apply bold to the entire paragraph
        paragraph.editAsText().setBold(true);
      }
    }
  }
}

To use this script:

  1. Open your Google Doc.
  2. Click on Extensions > Apps Script.
  3. Copy and paste the above script into the script editor.
  4. Save and name your project.
  5. Run the function boldTextStartingWithQuestion from the script editor.

Please note the following:

  • This script applies bold formatting to the entire paragraph that starts with “Question”. If you need to bold only the specific word “Question” or a part of the paragraph, the script needs to be modified accordingly.
  • The first time you run the script, you will need to authorize it to modify your document.
  • This script affects the entire document it’s attached to. Make sure to run it on a copy of your document if you’re unsure about the changes it will make.