0

I have a script that takes the current document and looks for a user defined string using findText. If that string contains a quote (like: Bob's Burgers). findText does not find it. I know it uses regular expressions, but I cannot figure out how to format the expression so it finds this properly.

code example:

var target = "Bob's Burgers";
var body = DocumentApp.getActiveDocument().getBody();

try
{
    var searchResult = body.findText(target);
    //does not find the text. But can find Bob easily.
}
catch(e) { ...}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152

2 Answers2

0

Use \` instead of ` to escape it.

var target = "Bob\'s Burgers";
Sangbok Lee
  • 2,132
  • 3
  • 15
  • 33
0

I tried the findText method using the code from this post:

function highlightText(findMe) {
    var body = DocumentApp.getActiveDocument().getBody();
    var foundElement = body.findText(findMe);

    while (foundElement != null) {
        // Get the text object from the element
        var foundText = foundElement.getElement().asText();

        // Where in the Element is the found text?
        var start = foundElement.getStartOffset();
        var end = foundElement.getEndOffsetInclusive();

        // Change the background color to yellow
        foundText.setBackgroundColor(start, end, "#FCFC00");

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

}

function myFunction() {
  highlightText("Bob’s Burger");

}

Result:

enter image description here

Hope this helps.

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
  • 1
    Yeah, this is very similar to what i am doing. I get sporadic results depending on the actual phrase, but thank you. I will mark this as the answer. Although I wonder if there are some issues with the Google side of the script. – Phil Allison Mar 20 '17 at 11:24