2017-03-18 13 views
0

私は現在のドキュメントを取り、findTextを使ってユーザー定義の文字列を探すスクリプトを持っています。その文字列に引用符が含まれている場合(Bob's Burgersのように) findTextはそれを見つけません。私はそれが正規表現を使用していることを知っていますが、これを正しく見つけるように式をフォーマットする方法を理解することはできません。findText引用符を含むものを見つけよう

コード例:

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) { ...} 

答えて

0

私はこのpostからのコードを使用してfindText方法を試してみました:

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"); 

} 

結果:

enter image description here

をこの情報がお役に立てば幸いです。

+1

ええ、これは私がやっていることに非常に似ています。実際のフレーズによって散発的な結果が出ますが、ありがとうございます。私はこれを答えとしてマークします。私はスクリプトのGoogle側にいくつかの問題があるのだろうかと思うが、 –

0

利用代わりに `それをエスケープするための` \。

var target = "Bob\'s Burgers"; 
+0

私はこれを試しました。あなたはそれを試しましたか?私はそれを働かせることができませんでした。 –

関連する問題