An element representing a rich text region. All text in a
Document
is contained within Text
elements. A Text
element can be contained within an Equation
,
EquationFunction
, ListItem
, or Paragraph
, but cannot itself contain any
other element. For more information on document structure, see the
guide to extending Google Docs.
var body = DocumentApp.getActiveDocument().getBody();
// Use editAsText to obtain a single text element containing
// all the characters in the document.
var text = body.editAsText();
// Insert text at the beginning of the document.
text.insertText(0, 'Inserted text.\n');
// Insert text at the end of the document.
text.appendText('\nAppended text.');
// Make the first half of the document blue.
text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');
Methods
Method | Return type | Brief description |
---|---|---|
appendText(text) | Text | Appends the specified text at the given character offset. |
copy() | Text | Returns a detached, deep copy of the current element. |
deleteText(startOffset, endOffsetInclusive) | Text | Deletes a range of text. |
editAsText() | Text | Obtains a Text version of the current element, for editing. |
findText(searchPattern) | RangeElement | Searches the contents of the element for the specified text pattern using regular expressions. |
findText(searchPattern, from) | RangeElement | Searches the contents of the element for the specified text pattern, starting from a given search result. |
getAttributes() | Object | Retrieves the element's attributes. |
getAttributes(offset) | Object | Retrieves the attributes at the specified character offset. |
getBackgroundColor() | String | Retrieves the background color setting. |
getBackgroundColor(offset) | String | Retrieves the background color at the specified character offset. |
getFontFamily() | FontFamily | Retrieves the font family setting. |
getFontFamily(offset) | FontFamily | Retrieves the font family at the specified character offset. |
getFontSize() | Integer | Retrieves the font size setting. |
getFontSize(offset) | Integer | Retrieves the font size at the specified character offset. |
getForegroundColor() | String | Retrieves the foreground color setting. |
getForegroundColor(offset) | String | Retrieves the foreground color at the specified character offset. |
getLinkUrl() | String | Retrieves the link url. |
getLinkUrl(offset) | String | Retrieves the link url at the specified character offset. |
getNextSibling() | Element | Retrieves the element's next sibling element. |
getParent() | ContainerElement | Retrieves the element's parent element. |
getPreviousSibling() | Element | Retrieves the element's previous sibling element. |
getText() | String | Retrieves the contents of the element as a text string. |
getTextAlignment() | TextAlignment | Gets the text alignment. |
getTextAlignment(offset) | TextAlignment | Gets the text alignment for a single character. |
getTextAttributeIndices() | Integer[] | Retrieves the set of text indices that correspond to the start of distinct text formatting runs. |
getType() | ElementType | Retrieves the element's ElementType . |
insertText(offset, text) | Text | Inserts the specified text at the given character offset. |
isAtDocumentEnd() | Boolean | Determines whether the element is at the end of the
Document . |
isBold() | Boolean | Retrieves the bold setting. |
isBold(offset) | Boolean | Retrieves the bold setting at the specified character offset. |
isItalic() | Boolean | Retrieves the italic setting. |
isItalic(offset) | Boolean | Retrieves the italic setting at the specified character offset. |
isStrikethrough() | Boolean | Retrieves the strikethrough setting. |
isStrikethrough(offset) | Boolean | Retrieves the strikethrough setting at the specified character offset. |
isUnderline() | Boolean | Retrieves the underline setting. |
isUnderline(offset) | Boolean | Retrieves the underline setting at the specified character offset. |
merge() | Text | Merges the element with the preceding sibling of the same type. |
removeFromParent() | Text | Removes the element from its parent. |
replaceText(searchPattern, replacement) | Element | Replaces all occurrences of a given text pattern with a given replacement string, using regular expressions. |
setAttributes(startOffset, endOffsetInclusive, attributes) | Text | Applies the specified attributes to the given character range. |
setAttributes(attributes) | Text | Sets the element's attributes. |
setBackgroundColor(startOffset, endOffsetInclusive, color) | Text | Sets the background color for the specified character range. |
setBackgroundColor(color) | Text | Sets the background color. |
setBold(bold) | Text | Sets the bold setting. |
setBold(startOffset, endOffsetInclusive, bold) | Text | Sets the bold setting for the specified character range. |
setFontFamily(family) | Text | Sets the font family. |
setFontFamily(startOffset, endOffsetInclusive, family) | Text | Sets the font family for the specified character range. |
setFontSize(size) | Text | Sets the font size. |
setFontSize(startOffset, endOffsetInclusive, size) | Text | Sets the font size for the specified character range. |
setForegroundColor(startOffset, endOffsetInclusive, color) | Text | Sets the foreground color for the specified character range. |
setForegroundColor(color) | Text | Sets the foreground color. |
setItalic(italic) | Text | Sets the italic setting. |
setItalic(startOffset, endOffsetInclusive, italic) | Text | Sets the italic setting for the specified character range. |
setLinkUrl(startOffset, endOffsetInclusive, url) | Text | Sets the link url for the specified character range. |
setLinkUrl(url) | Text | Sets the link url. |
setStrikethrough(strikethrough) | Text | Sets the strikethrough setting. |
setStrikethrough(startOffset, endOffsetInclusive, strikethrough) | Text | Sets the strikethrough setting for the specified character range. |
setText(text) | Text | Sets the text contents. |
setTextAlignment(startOffset, endOffsetInclusive, textAlignment) | Text | Sets the text alignment for a given character range. |
setTextAlignment(textAlignment) | Text | Sets the text alignment. |
setUnderline(underline) | Text | Sets the underline setting. |
setUnderline(startOffset, endOffsetInclusive, underline) | Text | Sets the underline setting for the specified character range. |
Detailed documentation
appendText(text)
Appends the specified text at the given character offset.
Parameters
Name | Type | Description |
---|---|---|
text | String | the text to append |
Return
Text
— the current element
copy()
Returns a detached, deep copy of the current element.
Any child elements present in the element are also copied. The new element will not have a parent.
Return
Text
— the new copy
deleteText(startOffset, endOffsetInclusive)
Deletes a range of text.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the character offset of the first character to delete |
endOffsetInclusive | Integer | the character offset of the last character to delete |
Return
Text
— the current element
editAsText()
Obtains a Text
version of the current element, for editing.
Use editAsText
for manipulating the elements contents as rich
text. The editAsText
mode ignores non-text elements (such as
InlineImage
and HorizontalRule
).
Child elements fully contained within a deleted text range are removed from the element.
var body = DocumentApp.getActiveDocument().getBody();
// Insert two paragraphs separated by a paragraph containing an
// horizontal rule.
body.insertParagraph(0, "An editAsText sample.");
body.insertHorizontalRule(0);
body.insertParagraph(0, "An example.");
// Delete " sample.\n\n An" removing the horizontal rule in the process.
body.editAsText().deleteText(14, 25);
Return
Text
— a text version of the current element
findText(searchPattern)
Searches the contents of the element for the specified text pattern using regular expressions.
A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.
The provided regular expression pattern is independently matched against each text block contained in the current element.
Parameters
Name | Type | Description |
---|---|---|
searchPattern | String | the pattern to search for |
Return
RangeElement
— a search result indicating the position of the search text, or null
if there is no match
findText(searchPattern, from)
Searches the contents of the element for the specified text pattern, starting from a given search result.
A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.
The provided regular expression pattern is independently matched against each text block contained in the current element.
Parameters
Name | Type | Description |
---|---|---|
searchPattern | String | the pattern to search for |
from | RangeElement | the search result to search from |
Return
RangeElement
— a search result indicating the next position of the search text, or
null if there is no match
getAttributes()
Retrieves the element's attributes.
The result is an object containing a property for each valid element
attribute where each property name corresponds to an item in the
DocumentApp.Attribute
enumeration.
var body = DocumentApp.getActiveDocument().getBody();
// Append a styled paragraph.
var par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);
// Retrieve the paragraph's attributes.
var atts = par.getAttributes();
// Log the paragraph attributes.
for (var att in atts) {
Logger.log(att + ":" + atts[att]);
}
Return
Object
— the element's attributes
getAttributes(offset)
Retrieves the attributes at the specified character offset.
The result is an object containing a property for each valid text
attribute where each property name corresponds to an item in the
DocumentApp.Attribute
enumeration.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
Object
— the element's attributes
getBackgroundColor()
Retrieves the background color setting.
Return
String
— the background color, or null if the element contains multiple
values for this attribute
getBackgroundColor(offset)
Retrieves the background color at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
String
— the background color
getFontFamily()
Retrieves the font family setting.
Return
FontFamily
— the font family, or null if the element contains multiple values
for this attribute
getFontFamily(offset)
Retrieves the font family at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
FontFamily
— the font family
getFontSize()
Retrieves the font size setting.
Return
Integer
— the font size, or null if the element contains multiple values
for this attribute
getFontSize(offset)
Retrieves the font size at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
Integer
— the font size
getForegroundColor()
Retrieves the foreground color setting.
Return
String
— the foreground color, or null if the element contains multiple
values for this attribute
getForegroundColor(offset)
Retrieves the foreground color at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
String
— the foreground color
getLinkUrl()
Retrieves the link url.
Return
String
— the link url, or null if the element contains multiple values
for this attribute
getLinkUrl(offset)
Retrieves the link url at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
String
— the link url
getNextSibling()
Retrieves the element's next sibling element.
The next sibling has the same parent and follows the current element.
Return
Element
— the next sibling element
getParent()
Retrieves the element's parent element.
The parent element contains the current element.
Return
ContainerElement
— the parent element
getPreviousSibling()
Retrieves the element's previous sibling element.
The previous sibling has the same parent and precedes the current element.
Return
Element
— the previous sibling element
getText()
Retrieves the contents of the element as a text string.
Return
String
— the contents of the element as text string
getTextAlignment()
Gets the text alignment. The available types of alignment are
DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
, and
DocumentApp.TextAlignment.SUPERSCRIPT
.
Return
TextAlignment
— the type of text alignment, or null
if the text contains multiple types of
text alignments or if the text alignment has never been set
getTextAlignment(offset)
Gets the text alignment for a single character. The available types of alignment are
DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
, and
DocumentApp.TextAlignment.SUPERSCRIPT
.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the offset of the character |
Return
TextAlignment
— the type of text alignment, or null
if the text alignment has never been set
getTextAttributeIndices()
Retrieves the set of text indices that correspond to the start of distinct text formatting runs.
Return
Integer[]
— the set of text indices at which text formatting changes
getType()
Retrieves the element's ElementType
.
Use getType()
to determine the exact type of a given element.
var body = DocumentApp.getActiveDocument().getBody();
// Obtain the first element in the document body.
var firstChild = body.getChild(0);
// Use getType() to determine the element's type.
if (firstChild.getType() == DocumentApp.ElementType.PARAGRAPH) {
Logger.log('The first element is a paragraph.');
} else {
Logger.log('The first element is not a paragraph.');
}
Return
ElementType
— the element type
insertText(offset, text)
Inserts the specified text at the given character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset at which to insert |
text | String | the text to insert |
Return
Text
— the current element
isAtDocumentEnd()
Determines whether the element is at the end of the
Document
.
Return
Boolean
— whether the element is at the end of the document
isBold()
Retrieves the bold setting.
Return
Boolean
— whether the text is bold, or null if the element contains multiple
values for this attribute
isBold(offset)
Retrieves the bold setting at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
Boolean
— the bold setting
isItalic()
Retrieves the italic setting.
Return
Boolean
— whether the text is italic, or null if the element contains
multiple values for this attribute
isItalic(offset)
Retrieves the italic setting at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
Boolean
— the italic setting
isStrikethrough()
Retrieves the strikethrough setting.
Return
Boolean
— whether the text is strikethrough, or null if the element contains
multiple values for this attribute
isStrikethrough(offset)
Retrieves the strikethrough setting at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
Boolean
— the strikethrough setting
isUnderline()
Retrieves the underline setting.
Return
Boolean
— whether the text is underlined, or null if the element contains
multiple values for this attribute
isUnderline(offset)
Retrieves the underline setting at the specified character offset.
Parameters
Name | Type | Description |
---|---|---|
offset | Integer | the character offset |
Return
Boolean
— the underline setting
merge()
Merges the element with the preceding sibling of the same type.
Only elements of the same ElementType
may be merged. Any
child elements contained in the current element are moved to the preceding
sibling element.
The current element is removed from the document.
var body = DocumentApp.getActiveDocument().getBody();
// Append two paragraphs to the document.
var par1 = body.appendParagraph('Paragraph 1.');
var par2 = body.appendParagraph('Paragraph 2.');
// Merge the newly added paragraphs into a single paragraph.
par2.merge();
Return
Text
— the merged element
removeFromParent()
Removes the element from its parent.
var body = DocumentApp.getActiveDocument().getBody();
// Remove all images in the document body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
imgs[i].removeFromParent();
}
Return
Text
— the removed element
replaceText(searchPattern, replacement)
Replaces all occurrences of a given text pattern with a given replacement string, using regular expressions.
A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.
The provided regular expression pattern is independently matched against each text block contained in the current element.
var body = DocumentApp.getActiveDocument().getBody();
// Clear the text surrounding "Apps Script", with or without text.
body.replaceText("^.*Apps ?Script.*$", "Apps Script");
Parameters
Name | Type | Description |
---|---|---|
searchPattern | String | the regex pattern to search for |
replacement | String | the text to use as replacement |
Return
Element
— the current element
setAttributes(startOffset, endOffsetInclusive, attributes)
Applies the specified attributes to the given character range.
The specified attributes parameter must be an object where each
property name is an item in the DocumentApp.Attribute
enumeration
and each property value is the new value to be applied.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
attributes | Object | the element's attributes |
Return
Text
— the current element
setAttributes(attributes)
Sets the element's attributes.
The specified attributes parameter must be an object where each
property name is an item in the DocumentApp.Attribute
enumeration
and each property value is the new value to be applied.
var body = DocumentApp.getActiveDocument().getBody();
// Define a custom paragraph style.
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] =
DocumentApp.FontFamily.CALIBRI;
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;
// Append a plain paragraph.
var par = body.appendParagraph('A paragraph with custom style.');
// Apply the custom style.
par.setAttributes(style);
Parameters
Name | Type | Description |
---|---|---|
attributes | Object | the element's attributes |
Return
Text
— the current element
setBackgroundColor(startOffset, endOffsetInclusive, color)
Sets the background color for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
color | String | the background color |
Return
Text
— the current element
setBackgroundColor(color)
Sets the background color.
Parameters
Name | Type | Description |
---|---|---|
color | String | the background color |
Return
Text
— the current element
setBold(bold)
Sets the bold setting.
Parameters
Name | Type | Description |
---|---|---|
bold | Boolean | the bold setting |
Return
Text
— the current element
setBold(startOffset, endOffsetInclusive, bold)
Sets the bold setting for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
bold | Boolean | the bold setting |
Return
Text
— the current element
setFontFamily(family)
Sets the font family.
Parameters
Name | Type | Description |
---|---|---|
family | FontFamily | the font family |
Return
Text
— the current element
setFontFamily(startOffset, endOffsetInclusive, family)
Sets the font family for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
family | FontFamily | the font family |
Return
Text
— the current element
setFontSize(size)
Sets the font size.
Parameters
Name | Type | Description |
---|---|---|
size | Integer | the font size |
Return
Text
— the current element
setFontSize(startOffset, endOffsetInclusive, size)
Sets the font size for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
size | Integer | the font size |
Return
Text
— the current element
setForegroundColor(startOffset, endOffsetInclusive, color)
Sets the foreground color for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
color | String | the foreground color |
Return
Text
— the current element
setForegroundColor(color)
Sets the foreground color.
Parameters
Name | Type | Description |
---|---|---|
color | String | the foreground color |
Return
Text
— the current element
setItalic(italic)
Sets the italic setting.
Parameters
Name | Type | Description |
---|---|---|
italic | Boolean | the italic setting |
Return
Text
— the current element
setItalic(startOffset, endOffsetInclusive, italic)
Sets the italic setting for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
italic | Boolean | the italic setting |
Return
Text
— the current element
setLinkUrl(startOffset, endOffsetInclusive, url)
Sets the link url for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
url | String | the link url |
Return
Text
— the current element
setLinkUrl(url)
Sets the link url.
Parameters
Name | Type | Description |
---|---|---|
url | String | the link url |
Return
Text
— the current element
setStrikethrough(strikethrough)
Sets the strikethrough setting.
Parameters
Name | Type | Description |
---|---|---|
strikethrough | Boolean | the strikethrough setting |
Return
Text
— the current element
setStrikethrough(startOffset, endOffsetInclusive, strikethrough)
Sets the strikethrough setting for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
strikethrough | Boolean | the strikethrough setting |
Return
Text
— the current element
setText(text)
Sets the text contents.
Parameters
Name | Type | Description |
---|---|---|
text | String | the new text contents |
Return
Text
— the current element
setTextAlignment(startOffset, endOffsetInclusive, textAlignment)
Sets the text alignment for a given character range. The available types of alignment are
DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
, and
DocumentApp.TextAlignment.SUPERSCRIPT
.
// Make the first character in the first paragraph be superscript.
var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(0, 0, DocumentApp.TextAlignment.SUPERSCRIPT);
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the start offset of the character range |
endOffsetInclusive | Integer | the end offset of the character range (inclusive) |
textAlignment | TextAlignment | the type of text alignment to apply |
Return
Text
— the current element
setTextAlignment(textAlignment)
Sets the text alignment. The available types of alignment are
DocumentApp.TextAlignment.NORMAL
, DocumentApp.TextAlignment.SUBSCRIPT
, and
DocumentApp.TextAlignment.SUPERSCRIPT
.
// Make the entire first paragraph be superscript.
var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
Parameters
Name | Type | Description |
---|---|---|
textAlignment | TextAlignment | the type of text alignment to apply |
Return
Text
— the current element
setUnderline(underline)
Sets the underline setting.
Parameters
Name | Type | Description |
---|---|---|
underline | Boolean | the underline setting |
Return
Text
— the current element
setUnderline(startOffset, endOffsetInclusive, underline)
Sets the underline setting for the specified character range.
Parameters
Name | Type | Description |
---|---|---|
startOffset | Integer | the text range's start offset |
endOffsetInclusive | Integer | the text range's end offset |
underline | Boolean | the underline setting |
Return
Text
— the current element