Class TextRange

  • A TextRange is a segment of text within a Shape or TableCell.

  • Using methods that edit how text fits within a shape will deactivate any autofit settings applied to that shape.

  • TextRange provides various methods for manipulating text, including appending, clearing, finding, getting information about, inserting, replacing, selecting, and setting text content.

  • Methods likeasRenderedString() andasString() allow retrieving the text in different formats, including rendered text and raw text with special characters for non-text elements.

  • Several methods require specific authorization scopes, such ashttps://www.googleapis.com/auth/presentations.currentonly orhttps://www.googleapis.com/auth/presentations.

TextRange

A segment of the text contents of aShape or aTableCell.

If you use methods that edit how the text fits within a shape, any autofit settings applied tothe shape are deactivated.

Methods

MethodReturn typeBrief description
appendParagraph(text)ParagraphAppends a paragraph at the end of the text range.
appendRange(textRange)TextRangeAppends a copy of the provided text range to the end of the current text range.
appendRange(textRange, matchSourceFormatting)TextRangeAppends a copy of the provided text range to the end of the current text range.
appendText(text)TextRangeAppends text at the end of the text range.
asRenderedString()StringReturns the rendered text bounded by this range of the associated shape or table cell in aformat appropriate to display to end users.
asString()StringReturns the raw text bounded by this range of the associated shape or table cell.
clear()voidClears the text bounded by this range.
clear(startOffset, endOffset)voidClears the text bounded by the start and end offsets in the range.
find(pattern)TextRange[]Returns all the ranges matching the search pattern in the current text range.
find(pattern, startOffset)TextRange[]Returns all the ranges matching the search pattern in the current text range starting from thestart offset.
getAutoTexts()AutoText[]Returns the auto texts within the current text range.
getEndIndex()IntegerReturns the exclusive, 0-based index for the last character in this range.
getLength()IntegerReturns the number of characters in this range.
getLinks()TextRange[]Returns a collection of text ranges that correspond to allLinks within the currenttext range or overlapping the current text range.
getListParagraphs()Paragraph[]Returns the paragraphs in lists that overlap the current text range.
getListStyle()ListStyleReturns theListStyle of the current text range.
getParagraphStyle()ParagraphStyleReturns theParagraphStyle of the current text range.
getParagraphs()Paragraph[]Returns the paragraphs that overlap the current text range.
getRange(startOffset, endOffset)TextRangeReturns a newTextRange covering part of the range from which it is derived.
getRuns()TextRange[]Returns the text runs that overlap the current text range.
getStartIndex()IntegerReturns the inclusive, 0-based index for the first character in this range.
getTextStyle()TextStyle|nullReturns the text style of the range, ornull if the range is empty.
insertParagraph(startOffset, text)ParagraphInserts a paragraph at the start offset.
insertRange(startOffset, textRange)TextRangeInserts a copy of the provided text range at the start offset.
insertRange(startOffset, textRange, matchSourceFormatting)TextRangeInserts a copy of the provided text range at the start offset.
insertText(startOffset, text)TextRangeInserts text at the start offset.
isEmpty()BooleanReturnstrue if there are no characters in this range, and returnsfalseotherwise.
replaceAllText(findText, replaceText)IntegerReplaces all instances of text matching find text with replace text.
replaceAllText(findText, replaceText, matchCase)IntegerReplaces all instances of text matching find text with replace text.
select()voidSelects only theTextRange in the active presentation and removes any previousselection.
setText(newText)TextRangeSets the text bounded by this range of the associated shape or table cell.

Detailed documentation

appendParagraph(text)

Appends a paragraph at the end of the text range. The paragraph maintains the styling of theend of the current text range.

The provided text string is appended as a paragraph by adding at least one surroundingnewline character to the string.

When the provided text string contains newline characters (thus consisting of multipleparagraphs), the final paragraph added is returned.

Parameters

NameTypeDescription
textStringThe string to append as a paragraph.

Return

Paragraph — The appendedParagraph.

Authorization

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

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

appendRange(textRange)

Appends a copy of the provided text range to the end of the current text range.

The formatting of the inserted text matches that of the source text.

Parameters

NameTypeDescription
textRangeTextRangeThe text range to append.

Return

TextRange — The text range representing the appended text.

Authorization

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

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

appendRange(textRange, matchSourceFormatting)

Appends a copy of the provided text range to the end of the current text range.

If set to match the formatting of the destination text,AutoText within the providedtext range are replaced with their rendered values. Furthermore, any non-text elements withinthe provided text range are not appended.

