Editing and styling text

  • Text ranges, represented by theTextRange type, allow you to edit and style segments of text within shapes or table cells.

  • You can determine a text range's start and end indexes usinggetStartIndex() andgetEndIndex(), and read its contents withasString() orasRenderedString().

  • Text ranges provide functions for inserting, deleting, and replacing text, includinginsertText(),appendText(),setText(), andclear().

  • You can search for and replace text globally usingreplaceAllText() on a presentation or page, or within a text range usingfind() andsetText().

  • Text ranges allow access to collections of text entities like paragraphs, list items, and runs, and enable styling throughTextStyle,ParagraphStyle, andListStyle objects.

You can edit and style text usingtext ranges, which are represented by theTextRange type. ATextRange represents a segment of text within a shape orwithin a table cell. CallinggetText() on a shape or table cell returns atext range that covers the entire text.

If you use methods that edit how text fits within a shape, any autofit settingsapplied to the shape are deactivated.

Using text ranges

A text range has two indexes that delimit the segment of textcovered by a text range: thestart index andend index. You can determinethese indexes using thegetStartIndex() andgetEndIndex() functions.

A text range's start index is inclusive, and its end indexis exclusive. Both indexes are zero based.

To read the contents of a text range, use theasString() orasRenderedString() functions.

To retrieve a subrange from within a text range, use thegetRange() function.

The following script creates a text box on the first slide and sets its text contentto "Hello world!". It then retrieves a subrange that spans just the "Hello".

