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();
  document.addNamedRange('element1', rangeBuilder.build());
  var ranges = document.getNamedRanges();
  var rangeElements = [];
  var rangeTexts = [];
  for (var i = 0; i < ranges.length; i++) {
    for (var j = 0; j < rangeElements.length; j++) {

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.)

1 Answer 1


The problem is that the objects returned by ranges[i] are not of the type that you thought.

To quickly find with methods are supported by an object, and the object type, use the autocomplete function. The below image shows the autocomplete pop-up showing

  • getId
  • getName
  • getRange
  • remove

but not getRangeElements.

To the right of each method might be shown a fragment of its documentation, clicking on it will open another box showing more details include the prototype path, that says what type of object is returned by the variable, in this case is a NamedRange, not a Range.

To get the Range associated with the NamedRange use getRange i.e.


once you have the Range then you could use getRangeElemens i.e.

  • Thanks for the quick response Rubén. It's confusing when the documentation in some cases refers to a namedrange as a range. I still have a problem with this rangeElements.push(ranges[i].getRange().getRangeElements()); for (var j = 0; j < rangeElements.length; j++) { rangeTexts.push(rangeElements[j].getText()); } where getText is not a function. Autocompletion only goes so far – on rangeElements[j]. it provides a list of array methods as when I enter a dot after ranges[i].getRange().getRangeElements() Mar 7, 2021 at 9:23
  • @MarkGrimshaw-Aagaard That is because getRangeElements() returns a collection ob objects. You might use [0] to get the first range elements [1] to get the second, and so on. Mar 7, 2021 at 18:04
  • Sorted. Thank you Rubén. Mar 8, 2021 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.