Google Docs - Format Matching Text Script

From NoskeWiki
Jump to navigation Jump to search

About

NOTE: This page is a daughter page of: Google Docs App Scripts


This demos a Google Docs Add On / App Script to format text. :)

What the thing looks like after a run


Instructions

The right API to help is the Google Docs API... but that's slow to process, so here's what you need to do to create a new menu item that does something:

  1. Create a brand new Google Doc ............... (tip: Try typing into Chrome: docs.new)
  2. Click menu bar: Extensions > App Scripts.
  3. Enter the javascript code below into the Script Editor and hit "Save" and "Deploy" (as "Editor Add-On")
    ..... (tip: You can hit the play button to test one of the functions before saving)
  4. Reload your Google Doc and approve permissions.
  5. Run the Script by clicking the new menu item that appears: Special Formatting > Format Stat Numbers.

(see pictures of this process)



Google Docs Add On - Format Matching Text Script

"±" Example

Code.gs

 /**
 * @fileoverview This script uses Google Docs API to format certain strings.
 * Instructions and documentation for the script lives at:
 * http://andrewnoske.com/wiki/Google_Docs_-_Format_Matching_Text_Script
 */


/**
  * Formats the matching strings that match the given pattern.
  * param@ {string} findMe Regex string to look for.
  * param@ {string} color Color string in the form "#ffffff".
  * param@ {number} fontSize The font size to set.
  */
 function formatMatchingStrings(findMe, color, fontSize) {
  var document = DocumentApp.getActiveDocument();
  var body = document.getBody();

  // Find and color each match:
  var foundElement = body.findText(findMe);
  while (foundElement != null) {
    // Color the next match:
    var foundText = foundElement.getElement().asText();
    var start = foundElement.getStartOffset();
    var end = foundElement.getEndOffsetInclusive();

    // Change the color and font size:
    foundText.setForegroundColor(start, end, color);
    foundText.setFontSize(start, end, fontSize);

    // Find the next match:
    foundElement = body.findText(findMe, foundElement);
  }

} 

/**
  * Formats stats numbers starting with "±".
  */
 function formatStatNumbers(findMe, color, fontSize) {
   formatMatchingStrings('±.*', '#dddddd', 7);  // Regex enabled.
   // INSTRUCTIONS: Add a bunch more formatting rules here if you like.
 }

/**
 * Called when doc opens. Creates menu item for "Special Formatting".
 */
function onOpen() {
  // Add a menu item:
  DocumentApp.getUi().createMenu('Special Formatting')
      .addItem('Format Stat Numbers', 'formatStatNumbers')
      .addToUi();
}


Italicize Quotes Example

I often think italicizing quotes looks good, *but* for some reason the RE2 / body.findText(searchPattern) doesn't seem to play nice with the " symbol (even when you escape it with another), so this only works if you've already changed the quotes to directional (“ and ”). RE2 can be really finicky, and I can't even figure out how to pass them around as variables honestly.

Code.gs

/**
 * @OnlyCurrentDoc
 * @fileoverview This script uses Google Docs API to format certain strings.
 * Instructions and documentation for the script lives at:
 * http://andrewnoske.com/wiki/Google_Docs_-_Format_Matching_Text_Script
 */


/**
 * Italicizes directional double quotes.
 */
 function italicizeDirectionalQuotes() {
   var document = DocumentApp.getActiveDocument();
   var body = document.getBody();

   // Find and change each match:
   var foundElement = body.findText('“.*”');  // For some reason can't feed in variable for this.
   var count = 0
   while (foundElement != null) {
     // Color the next match:
     var foundText = foundElement.getElement().asText();
     var start = foundElement.getStartOffset();
     var end = foundElement.getEndOffsetInclusive();

     // Change the italics:
     foundText.setItalic(start, end, true);
     foundText.setForegroundColor(start, end, '#222222');
   
     // Find the next match:
     foundElement = body.findText('“.*”', foundElement);
     count += 1;
  }
  console.log("Count is: " + count);  // Console log number of matches.
 }

/**
 * Called when doc opens. Creates menu item for "Special Formatting".
 */
function onOpen() {
  // Add a menu item:
  DocumentApp.getUi().createMenu('Special Formatting')
      .addItem('Italicize Quotes', 'italicizeDirectionalQuotes')
      .addToUi();
}