Parameters

NameTypeDescription
textRangeTextRangeThe text range to append.
matchSourceFormattingBooleanIftrue, match the formatting of the source text; iffalse, match the formatting of the destination text.

Return

TextRange — The text range representing the appended text.

Authorization

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

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

appendText(text)

Appends text at the end of the text range. The text maintains the styling of the end of theexisting text.

Parameters

NameTypeDescription
textStringThe string to append.

Return

TextRange — The text range representing the appended text.

Authorization

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

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

asRenderedString()

Returns the rendered text bounded by this range of the associated shape or table cell in aformat appropriate to display to end users.

AutoText elements, such as generated slide numbers, are replaced with their rendered values.Any non-text elements in the range are omitted.

Return

String — The rendered text in the range.

Authorization

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

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

asString()

Returns the raw text bounded by this range of the associated shape or table cell.

AutoText elements such as generated slide numbers and any non-text elements in the range arereplaced with the Unicode character U+E907.

Return

String — The raw text in the range.

Authorization

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

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

clear()

Clears the text bounded by this range.

Since the entire text in a Shape or TableCell must end in a newline, the final newline inthe text is not removed.

Authorization

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

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

clear(startOffset, endOffset)

Clears the text bounded by the start and end offsets in the range.

Since the text must end in a newline, the final newline in text is not removed even if it'scovered by the given offsets.

Parameters

NameTypeDescription
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the range to clear. The start offset must be equal to or greater than 0 and less than or equal toendOffset.startOffset must also be less than the length of the current range.
endOffsetIntegerThe number of characters past the start index of the current text range used to determine the exclusive end index of the range to clear. TheendOffset must be equal to or greater thanstartOffset.endOffset must also be less than or equal to the length of the current range.

Authorization

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

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

find(pattern)

Returns all the ranges matching the search pattern in the current text range. The search iscase sensitive.

Parameters

NameTypeDescription
patternStringThe regular expression pattern to search; any backslashes in the pattern should be escaped.

Return

TextRange[] — A list of text ranges.

Authorization

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

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

find(pattern, startOffset)

Returns all the ranges matching the search pattern in the current text range starting from thestart offset. The search is case sensitive.

Parameters

NameTypeDescription
patternStringThe regular expression pattern to search; any backslashes in the pattern should be escaped.
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the range to search.startOffset must also be less than the length of the current range.

Return

TextRange[] — A list of text ranges.

Authorization

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

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

getAutoTexts()

Returns the auto texts within the current text range.

Return

AutoText[] — A list of auto texts.

Authorization

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

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

getEndIndex()

Returns the exclusive, 0-based index for the last character in this range. If the start and endindices are equal, the range is considered to be empty.

Return

Integer — The end index of the range.

Authorization

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

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

getLength()

Returns the number of characters in this range.

Return

Integer — The number of characters in this range.

Authorization

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

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

getLinks()

Returns a collection of text ranges that correspond to allLinks within the currenttext range or overlapping the current text range.

Each returned range is guaranteed to span one link when it is created. Text modificationscan cause it to no longer represent exactly one link.

EachLink on the returned ranges can be accessed viaTextStyle.getLink().

// Accesses the first link on a TextRange object.consttextRange=SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0].getText();constlinkTextRange=textRange.getLinks()[0];consttextStyle=linkTextRange.getTextStyle();Logger.log(textStyle.hasLink());// logs 'true'constlink=textStyle.getLink();// Link object

Return

TextRange[] — A list of text ranges.

Authorization

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

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

getListParagraphs()

Returns the paragraphs in lists that overlap the current text range.

Return

Paragraph[] — A list of paragraphs in lists.

Authorization

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

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

getListStyle()

Returns theListStyle of the current text range.

Return

ListStyle — The list style of the current text range.

Authorization

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

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

getParagraphStyle()

Returns theParagraphStyle of the current text range.

Return

ParagraphStyle — The paragraph style of the current text range.

Authorization

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

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

getParagraphs()

Returns the paragraphs that overlap the current text range.

Return

Paragraph[] — A list of paragraphs.

Authorization

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

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

getRange(startOffset, endOffset)

Returns a newTextRange covering part of the range from which it is derived.

Parameters

NameTypeDescription
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the returned range. The start offset must be equal to or greater than 0 and less than or equal toendOffset.startOffset must also be less than the length of the current range.
endOffsetIntegerThe number of characters past the start index of the current text range used to determine the exclusive end index of the returned range. TheendOffset must be equal to or greater thanstartOffset.endOffset must also be less than or equal to the length of the current range.

