Reputation: 373
I'm trying to highlight some text (in the example below I would like to highlight "ORGANIZA") with some regex on a Google Docs document, but I'm unable to make first work a simple regex to find the "category_name" string.
Why this:
function highlightTextTwo() {
/* DOCUMENT DEFINITION */
var doc = DocumentApp.openById('1M6JmJPndLS_hkdaUo5holsdxB5GSSrcWMa1j4Hh7Dig');
/* VARIABLE DEFINITION */
var highlightStyle = {};
var paras = doc.getParagraphs();
var textLocation = {};
var i;
/* REGEX DEFINITION */
var MyRegex = new RegExp('category_name','i');
/* COLOR STYLE DEFINITION */
highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
/* CODE */
for (i=0; i<paras.length; ++i) {
Logger.log( paras[i].findText(MyRegex) );
}
}
applied to this document:
{
"map_image": "mapa_con_close_button.png",
"categories":[
{
"category_id": 1,
"category_name": "ORGANIZA",
"color": "#4591D0",
"icon_image": "Organiza.png"
},
{
"category_id": 2,
"category_name": "DELEGA",
"color": "#94C5DD",
"icon_image": "Delega.png"
},
{
"category_id": 3,
"category_name": "NEGOCIA Y GESTIONA EL CONFLICTO",
"color": "#E7344A",
"icon_image": "Negocia_y_Gestiona.png"
}
returns this:
[15-06-03 20:12:48:026 CEST] null
[15-06-03 20:12:48:027 CEST] null
[15-06-03 20:12:48:028 CEST] null
[15-06-03 20:12:48:029 CEST] null
[15-06-03 20:12:48:030 CEST] null
[15-06-03 20:12:48:030 CEST] null
instead the some nulls and one "category_name".
Upvotes: 2
Views: 532
Reputation: 626806
I found out a way to display all of your category_name
strings. Main points:
exec
in a while
loop instead od findText
g
flag with regexgetText()
Code:
var paras = doc.getParagraphs();
var MyRegex = new RegExp('category_name','ig');
for (i=0; i<paras.length; ++i) {
while (match = MyRegex.exec(paras[i].getText()))
{
Logger.log(match[0]);
}
}
Output in the log:
[15-06-04 21:07:36:320 CEST] category_NAME
[15-06-04 21:07:36:322 CEST] category_name
[15-06-04 21:07:36:324 CEST] category_name
EDIT:
Here is a way to highlight the matches with red color:
var paras = doc.getParagraphs();
var MyRegex = new RegExp('category_name','ig');
for (i=0; i<paras.length; ++i) {
while (match = MyRegex.exec(paras[i].getText()))
{
var searchResult = paras[i].findText(match[0]);
if (searchResult !== null) {
var thisElement = searchResult.getElement();
var thisElementText = thisElement.asText();
thisElementText.setBackgroundColor(searchResult.getStartOffset(), searchResult.getEndOffsetInclusive(),"#FF0000");
}
}
}
Upvotes: 3