Clear formating from page elements Google Apps Script

Creating a Google Apps Script to remove any additional formatting and reset elements to default settings in a Google Docs document involves iterating through the elements in the document and applying the default formatting. This script will target the body of the document and reset the formatting of paragraphs and text.

Here’s a basic script to get you started:

function resetDocumentFormatting() {
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();

  // Get all elements in the body
  var numElements = body.getNumChildren();

  for (var i = 0; i < numElements; i++) {
    var element = body.getChild(i);

    // Check if the element is a paragraph
    if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
      var paragraph = element.asParagraph();

      // Reset paragraph and text attributes to default
      //paragraph.setHeading(DocumentApp.ParagraphHeading.NORMAL);
      paragraph.setLineSpacing(1);
      paragraph.setSpacingAfter(0);
      paragraph.setSpacingBefore(0);
      paragraph.setIndentStart(0);
      paragraph.setIndentEnd(0);

      // Iterate through text elements to reset formatting
      var numChildren = paragraph.getNumChildren();
      for (var j = 0; j < numChildren; j++) {
        var child = paragraph.getChild(j);
        if (child.getType() === DocumentApp.ElementType.TEXT) {
          var text = child.asText();
          text.setFontFamily(null);
          text.setFontSize(null);
          text.setForegroundColor(null);
          text.setBackgroundColor(null);
          text.setBold(false);
          text.setItalic(false);
          text.setUnderline(false);
        }
      }
    }
  }
}

Usage Instructions:

  1. Open your Google Docs document.
  2. Go to Extensions > Apps Script.
  3. Copy and paste the above script into the Apps Script editor.
  4. Save and name your project.
  5. Run the resetDocumentFormatting function from the Apps Script editor.
  6. Authorize the script if prompted.

Tips:

  • This script resets the basic formatting for paragraphs and text elements to their default states.
  • The script does not handle all possible elements (like images, tables, etc.) and their specific formatting. You can extend the script to handle other types as needed.
  • Remember that running this script will apply changes to the entire document, which can’t be easily reversed. It’s advisable to test it on a copy of your document first.