Jason Allshorn
Jason Allshorn

Reputation: 1625

How to get the index of the cursor in a Google Document using Google app script?

I have this code so far:

 var targetDoc = DocumentApp.getActiveDocument();
 var cursor = targetDoc.getCursor().getElement();
 var cursorParent  = cursor.getParent();
 var cursorPoint = cursorParent.getParent().getChildIndex(cursorParent);

But this brings back the child of the cursors parent's parent which is always 0. I cannot find a way to get the childIndex of the cursor's own parent.

Does anyone know how to do this?

Kind Regards.

Upvotes: 0

Views: 696

Answers (1)

Jason Allshorn
Jason Allshorn

Reputation: 1625

I was totally over thinking it. I separated out my code from the main function and found this will bring back the index number of the cursor's parent:

var targetDoc = DocumentApp.getActiveDocument();
var cursor = targetDoc.getCursor().getElement();
var cursorParent  = cursor.getParent().getChildIndex(cursor);
Logger.log(cursorParent);

Upvotes: 2

Related Questions