1
function searchAndColorInRed() {
  var doc = DocumentApp.getActiveDocument();
  var textToHighlight = new RegExp('\(\d{0,4}\)');
  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);
    }
  }
};

Got this function to search for numbers in parentheses like (6406) however the coloration won't work.. I have no ideas why, can someone help me please ?

Srik
  • 7,907
  • 2
  • 20
  • 29
Ydhem
  • 928
  • 2
  • 14
  • 36
  • I can't get it to work either ... I could only get numbers highlighted in the doc which had a slash then numbers then slash, such as this: /234/ – David Tew Jun 29 '13 at 11:25
  • And I could also match the following text in the dox: /(234)/ using regex /\(\d*\)/ (but, as above comment, not (234) – David Tew Jun 29 '13 at 11:48
  • I hope you'll get an answer...I've been trying a few variant of your code and other approaches but without success. No idea why :-/ pourtant le regex est correct..., bizarre :-) – Serge insas Jun 29 '13 at 22:36
  • 3
    I could be wrong, but I believe the findText() method expects a string parameter representing the regular expression to search for, not a regex object. But yeah, it seems pretty restrictive what you can use. I managed to get it to work if you put everything in character classes rather than escaping: `var textToHighlight = '[(][0-9]{0,4}[)]';` – AdamL Jun 30 '13 at 09:33
  • Nicely bottomed out Adam. – David Tew Jun 30 '13 at 10:57
  • @Meds, wrt the highlighting script itself, mogsdad has posted an alternative to yours above, where, using AdamL's string of character classes above, then all occurrences of your target are highlighted, even more than once in the same paragraph. Mogsdad script here: http://stackoverflow.com/questions/12064972/can-i-color-certain-words-in-google-document-using-google-script/16924466#16924466 – David Tew Jun 30 '13 at 12:24

1 Answers1

0

AdamL is right.

I was bit confused due to the Google Documentation's description of the findText method :

findText(searchPattern)

Searches the contents of the element for the specified text pattern using regular expressions.

I also managed to get it works by escaping all \ using a string too.

function searchAndColorInRed() {
  var doc = DocumentApp.getActiveDocument();
  var textToHighlight = "\\(\\d{0,4}\\)";
  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);
    }
  }
};

So yes, eventually as AdamL said you can use '[(][0-9]{0,4}[)]'; Or you can use what David has proposed, Mogsdad's script that seems convenient for what I need Can I color certain words in Google Document using Google Apps Script?

Community
  • 1
  • 1
Ydhem
  • 928
  • 2
  • 14
  • 36