Return

TextRange — A new text range.

Authorization

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

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

getRuns()

Returns the text runs that overlap the current text range. A text run is a segment of textwhere all the characters have the same text style.

Each returned range is only guaranteed to span one run when it is created. Text or stylemodifications can cause it to no longer represent exactly one run.

Return

TextRange[] — A list of text ranges.

Authorization

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

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

getStartIndex()

Returns the inclusive, 0-based index for the first character in this range. If the start andend indices are equal, the range is considered to be empty.

Return

Integer — The start index of the range.

Authorization

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

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

getTextStyle()

Returns the text style of the range, ornull if the range is empty.

Return

TextStyle|null — The text style of the range.

Authorization

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

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

insertParagraph(startOffset, text)

Inserts a paragraph at the start offset. The paragraph maintains the styling of the currenttext range at the start offset.

The provided text string is inserted as a paragraph by adding at least one surroundingnewline character to the string.

When the provided text string contains newline characters (thus consisting of multipleparagraphs), the final paragraph added is returned.

Parameters

NameTypeDescription
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the text to insert.
textStringThe string to insert.

Return

Paragraph — The insertedParagraph.

Authorization

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

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

insertRange(startOffset, textRange)

Inserts a copy of the provided text range at the start offset.

The formatting of the inserted text matches that of the source text.

Parameters

NameTypeDescription
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the text to insert.
textRangeTextRangeThe text range to insert.

Return

TextRange — The text range representing the inserted text.

Authorization

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

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

insertRange(startOffset, textRange, matchSourceFormatting)

Inserts a copy of the provided text range at the start offset.

If set to match the formatting of the destination text,AutoText within the providedtext range are replaced with their rendered values. Furthermore, any non-text elements withinthe provided text range are not inserted.

Parameters

NameTypeDescription
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the text to insert.
textRangeTextRangeThe text range to insert.
matchSourceFormattingBooleanIftrue, match the formatting of the source text; iffalse, match the formatting of the destination text.

Return

TextRange — The text range representing the inserted text.

Authorization

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

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

insertText(startOffset, text)

Inserts text at the start offset. The text maintains the styling of the existing text at thestart offset.

Parameters

NameTypeDescription
startOffsetIntegerThe number of characters past the start index of the current text range used to determine the inclusive start index of the text to insert.
textStringThe string to insert.

Return

TextRange — The text range representing the inserted text.

Authorization

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

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

isEmpty()

Returnstrue if there are no characters in this range, and returnsfalseotherwise.

Return

Booleantrue if there are no characters in this range, and returnsfalse otherwise.

Authorization

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

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

replaceAllText(findText, replaceText)

Replaces all instances of text matching find text with replace text. The search is caseinsensitive.

Parameters

NameTypeDescription
findTextStringThe text to find.
replaceTextStringThe text to replace the matched text.

Return

Integer — the number of occurrences changed

Authorization

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

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

replaceAllText(findText, replaceText, matchCase)

Replaces all instances of text matching find text with replace text.

Parameters

NameTypeDescription
findTextStringThe text to find.
replaceTextStringThe text to replace the matched text.
matchCaseBooleanIftrue, the search is case sensitive; iffalse, the search is case insensitive.

Return

Integer — the number of occurrences changed

Authorization

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

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

select()

Selects only theTextRange in the active presentation and removes any previousselection.

A script can only access the selection of the user who is running the script, and only ifthe script isbound to the presentation.

The parentPage of the correspondingShape or theTableCell is setas thecurrent page selection. TheShape or theTableCell is set as theselected page element.

    TheSelection can be of two types:
    • 1. Range of text, use select on a non-empty TextRange to select the range of the characters.
    • 2. Cursor position, use an empty TextRange to place the cursor at the desired index.
      constshape=SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0];shape.getText().setText('Hello');// Range selection: Select the text range "He".shape.getText().getRange(0,2).select();// Cursor selection: Place the cursor after "H" like "H|ello".shape.getText().getRange(1,1).select();

      Authorization

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

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

    setText(newText)

    Sets the text bounded by this range of the associated shape or table cell. The text maintainsthe styling of the start of the existing text.

    Parameters

    NameTypeDescription
    newTextStringThe string to set as the new text.

    Return

    TextRange — The text range representing the set text.

    Authorization

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

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

    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.