Apps Script to Reset Font sizes in Docs to Default

To create a Google Apps Script that updates the formatting in a Google Docs document to remove all preset font styles and sizes, you can use the following code. This script will remove any specific font styles and sizes applied to the text and set it to the default formatting:

function removeFontStylesAndSizes() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  // Define the default text style you want to apply
  var defaultTextStyle = {
    fontSize: 12,
    bold: false,
    italic: false,
    underline: false,
    strikethrough: false,
    fontFamily: 'Arial',
    foregroundColor: null,
    backgroundColor: null
  };

  // Iterate through all the elements in the document's body
  var elements = body.getNumChildren();
  for (var i = 0; i < elements; i++) {
    var element = body.getChild(i);

    // Check if the element is a text element
    if (element.getType() === DocumentApp.ElementType.TEXT) {
      var text = element.asText();

      // Remove specific font styles and sizes
      text.setFontSize(defaultTextStyle.fontSize);
      text.setBold(defaultTextStyle.bold);
      text.setItalic(defaultTextStyle.italic);
      text.setUnderline(defaultTextStyle.underline);
      text.setStrikethrough(defaultTextStyle.strikethrough);
      text.setFontFamily(defaultTextStyle.fontFamily);
      text.setForegroundColor(defaultTextStyle.foregroundColor);
      text.setBackgroundColor(defaultTextStyle.backgroundColor);
    }
  }

  // Save the changes made to the document
  doc.saveAndClose();

  // Inform the user that the formatting has been updated
  DocumentApp.getUi().alert('Font styles and sizes have been updated to default.');
}

Here’s how you can use this script:

  1. Open your Google Docs document.
  2. Click on “Extensions” in the menu.
  3. Select “Apps Script.”
  4. Delete any code in the script editor.
  5. Paste the provided code into the script editor.
  6. Save the script by clicking the floppy disk icon or using Ctrl/Cmd + S.
  7. Close the script editor.
  8. Click on “Extensions” again and select “Custom Menu.”
  9. Click “Remove Font Styles” (or any other name you prefer) from the menu.

This will run the script and update the document’s formatting to the default styles you specified in the defaultTextStyle object. Make sure to adjust the defaultTextStyle object to match your desired default formatting.