Google Apps Script that bolds any document element text ending with a question mark

To create a Google Apps Script that bolds any document element (text) ending with a question mark, you can use the following approach. This script iterates through all the elements in the document. For text elements, it checks if the text ends with a question mark. If so, it applies bold formatting to the entire text element. This example focuses on text elements specifically, but the concept can be adapted to other types of elements if needed.

Here’s how you can write the script:

function boldTextEndingWithQuestionMark() {
  // Open the active document or use DocumentApp.openById('DOCUMENT_ID') to open by ID
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  // Get the total number of elements in the document
  var numElements = body.getNumChildren();
  
  for (var i = 0; i < numElements; i++) {
    var element = body.getChild(i);
    
    // Check if the element is of type text (paragraph, list item, etc.)
    if (element.getType() === DocumentApp.ElementType.PARAGRAPH ||
        element.getType() === DocumentApp.ElementType.LIST_ITEM) {
      
      var text = element.asText().getText(); // Get the text of the element
      var textLength = text.length;
      
      // Check if the text ends with a question mark
      if (textLength > 0 && text[textLength - 1] === '?') {
        element.asText().setBold(true); // Apply bold formatting to the entire element
      }
    }
  }
}

Steps to use this script:

  1. Open your Google Doc.
  2. Navigate to Extensions > Apps Script.
  3. Delete any code in the script editor and paste the code provided above.
  4. Save the script with an appropriate name.
  5. Run the function boldTextEndingWithQuestionMark by clicking the play/run button.
  6. Authorize the script if prompted, by following the on-screen instructions.

This script will apply bold formatting to the entire paragraph or list item that ends with a question mark. Note that it checks for text elements specifically (paragraphs and list items in this case), and it will apply the bold formatting to the entire text element if the last character is a question mark.

Remember, the script applies changes directly to your document, and these changes might not be easily reversible. Consider creating a copy of your document for testing purposes before running the script on your main document.