- You want to confirm whether the current cursor position is inside in the namedRange on Google Document.
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this sample script? Please think of this as just one of several answers.
Workaround:
In this workaround, I checked whether the cursor position is included in the namedRange by comparing the indexes of paragraph of both the namedRange and the cursor position.
Flow:
The flow of the script is as follows.
- Retrieve the indexes of paragraph of the namedRange.
- I this sample script, from your question, the namedRange ID is used.
- In this case, there might be multiple paragraphs including table, list and so on. So all indexes in the namedRange are retrieved.
- Retrieve the index of paragraph of the cursor position.
- Retrieve the index of paragraph of the selected range.
- This sample script also checks whether the selected range is in the namedRange. Because when the text is selected,
cursor becomes null.
- If the cursor or selected range are staying in the namedRange,
myFunction() returns true.
- If the cursor or selected range are not staying in the namedRange,
myFunction() returns false.
- Also you can confirm it at the log.
Sample script:
Before you use this script, please set the namedRange ID.
function myFunction() {
var nameRangeId = "###"; // Please set namedRange ID here.
var getIndex = function(doc, e) {
while (e.getParent().getType() != DocumentApp.ElementType.BODY_SECTION) e = e.getParent();
return doc.getBody().getChildIndex(e);
};
var doc = DocumentApp.getActiveDocument();
// For namedRange
var namedRange = doc.getNamedRangeById(nameRangeId);
if (namedRange) {
var indexOfNamedRange = namedRange.getRange().getRangeElements().map(function(e) {return getIndex(doc, e.getElement())});
} else {
throw new Error("No namedRange.");
}
var name = namedRange.getName();
// For cursor
var cursor = doc.getCursor();
if (cursor) {
var indexOfCursor = getIndex(doc, cursor.getElement());
if (~indexOfNamedRange.indexOf(indexOfCursor)) {
Logger.log("Inside of %s", name);
return true;
}
Logger.log("Outside of %s", name);
return false;
}
// For select
var select = doc.getSelection();
if (select) {
var indexOfSelect = select.getRangeElements().map(function(e) {return getIndex(doc, e.getElement())});
if (indexOfSelect.some(function(e) {return ~indexOfNamedRange.indexOf(e)})) {
Logger.log("Inside of %s", name);
return true;
}
Logger.log("Outside of %s", name);
return false;
}
throw new Error("No cursor and select.");
}
Note:
- In this script, when the text is selected on Document, the cursor position cannot be retrieved. So I added the function to check the selected range. If you don't want to check the selected range, please remove the script of
// For select.
- In this script, even only one index of selected range are included in the namedRange,
true is returned. About this, please modify for your situation.
- In the current stage, this script doesn't suppose about the header and footer sections.
References:
If I misunderstood your question and this was not the direction you want, I apologize.