0

I am looking at a way to determine the named range in Google docs through Google Apps Script for the current cursor position. Is there anyway to do that?

Context: I am marking a range in the google document for review using named range. I would like to get the named range if the cursor is placed in that location of the document so that I can show that there is a review item on that area.

Could you please let me know if there is a way to do that in Google Apps Script? thanks Jasper

Jasperin
  • 53
  • 7
  • 1
    What if there are 15 named ranges that contain the cell where the cursor is placed? –  May 17 '16 at 18:57
  • Have you tried reading this? http://stackoverflow.com/questions/12342164/getrange-with-named-range-google-spreadsheet-using-scripts – Android Enthusiast May 18 '16 at 14:45
  • @sandwich, I would like to get all the named ranges for the current position. – Jasperin May 19 '16 at 06:50
  • @d.datul1990 the reference is not helping as it is for Google Sheets in a different context. I am looking for Google Docs. – Jasperin May 19 '16 at 06:53

1 Answers1

0
var cursor = DocumentApp.getActiveDocument().getCursor();

cursor is a "Position".

A "Position" can tell you several things, for example the element which holds the position. Be careful: The thinking of a position is quite opposite: A position cannot contain other elements. A position is contained within an element.

So i think you could search your named ranges for this element:

cursor.getElement()

You have to iterate through your ranges, i think and if there is a match ... bingo.

Let me know if this helps.

Richard Gantz
  • 321
  • 1
  • 8
  • Thanks Richard. This is the kind of approach that I have been trying. But this is a brute force approach (still not reached solution though). I am wondering if there is any elegant solution. – Jasperin May 22 '16 at 16:02
  • Yes, i know what you mean. It's terrible, i know. i searched for a solution of a problem i had with bookmarks. This is very similar with the cursor and ranges. – Richard Gantz May 24 '16 at 20:08
  • But more as the Element the Position resides in or the surrounding text of the Position is not availlale. – Richard Gantz May 24 '16 at 20:23
  • So you have to use either the Element itself or the surrounding text to search for the Position within your document or named ranges. It's a brute force method, yes, but nothing else is availlable. I'm sorry, too. But i can help you find your ranges if you provide me a testdocument with some overlapping named ranges, if you like. It's really not that hard to locate all the named ranges your cursor is in. – Richard Gantz May 24 '16 at 20:26