Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I color certain words in Google Document using Google Apps Script?

I'm trying to highlight certain words in my Google Document. I know I can replace text using document.replace, but it only replaces string itself, not formatting. Is there a way to replace string with colored string using Google Apps Script?

like image 484
user1613077 Avatar asked Aug 22 '12 00:08

user1613077


People also ask

Can you fill color in Google Docs?

Adding a Background to Text Boxes in Google DocsChoose your textbox and choose the “Fill Color” option highlighted in the toolbar. This is the tipping paint can icon. 2. From the drop-down menu, choose the color you want for your text box background.

How do I filter in Google script?

In google script, we have to set the range of data that we want the script to filter on. The range starts at A1 and ends at F10 (A1:F10). Once we set the filter, we need to filter on a specific column. If we want to filter on a specific 'Vendor ID', we will need to set the criteria for that column.


2 Answers

With the introduction of document-bound scripts, it's now possible to make a text highlighting function that's invoked from a custom menu.

Surely THIS is the best answer now! 8^)

This script was modified from the one in this answer, and may be called from the UI (with no parameters) or a script.

/**
 * Find all matches of target text in current document, and highlight them.
 *
 * @param {String} target     (Optional) The text or regex to search for. 
 *                            See Body.findText() for details.
 * @param {String} background (Optional) The desired highlight color.
 *                            A default orange is provided.
 */
function highlightText(target,background) {
  // If no search parameter was provided, ask for one
  if (arguments.length == 0) {
    var ui = DocumentApp.getUi();
    var result = ui.prompt('Text Highlighter',
      'Enter text to highlight:', ui.ButtonSet.OK_CANCEL);
    // Exit if user hit Cancel.
    if (result.getSelectedButton() !== ui.Button.OK) return;
    // else
    target = result.getResponseText();
  }
  var background = background || '#F3E2A9';  // default color is light orangish.
  var doc = DocumentApp.getActiveDocument();
  var bodyElement = DocumentApp.getActiveDocument().getBody();
  var searchResult = bodyElement.findText(target);

  while (searchResult !== null) {
    var thisElement = searchResult.getElement();
    var thisElementText = thisElement.asText();

    //Logger.log(url);
    thisElementText.setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(),background);

    // search for next match
    searchResult = bodyElement.findText(target, searchResult);
  }
}

/**
 * Create custom menu when document is opened.
 */
function onOpen() {
  DocumentApp.getUi().createMenu('Custom')
      .addItem('Text Highlighter', 'highlightText')

      .addToUi();
}
like image 178
Mogsdad Avatar answered Sep 17 '22 20:09

Mogsdad


This is a better solution:

function highlightTextTwo() {
  var doc  = DocumentApp.openById('<your document id');
  var textToHighlight = 'dusty death';
  var highlightStyle = {};
  highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
  var paras = doc.getParagraphs();
  var textLocation = {};
  var i;

  for (i=0; i<paras.length; ++i) {
    textLocation = paras[i].findText(textToHighlight);
    if (textLocation != null && textLocation.getStartOffset() != -1) {
      textLocation.getElement().setAttributes(textLocation.getStartOffset(),textLocation.getEndOffsetInclusive(), highlightStyle);
    }
  }
}

Previous Answer:

The key is to being able to reference just the words you want to color.

My solution is to:

Get the text of the paragraph that contains the words you wish to color, remove the original paragraph, then add each part of the text back. As you add each part back the appendText returns a reference to just the text added, you then can specify its color with setForegroundColor():

function highlightText() {
  var doc = DocumentApp.openById('<your document id>');
  var textToHighlight = 'dusty death';
  var textLength = textToHighlight.length;
  var paras = doc.getParagraphs();
  var paraText = '';
  var start;
  for (var i=0; i<paras.length; ++i) {
    paraText = paras[i].getText();
    start = paraText.indexOf(textToHighlight);
    if (start >= 0) {
      var preText = paraText.substr(0, start);
      var text = paraText.substr(start, textLength);
      var postText = paraText.substr(start + textLength, paraText.length);
      doc.removeChild(paras[i]);
      var newPara = doc.insertParagraph(i, preText);
      newPara.appendText(text).setForegroundColor('#FF0000');
      newPara.appendText(postText).setForegroundColor('#000000');
    }
  }
}
like image 27
Weehooey Avatar answered Sep 19 '22 20:09

Weehooey