As I understand from the documentation, a namedrange is an object that can be given a name and each range comprises one or more elements (e.g. text element). Once I have created a namedrange, I can then get all range objects of that namerange within the document using getNamedRanges() and then on each range object I can then use getRangeElements().
The following script uses the element returned by insertText() to create a namedrange 'element1' in google docs (ultimately, I will have multiple namedranges most with a unique name). Using getNamedRanges() I get back all namedranges in the document and then getRangeElements() to access each element of each of those namedranges. This is what I hope the following script does (simplified with just one namedrange and one text element in it).
var document = DocumentApp.getActiveDocument(); var cursor = document.getCursor(); var element = cursor.insertText('1st element'); var rangeBuilder = document.newRange(); rangeBuilder.addElement(element); document.addNamedRange('element1', rangeBuilder.build()); var ranges = document.getNamedRanges(); var rangeElements = []; var rangeTexts = []; for (var i = 0; i < ranges.length; i++) { rangeElements.push(ranges[i].getRangeElements()); for (var j = 0; j < rangeElements.length; j++) { rangeTexts.push(rangeElements[j].getText()); } }However, I get the error 'TypeError: ranges[i].getRangeElements is not a function'.
ranges is an array of objects and so range[i]is one range object on which I should be able to use the range method getRangeElements() (and because these range elements comprise text elements, I should then be able to use getText() on each of them).
All examples in the documentation I come across demonstrate the use of ranges within the user's selection rather than, as here, wrapping a range around the element that has been newly inserted. Perhaps the technique is different in that case.
Presumably I get the error because my understanding of name ranges is incorrect so where have I gone wrong? (Note, this is google docs not google sheets.)