You can edit and style text using text ranges, which are represented by the
TextRange
type. A TextRange
represents a segment of text within a shape or
within a table cell. Calling getText()
on a shape or table cell returns a
text range that covers the entire text.
If you use methods that edit how text fits within a shape, any autofit settings applied to the shape are deactivated.
Using text ranges
A text range has two indexes that delimit the segment of text
covered by a text range: the start index and end index. You can determine
these indexes using the getStartIndex()
and getEndIndex()
functions.
To read the contents of a text range, use the asString()
or
asRenderedString()
functions.
To retrieve a subrange from within a text range, use the getRange()
function.
The following script creates a text box on the first slide and sets its text content to "Hello world!". It then retrieves a subrange that spans just the "Hello".
The text range returned by a shape or table cell will always cover entire text, even if text is inserted and deleted. So the above example produces the following log statements:
Start: 0; End: 13; Content: Hello world! Start: 0; End: 5; Content: Hello
Inserting and deleting text
You can also insert and delete text shapes and table cells using a text ranges.
insertText()
andappendText()
let you insert text.setText()
replaces a text range's text with the provided text.clear()
deletes text from within a text range.
The following script demonstrates the use of these functions:
This script creates a text box on the first slide and sets its text content to "Hello world!". It then deletes characters 6 through 11 ("world"), and inserts the text "galaxy" at index 6 instead. The above example produces the following log statement:
Start: 0; End: 14; Content: Hello galaxy!
Search and replace
Use the replaceAllText()
function on presentation or page to perform a global
find and replace across the entire presentation or a specific page.
The find()
function on TextRange returns the instances of a string within the
range. It can be used along with setText()
for performing find-and-replace
within a shape or table cell.
Paragraphs, list items, and runs
TextRange
provides functions to return useful collections of text entities.
Some of these functions include:
getParagraphs(),
which provides all paragraphs which overlap the text range. A paragraph is a sequence of text which terminates with the newline character, "\n".getListParagraphs(),
which returns the list items in the current text range.getRuns(),
which provides the text runs that overlap the current text range. A text run is a segment of text where all the characters have the same text style.
Text styling
Text style determines the rendering of text characters in your presentation, including font, color, and hyperlinking.
The getTextStyle()
function of a text range provides a TextStyle
object used for
styling text. The TextStyle
object covers the same text as its parent TextRange
.
The above example first creates a text box on the first slide and sets its
content to "Hello ". Then it appends the text "world!". The newly appended text
is bolded, linked to www.example.com
, and its color is set
to red.
When reading styles, the function returns null if the range has multiple values for the style. So the above sample produces the following log statements:
Text: Hello; Bold: false Text: world!; Bold: true Text: Hello world!; Bold: null
There are many other styles that can be applied to text. More details can be
found in the TextStyle
reference documentation.
Paragraph styling
Paragraph styles apply to entire paragraphs, and include things like text alignment and line
spacing. The getParagraphStyle() function in TextRange
provides a ParagraphStyle
object for styling all the paragraphs that overlap the parent text range.
The following example creates a text box on the first slide with four paragraphs, then center aligns the first three paragraphs.
List styling
Similar to ParagraphStyle
, ListStyle
can be used for styling all paragraphs
which overlap the parent text range.
The above example creates a text box on the first slide, containing four paragraphs: the second paragraph is indented once and the third paragraph is indented twice. Then it applies a list preset to the all the paragraphs. Finally, each paragraph's nesting level is logged. (The paragraph's nesting level comes from the number of tabs before the paragraph's text.) So above script produces the following log statements:
Paragraph 1's nesting level: 0 Paragraph 2's nesting level: 1 Paragraph 3's nesting level: 2 Paragraph 4's nesting level: 0