Update H3 elements font size in a doc with Apps Script

To create a Google Apps Script that updates the font size of <h3> elements in a Google Document to a larger size, you will need to follow these steps. The script will loop through the document, identify all <h3> styled paragraphs, and increase their font size.

  1. Open Google Apps Script: Go to Google Apps Script and sign in with your Google account.
  2. Create a New Project: Click on New Project.
  3. Replace the Code: In the script editor, replace the contents of the Code.gs file with the script provided below.
  4. Save and Name Your Project: Click on File > Save, and give your project a name.
  5. Run the Script: Before running the script, ensure the document you want to update is open or you know its ID. You may need to authorize the script to run under your account the first time.

Here’s a basic script example that increases the font size of all <h3> elements in a document:

function updateH3FontSize() {
  var documentId = 'YOUR_DOCUMENT_ID_HERE'; // Replace with your Google Document ID
  var doc = DocumentApp.openById(documentId);
  var body = doc.getBody();
  
  // Define the new font size
  var newFontSize = 18; // Example: 18pt font size
  
  var paragraphs = body.getParagraphs();
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
      paragraph.setFontSize(newFontSize);
    }
  }
  
  // Save the changes
  doc.saveAndClose();
}

Instructions:

  • YOUR_DOCUMENT_ID_HERE: Replace this with the ID of the Google Document you want to modify. You can find the ID in the URL of the document. For example, in the URL https://docs.google.com/document/d/1A2B3C4D5E6F7G8H_IJKLMNOPQRST/view, the ID is 1A2B3C4D5E6F7G8H_IJKLMNOPQRST.
  • newFontSize: Set this to the desired font size for your <h3> elements.

This script sets the font size of all paragraphs with the Heading 3 style to 18 points. You can adjust the newFontSize variable to meet your specific needs.

After saving the script, click on the run button to execute the script. If it’s the first time you’re running the script, Google will prompt you to authorize the script to access your Google Docs. Follow the prompts to allow the necessary permissions.