try{// Get the first slide of active presentationconstslide=SlidesApp.getActivePresentation().getSlides()[0];// Insert shape in the slide with dimensionsconstshape=slide.insertShape(SlidesApp.ShapeType.TEXT_BOX,100,200,300,60,);consttextRange=shape.getText();// Set text in TEXT_BOXtextRange.setText("Hello world!");console.log(`Start:${textRange.getStartIndex()}; End:${textRange.getEndIndex()}; Content:${textRange.asString()}`,);constsubRange=textRange.getRange(0,5);console.log(`Sub-range Start:${subRange.getStartIndex()}; Sub-range End:${subRange.getEndIndex()}; Sub-range Content:${subRange.asString()}`,);}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed with an error %s ",err.message);}

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 thefollowing 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 atext 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:

slides/style/style.gs
try{// Get the first slide of active presentationconstslide=SlidesApp.getActivePresentation().getSlides()[0];// Insert shape in the slide with dimensionsconstshape=slide.insertShape(SlidesApp.ShapeType.TEXT_BOX,100,200,300,60,);consttextRange=shape.getText();textRange.setText("Hello world!");textRange.clear(6,11);// Insert text in TEXT_BOXtextRange.insertText(6,"galaxy");console.log(`Start:${textRange.getStartIndex()}; End:${textRange.getEndIndex()}; Content:${textRange.asString()}`,);}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed with an error %s ",err.message);}

This script creates a text box on the first slide and sets its text contentto "Hello world!". It then deletes characters 6 through 11 ("world"), andinserts the text "galaxy" at index 6 instead. The above example produces thefollowing log statement:

Start: 0; End: 14; Content: Hello galaxy!

Search and replace

Use thereplaceAllText() function on presentation or page to perform a globalfind and replace across the entire presentation or a specific page.

Thefind() function on TextRange returns the instances of a string within therange. It can be used along withsetText() for performing find-and-replacewithin 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. Aparagraph 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. Atext run is a segment of text where all the characters have the same textstyle.

Text styling

Text style determines the rendering of text characters in your presentation,including font, color, and hyperlinking.

ThegetTextStyle() function of a text range provides aTextStyle object used forstyling text. TheTextStyle object covers the same text as its parentTextRange.

slides/style/style.gs
try{// Get the first slide of active presentationconstslide=SlidesApp.getActivePresentation().getSlides()[0];// Insert shape in the slide with dimensionsconstshape=slide.insertShape(SlidesApp.ShapeType.TEXT_BOX,100,200,300,60,);consttextRange=shape.getText();// Set text in TEXT_BOXtextRange.setText("Hello ");// Append text in TEXT_BOXconstinsertedText=textRange.appendText("world!");// Style the text with url,boldinsertedText.getTextStyle().setBold(true).setLinkUrl("www.example.com").setForegroundColor("#ff0000");consthelloRange=textRange.getRange(0,5);console.log(`Text:${helloRange.asString()}; Bold:${helloRange.getTextStyle().isBold()}`,);console.log(`Text:${insertedText.asString()}; Bold:${insertedText.getTextStyle().isBold()}`,);console.log(`Text:${textRange.asString()}; Bold:${textRange.getTextStyle().isBold()}`,);}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed with an error %s ",err.message);}

The above example first creates a text box on the first slide and sets itscontent to "Hello ". Then it appends the text "world!". The newly appended textis bolded, linked towww.example.com, and its color is setto red.

When reading styles, the function returns null if the range has multiple valuesfor the style. So the above sample produces the following log statements:

Text: Hello; Bold: falseText: world!; Bold: trueText: Hello world!; Bold: null

There are many other styles that can be applied to text. More details can befound in theTextStyle reference documentation.

Paragraph styling

Paragraph styles apply to entire paragraphs, and include things like text alignment and linespacing. The getParagraphStyle() function inTextRange provides aParagraphStyleobject for styling all the paragraphs that overlap the parent text range.

The following example creates a text box on the first slide with fourparagraphs, then center aligns the first three paragraphs.

slides/style/style.gs
try{// Get the first slide of active presentationconstslide=SlidesApp.getActivePresentation().getSlides()[0];// Insert shape in the slide with dimensionsconstshape=slide.insertShape(SlidesApp.ShapeType.TEXT_BOX,50,50,300,300,);consttextRange=shape.getText();// Set the text in the shape/TEXT_BOXtextRange.setText("Paragraph 1\nParagraph2\nParagraph 3\nParagraph 4");constparagraphs=textRange.getParagraphs();// Style the paragraph alignment center.for(leti=0;i<=3;i++){constparagraphStyle=paragraphs[i].getRange().getParagraphStyle();paragraphStyle.setParagraphAlignment(SlidesApp.ParagraphAlignment.CENTER);}}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed with an error %s ",err.message);}

List styling

Similar toParagraphStyle,ListStyle can be used for styling all paragraphswhich overlap the parent text range.

slides/style/style.gs
try{// Get the first slide of active presentationconstslide=SlidesApp.getActivePresentation().getSlides()[0];// Insert shape in the slide with dimensionsconstshape=slide.insertShape(SlidesApp.ShapeType.TEXT_BOX,50,50,300,300,);// Add and style the listconsttextRange=shape.getText();textRange.appendText("Item 1\n").appendText("\tItem 2\n").appendText("\t\tItem 3\n").appendText("Item 4");// Preset patterns of glyphs for lists in text.textRange.getListStyle().applyListPreset(SlidesApp.ListPreset.DIGIT_ALPHA_ROMAN);constparagraphs=textRange.getParagraphs();for(leti=0;i <paragraphs.length;i++){constlistStyle=paragraphs[i].getRange().getListStyle();console.log(`Paragraph${i+1}'s nesting level:${listStyle.getNestingLevel()}`,);}}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed with an error %s ",err.message);}

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 indentedtwice. Then it applies a list preset to the all the paragraphs. Finally, eachparagraph's nesting level is logged. (The paragraph's nesting level comes fromthe number of tabs before the paragraph's text.) So above script produces thefollowing log statements:

Paragraph 1's nesting level: 0Paragraph 2's nesting level: 1Paragraph 3's nesting level: 2Paragraph 4's nesting level: 0

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-11-14 UTC.