Class RangeElement

  • RangeElement is a wrapper around an Element with possible start and end offsets, useful for representing partial selections within Text elements.

  • The getElement() method retrieves the Element corresponding to the RangeElement.

  • The getStartOffset() and getEndOffsetInclusive() methods provide the character positions for the start and end of a partial range, returning -1 for non-Text elements or full ranges.

  • The isPartial() method indicates whether the RangeElement covers the entire element or just a portion of its characters.

RangeElement

A wrapper around anElement with a possible start and end offset. These offsets allow arange of characters within aTextelement to be represented in search results, document selections, and named ranges.

Methods

MethodReturn typeBrief description
getElement()ElementGets theElement that corresponds to thisRangeElement.
getEndOffsetInclusive()IntegerGets the position of the end of a partial range within the range element.
getStartOffset()IntegerGets the position of the start of a partial range within the range element.
isPartial()BooleanDetermines whether this range element covers the entire element or a partial selection of theelement's characters.

Detailed documentation

getElement()

Gets theElement that corresponds to thisRangeElement.

constrangeElement=DocumentApp.getActiveDocument().getSelection().getRangeElements()[0];// Log debug information about a range element.Logger.log(`Element type:${rangeElement.getElement().getType()}`);if(rangeElement.isPartial()){Logger.log(`The character range begins at${rangeElement.getStartOffset()}`);Logger.log(`The character range ends at${rangeElement.getEndOffsetInclusive()}`,);}else{Logger.log('The entire range element is included.');}

Return

Element — the element that corresponds to thisRangeElement

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getEndOffsetInclusive()

Gets the position of the end of a partial range within the range element. If the element is aText element andisPartial() returnstrue, the offset is the number of characters before the lastcharacter in the range (that is, the index of the last character in the range); in any othercase, this method returns-1.

Return

Integer — forText elements, the number of character before the last character in the range; for other elements,-1

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getStartOffset()

Gets the position of the start of a partial range within the range element. If the element is aText element andisPartial() returnstrue, the offset is the number of characters before the start ofthe range (that is, the index of the first character in the range); in any other case, thismethod returns-1.

Return

Integer — forText elements, the number of characters before the start of the range; for other elements,-1

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

isPartial()

Determines whether this range element covers the entire element or a partial selection of theelement's characters. The beginning and end of a partial range element's range can bedetermined usinggetStartOffset() andgetEndOffsetInclusive(), respectively.

Return

Booleantrue if the element is partially included;false if not

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.