Google Apps Script to retrieve the font style properties of the first 10 elements in a Google Docs document body

Here’s how to 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. To run the script, you can click the play button (▶) in the script editor.
  9. After running the script, check the “View” menu and select “Logs” to see the font style properties of the first 10 elements in the document’s body.

This script will log the font style properties, including font size, bold, italic, underline, strikethrough, font family, foreground color, and background color, for the first 10 text elements in the document’s body. You can modify the count variable to retrieve properties for more or fewer elements as needed.

function getFontStyleProperties() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var result = [];

  // Iterate through the first 10 elements in the document's body
  var elements = body.getNumChildren();
  var count = Math.min(elements, 10); // Get the first 10 elements or less
  for (var i = 0; i < count; i++) {
    var element = body.getChild(i);

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

      // Get font style properties
      var fontStyle = {
        fontSize: text.getFontSize(),
        bold: text.isBold(),
        italic: text.isItalic(),
        underline: text.isUnderline(),
        strikethrough: text.isStrikethrough(),
        fontFamily: text.getFontFamily(),
        foregroundColor: text.getForegroundColor(),
        backgroundColor: text.getBackgroundColor()
      };

      result.push({
        elementIndex: i + 1,
        text: text.getText(),
        fontStyle: fontStyle
      });
    }
  }

  // Log the font style properties to the Apps Script log
  Logger.log(result);
}