0

I would like to setCursor to the location that is returned by findText.

Here is what I am trying:

    var position = doc.newPosition(foundElement.getElement(), foundElement.getStartOffset());
    doc.setCursor(position);

But, the cursor does not move. Even with the simple examples like

 //setting cursor at the beginning of the doc
 var paragraph = doc.getBody().getChild(0);
 var position = doc.newPosition(paragraph.getChild(0), 0);
 doc.setCursor(position);

findText returns a rangelement, while document.setCursor expects a position. How do I go from rangeelement to position? :)

This got me halfway to the solution Finding text (multiple times) and highlighting

Community
  • 1
  • 1
giorgio79
  • 3,353
  • 8
  • 46
  • 76

1 Answers1

2

Tried this code and it is perfectly setting the cursor in the beginning of the found text.

function myFunction() {
  var doc = DocumentApp.getActiveDocument();
  var paragraph = doc.getBody().getChild(0);
  var foundElement = doc.getBody().findText("text");
  var position = doc.newPosition(foundElement.getElement(), foundElement.getStartOffset());
    doc.setCursor(position);
}

Hope that helps!

KRR
  • 4,339
  • 2
  • 13
  • 14