3

I am trying to select only part of a paragraph/text element, the code below works partially as it selects the entire paragraph:

 function FindTextSelection(){
   var range = DocumentApp.getActiveDocument().newRange();
   var findtext = DocumentApp.getActiveDocument().getBody().editAsText().findText("some").getElement();
   Logger.log(findtext);
   range.addElement(findtext);
   DocumentApp.getActiveDocument().setSelection(range.build());

 }

The aim is to find a #placeholder# and user can just type straight over the highlighted/selected text.

Can anyone point me in the right direction?

2 Answers2

0

After getting the value for "findText", you can get the text value of the var by

 var findTextValue = findText.asText();

and then change the background of that text in the body.

You can possible write a recursive function to find the #placeholder# and change the background color.

Try checking this question already been answered by Mogsdad for more details.

Hope that helps!

Community
  • 1
  • 1
KRR
  • 4,647
  • 2
  • 14
  • 14
-1
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var rangeBuilder = doc.newRange();

var cursor = DocumentApp.getActiveDocument().getCursor();
var surroundingText = cursor.getSurroundingText().getText();
var surroundingTextOffset = cursor.getSurroundingTextOffset();
surroundingText = surroundingText.substring(0,surroundingTextOffset);
rangeBuilder.addElement(DocumentApp.getActiveDocument().getCursor().getElement(), 0, surroundingTextOffset);
DocumentApp.getActiveDocument().setSelection(rangeBuilder.build())
Avinash
  • 4,115
  • 2
  • 22
  • 41