Resetting H1 Font Size to Default in Google Docs Using Google Apps Script

If you’ve adjusted the font size of your H1 headings in Google Docs and want to reset them back to the default size, Google Apps Script offers a simple solution to automate this process. In this blog post, we’ll guide you through creating a script to reset the font size of all H1 headings to their default size.

function resetH1Size() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var paragraphs = body.getParagraphs();
  
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    
    if (paragraph.getHeading() == DocumentApp.ParagraphHeading.HEADING1) {
      paragraph.setFontSize(null); // Reset the font size to default
    }
  }
}

Step-by-Step Guide

  1. Open Your Google Doc: Begin by opening the Google Doc you want to modify.
  2. Access Google Apps Script: Navigate to Extensions -> Apps Script. This will open the script editor.
  3. Delete Existing Code: If there’s any pre-existing code in the script editor, you can delete it to start fresh.
  4. Add the Script: Copy and paste the following code into the script editor:function resetH1Size() { var doc = DocumentApp.getActiveDocument(); var body = doc.getBody(); var paragraphs = body.getParagraphs(); for (var i = 0; i < paragraphs.length; i++) { var paragraph = paragraphs[i]; if (paragraph.getHeading() == DocumentApp.ParagraphHeading.HEADING1) { paragraph.setFontSize(null); // Reset the font size to default } } }
  5. Save the Script: Save your script with a descriptive name like “ResetH1Size”.
  6. Run the Script: Click the play button (triangle icon) in the toolbar to run the script. You may be prompted to authorize the script to make changes to your document. Follow the authorization steps.

How the Script Works

This script works by iterating through all the paragraphs in your document. It checks if each paragraph is an H1 heading. If an H1 heading is found, the script resets its font size to the default value specified by Google Docs.

Here’s a detailed breakdown of the script:

  • Get the Active Document:var doc = DocumentApp.getActiveDocument(); var body = doc.getBody();
  • Loop Through Paragraphs:var paragraphs = body.getParagraphs();
  • Check for H1 Headings and Reset Font Size:if (paragraph.getHeading() == DocumentApp.ParagraphHeading.HEADING1) { paragraph.setFontSize(null); // Reset the font size to default }

The setFontSize(null) function call effectively removes any custom font size applied to the H1 headings, reverting them to the default size.

Conclusion

By following these steps, you can efficiently reset the font size of all H1 headings in your Google Docs to the default size. This automation saves you time and ensures consistent formatting across your document. Feel free to customize the script further to suit your specific needs. Happy scripting!