Class RangeBuilder

  • RangeBuilder is used to constructRange objects from document elements.

  • It offers methods to add entire or partial elements, elements between two points, or contents of another range.

  • Thebuild() method constructs the finalRange object.

  • ThegetRangeElements() method retrieves all elements within the built range.

  • getSelectedElements() is a deprecated method, replaced bygetRangeElements().

RangeBuilder

A builder used to constructRange objects from document elements.

// Change the user's selection to a range that includes every table in the// active tab.constdoc=DocumentApp.getActiveDocument();constdocumentTab=doc.getActiveTab().asDocumentTab();constrangeBuilder=documentTab.newRange();consttables=documentTab.getBody().getTables();for(leti=0;i <tables.length;i++){rangeBuilder.addElement(tables[i]);}doc.setSelection(rangeBuilder.build());

Methods

MethodReturn typeBrief description
addElement(element)RangeBuilderAdds an entireElement to thisRangeBuilder.
addElement(textElement, startOffset, endOffsetInclusive)RangeBuilderAdds a partialText element to thisRangeBuilder.
addElementsBetween(startElement, endElementInclusive)RangeBuilderAdds two entire elements, and all elements between them, to thisRangeBuilder.
addElementsBetween(startTextElement, startOffset, endTextElementInclusive, endOffsetInclusive)RangeBuilderAdds two partialText elements, and all elements between them, to theRangeBuilder.
addRange(range)RangeBuilderAdds the contents of anotherRange to thisRangeBuilder.
build()RangeConstructs aRange from the settings applied to the builder.
getRangeElements()RangeElement[]Gets all elements in thisRange, including any partialText elements (for example, in the caseof a selection that includes only part of aText element).

Deprecated methods

MethodReturn typeBrief description
getSelectedElements()RangeElement[]Gets all elements that the user has selected in the open instance of the document, includingany partially selectedTextelements.

Detailed documentation

addElement(element)

Adds an entireElement to thisRangeBuilder.

Parameters

NameTypeDescription
elementElementthe element to be added

Return

RangeBuilder — this builder, for chaining

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

addElement(textElement, startOffset, endOffsetInclusive)

Adds a partialText element to thisRangeBuilder.

Parameters

NameTypeDescription
textElementTextthe text element to be partially added
startOffsetIntegerthe number of characters before the first character to be included (that is, the index of the first character in the range)
endOffsetInclusiveIntegerthe number of characters before the last character to be included (that is, the index of the last character in the range)

Return

RangeBuilder — this builder, for chaining

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

addElementsBetween(startElement, endElementInclusive)

Adds two entire elements, and all elements between them, to thisRangeBuilder.

Parameters

NameTypeDescription
startElementElementthe first element to be added
endElementInclusiveElementthe last element to be added

Return

RangeBuilder — this builder, for chaining

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

addElementsBetween(startTextElement, startOffset, endTextElementInclusive, endOffsetInclusive)

Adds two partialText elements, and all elements between them, to theRangeBuilder.

Parameters

NameTypeDescription
startTextElementTextthe first text element to be partially added
startOffsetIntegerthe number of characters before the first character ofstartTextElement to be included (that is, the index of the first character in the range)
endTextElementInclusiveTextthe last text element to be partially added
endOffsetInclusiveIntegerthe number of characters before the last character ofendTextElementInclusive to be included (that is, the index of the last character in the range)

Return

RangeBuilder — this builder, for chaining

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

addRange(range)

Adds the contents of anotherRange to thisRangeBuilder.

Parameters

NameTypeDescription
rangeRangethe range whose elements should be added

Return

RangeBuilder — this builder, for chaining

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

build()

Constructs aRange from the settings applied to the builder.

Return

Range — the newly constructed range


getRangeElements()

Gets all elements in thisRange, including any partialText elements (for example, in the caseof a selection that includes only part of aText element). To determine whether aText element is only partially included in the range, seeRangeElement.isPartial().

Return

RangeElement[] — an array of elements, in the order they appear in the document

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

Deprecated methods

getSelectedElements()

Deprecated. Renamed togetRangeElements().

Gets all elements that the user has selected in the open instance of the document, includingany partially selectedTextelements.

Return

RangeElement[] — an array of selected or partially selected elements, in the order they appear in the document

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.