Text

An element representing a rich text region. All text in a `https://developers.google.com/apps-script/reference/document/document` is contained within `Text` elements.
A `Text` element can be contained within an `https://developers.google.com/apps-script/reference/document/equation`, `https://developers.google.com/apps-script/reference/document/equation-function`,
`https://developers.google.com/apps-script/reference/document/list-item`, or `https://developers.google.com/apps-script/reference/document/paragraph`, but cannot itself contain any other element. For more
information on document structure, see the [guide to extending Google Docs](https://developers.google.com/apps-script/guides/docs#structure_of_a_document).

```javascript
// Gets the body contents of the active tab.
const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Use editAsText to obtain a single text element containing
// all the characters in the tab.
const text = body.editAsText();

// Insert text at the beginning of the tab.
text.insertText(0, 'Inserted text.\n');

// Insert text at the end of the tab.
text.appendText('\nAppended text.');

// Make the first half of the tab blue.
text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');
```

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/document/text#appendText(String)` | `https://developers.google.com/apps-script/reference/document/text#` | Adds the specified text to the end of this text region. |
| `https://developers.google.com/apps-script/reference/document/text#copy()` | `https://developers.google.com/apps-script/reference/document/text#` | Returns a detached, deep copy of the current element. |
| `https://developers.google.com/apps-script/reference/document/text#deleteText(Integer,Integer)` | `https://developers.google.com/apps-script/reference/document/text#` | Deletes a range of text. |
| `https://developers.google.com/apps-script/reference/document/text#editAsText()` | `https://developers.google.com/apps-script/reference/document/text#` | Obtains a `https://developers.google.com/apps-script/reference/document/text#` version of the current element, for editing. |
| `https://developers.google.com/apps-script/reference/document/text#findText(String)` | `https://developers.google.com/apps-script/reference/document/range-element|null` | Searches the contents of the element for the specified text pattern using regular expressions. |
| `https://developers.google.com/apps-script/reference/document/text#findText(String,RangeElement)` | `https://developers.google.com/apps-script/reference/document/range-element|null` | Searches the contents of the element for the specified text pattern, starting from a given search result. |
| `https://developers.google.com/apps-script/reference/document/text#getAttributes()` | `Object` | Retrieves the element's attributes. |
| `https://developers.google.com/apps-script/reference/document/text#getAttributes(Integer)` | `Object` | Retrieves the attributes at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#getBackgroundColor()` | `String|null` | Retrieves the background color setting. |
| `https://developers.google.com/apps-script/reference/document/text#getBackgroundColor(Integer)` | `String|null` | Retrieves the background color at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#getFontFamily()` | `String|null` | Retrieves the font family setting. |
| `https://developers.google.com/apps-script/reference/document/text#getFontFamily(Integer)` | `String|null` | Retrieves the font family at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#getFontSize()` | `Number|null` | Retrieves the font size setting. |
| `https://developers.google.com/apps-script/reference/document/text#getFontSize(Integer)` | `Number|null` | Retrieves the font size at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#getForegroundColor()` | `String|null` | Retrieves the foreground color setting. |
| `https://developers.google.com/apps-script/reference/document/text#getForegroundColor(Integer)` | `String|null` | Retrieves the foreground color at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#getLinkUrl()` | `String|null` | Retrieves the link url. |
| `https://developers.google.com/apps-script/reference/document/text#getLinkUrl(Integer)` | `String|null` | Retrieves the link URL at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#getNextSibling()` | `https://developers.google.com/apps-script/reference/document/element|null` | Retrieves the element's next sibling element. |
| `https://developers.google.com/apps-script/reference/document/text#getParent()` | `https://developers.google.com/apps-script/reference/document/container-element|null` | Retrieves the element's parent element. |
| `https://developers.google.com/apps-script/reference/document/text#getPreviousSibling()` | `https://developers.google.com/apps-script/reference/document/element|null` | Retrieves the element's previous sibling element. |
| `https://developers.google.com/apps-script/reference/document/text#getText()` | `String` | Retrieves the contents of the element as a text string. |
| `https://developers.google.com/apps-script/reference/document/text#getTextAlignment()` | `https://developers.google.com/apps-script/reference/document/text-alignment|null` | Gets the text alignment. |
| `https://developers.google.com/apps-script/reference/document/text#getTextAlignment(Integer)` | `https://developers.google.com/apps-script/reference/document/text-alignment|null` | Gets the text alignment for a single character. |
| `https://developers.google.com/apps-script/reference/document/text#getTextAttributeIndices()` | `Integer[]` | Retrieves the set of text indices that correspond to the start of distinct text formatting runs. |
| `https://developers.google.com/apps-script/reference/document/text#getType()` | `https://developers.google.com/apps-script/reference/document/element-type` | Retrieves the element's `https://developers.google.com/apps-script/reference/document/element-type`. |
| `https://developers.google.com/apps-script/reference/document/text#insertText(Integer,String)` | `https://developers.google.com/apps-script/reference/document/text#` | Inserts the specified text at the given character offset. |
| `https://developers.google.com/apps-script/reference/document/text#isAtDocumentEnd()` | `Boolean` | Determines whether the element is at the end of the `https://developers.google.com/apps-script/reference/document/document`. |
| `https://developers.google.com/apps-script/reference/document/text#isBold()` | `Boolean|null` | Retrieves the bold setting. |
| `https://developers.google.com/apps-script/reference/document/text#isBold(Integer)` | `Boolean|null` | Retrieves the bold setting at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#isItalic()` | `Boolean|null` | Retrieves the italic setting. |
| `https://developers.google.com/apps-script/reference/document/text#isItalic(Integer)` | `Boolean|null` | Retrieves the italic setting at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#isStrikethrough()` | `Boolean|null` | Retrieves the strikethrough setting. |
| `https://developers.google.com/apps-script/reference/document/text#isStrikethrough(Integer)` | `Boolean|null` | Retrieves the strikethrough setting at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#isUnderline()` | `Boolean|null` | Retrieves the underline setting. |
| `https://developers.google.com/apps-script/reference/document/text#isUnderline(Integer)` | `Boolean|null` | Retrieves the underline setting at the specified character offset. |
| `https://developers.google.com/apps-script/reference/document/text#merge()` | `https://developers.google.com/apps-script/reference/document/text#|null` | Merges the element with the preceding sibling of the same type. |
| `https://developers.google.com/apps-script/reference/document/text#removeFromParent()` | `https://developers.google.com/apps-script/reference/document/text#|null` | Removes the element from its parent. |
| `https://developers.google.com/apps-script/reference/document/text#replaceText(String,String)` | `https://developers.google.com/apps-script/reference/document/element` | Replaces all occurrences of a given text pattern with a given replacement string, using regular expressions. |
| `https://developers.google.com/apps-script/reference/document/text#setAttributes(Integer,Integer,Object)` | `https://developers.google.com/apps-script/reference/document/text#` | Applies the specified attributes to the given character range. |
| `https://developers.google.com/apps-script/reference/document/text#setAttributes(Object)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the element's attributes. |
| `https://developers.google.com/apps-script/reference/document/text#setBackgroundColor(Integer,Integer,String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the background color for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setBackgroundColor(String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the background color. |
| `https://developers.google.com/apps-script/reference/document/text#setBold(Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the bold setting. |
| `https://developers.google.com/apps-script/reference/document/text#setBold(Integer,Integer,Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the bold setting for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setFontFamily(Integer,Integer,String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the font family for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setFontFamily(String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the font family. |
| `https://developers.google.com/apps-script/reference/document/text#setFontSize(Integer,Integer,Number)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the font size for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setFontSize(Number)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the font size. |
| `https://developers.google.com/apps-script/reference/document/text#setForegroundColor(Integer,Integer,String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the foreground color for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setForegroundColor(String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the foreground color. |
| `https://developers.google.com/apps-script/reference/document/text#setItalic(Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the italic setting. |
| `https://developers.google.com/apps-script/reference/document/text#setItalic(Integer,Integer,Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the italic setting for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setLinkUrl(Integer,Integer,String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the link URL for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setLinkUrl(String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the link url. |
| `https://developers.google.com/apps-script/reference/document/text#setStrikethrough(Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the strikethrough setting. |
| `https://developers.google.com/apps-script/reference/document/text#setStrikethrough(Integer,Integer,Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the strikethrough setting for the specified character range. |
| `https://developers.google.com/apps-script/reference/document/text#setText(String)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the text contents. |
| `https://developers.google.com/apps-script/reference/document/text#setTextAlignment(Integer,Integer,TextAlignment)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the text alignment for a given character range. |
| `https://developers.google.com/apps-script/reference/document/text#setTextAlignment(TextAlignment)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the text alignment. |
| `https://developers.google.com/apps-script/reference/document/text#setUnderline(Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the underline setting. |
| `https://developers.google.com/apps-script/reference/document/text#setUnderline(Integer,Integer,Boolean)` | `https://developers.google.com/apps-script/reference/document/text#` | Sets the underline setting for the specified character range. |

## Detailed documentation

### `appendText(text)`

Adds the specified text to the end of this text region.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Adds the text, 'Sample body text,' to the end of the tab body.
const text = body.editAsText().appendText('Sample body text');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `text` | `String` | The text to append. |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `copy()`

Returns a detached, deep copy of the current element.

Any child elements present in the element are also copied. The new element doesn't have a
parent.

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The new copy.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `deleteText(startOffset, endOffsetInclusive)`

Deletes a range of text.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Deletes the first 10 characters in the body.
const text = body.editAsText().deleteText(0, 9);
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `editAsText()`

Obtains a `https://developers.google.com/apps-script/reference/document/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 `https://developers.google.com/apps-script/reference/document/inline-image` and `https://developers.google.com/apps-script/reference/document/horizontal-rule`).

Child elements fully contained within a deleted text range are removed from the element.

```javascript
const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().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


`https://developers.google.com/apps-script/reference/document/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


`https://developers.google.com/apps-script/reference/document/range-element|null` --- a search result indicating the position of the search text, or null if there is no
match

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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` | `https://developers.google.com/apps-script/reference/document/range-element` | the search result to search from |

#### Return


`https://developers.google.com/apps-script/reference/document/range-element|null` --- a search result indicating the next position of the search text, or null if there is no
match

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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.

```javascript
const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Append a styled paragraph.
const par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);

// Retrieve the paragraph's attributes.
const atts = par.getAttributes();

// Log the paragraph attributes.
for (const att in atts) {
  Logger.log(`${att}:${atts[att]}`);
}
```

#### Return


`Object` --- The element's attributes.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Declares style attributes.
const style = {};
style[DocumentApp.Attribute.BOLD] = true;
style[DocumentApp.Attribute.ITALIC] = true;
style[DocumentApp.Attribute.FONT_SIZE] = 29;

// Sets the style attributes to the tab's body.
const text = body.editAsText();
text.setAttributes(style);

// Gets the style attributes applied to the eleventh character in the
// body and logs them to the console.
const attributes = text.getAttributes(10);
console.log(attributes);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`Object` --- The element's attributes.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getBackgroundColor()`

Retrieves the background color setting.

#### Return


`String|null` --- the background color, formatted in CSS notation (like `'#ffffff'`), or null
if the element contains multiple values for this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getBackgroundColor(offset)`

Retrieves the background color at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the background color of the first 3 characters in the body.
const text = body.editAsText().setBackgroundColor(0, 2, '#FFC0CB');

// Gets the background color of the first character in the body.
const backgroundColor = text.getBackgroundColor(0);

// Logs the background color to the console.
console.log(backgroundColor);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`String|null` --- The background color, formatted in CSS notation (like `'#ffffff'`).

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getFontFamily()`

Retrieves the font family setting. The name can be any font from the Font menu in Docs or [Google Fonts](https://www.google.com/fonts), and is case-sensitive. The methods `getFontFamily()` and `https://developers.google.com/apps-script/reference/document/text#setFontFamily(String)` now use string names for fonts instead of
the `https://developers.google.com/apps-script/reference/document/font-family` enum. Although this enum is
deprecated, it will remain available for compatibility with older scripts.

#### Return


`String|null` --- the font family, or null if the element contains multiple values for this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getFontFamily(offset)`

Retrieves the font family at the specified character offset. The name can be any font from the
Font menu in Docs or [Google Fonts](https://www.google.com/fonts), and is
case-sensitive. The methods `getFontFamily()` and `setFontFamily(fontFamilyName)`
now use string names for fonts instead of the `https://developers.google.com/apps-script/reference/document/font-family` enum. Although this enum is
deprecated, it will remain available for compatibility with older scripts.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the font of the first 16 characters to Impact.
const text = body.editAsText().setFontFamily(0, 15, 'Impact');

// Gets the font family of the 16th character in the tab body.
const fontFamily = text.getFontFamily(15);

// Logs the font family to the console.
console.log(fontFamily);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`String|null` --- The font family.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getFontSize()`

Retrieves the font size setting.

#### Return


`Number|null` --- the font size, or null if the element contains multiple values for this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getFontSize(offset)`

Retrieves the font size at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the font size of the first 13 characters to 15.
const text = body.editAsText().setFontSize(0, 12, 15);

// Gets the font size of the first character.
const fontSize = text.getFontSize(0);

// Logs the font size to the console.
console.log(fontSize);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`Number|null` --- The font size.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getForegroundColor()`

Retrieves the foreground color setting.

#### Return


`String|null` --- the foreground color, formatted in CSS notation (like `'#ffffff'`), or null
if the element contains multiple values for this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getForegroundColor(offset)`

Retrieves the foreground color at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the foreground color of the first 3 characters in the tab body.
const text = body.editAsText().setForegroundColor(0, 2, '#0000FF');

// Gets the foreground color of the first character in the tab body.
const foregroundColor = text.getForegroundColor(0);

// Logs the foreground color to the console.
console.log(foregroundColor);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`String|null` --- The foreground color, formatted in CSS notation (like `'#ffffff'`).

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getLinkUrl()`

Retrieves the link url.

#### Return


`String|null` --- the link url, or null if the element contains multiple values for this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getLinkUrl(offset)`

Retrieves the link URL at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Applies a link to the first 10 characters in the body.
const text = body.editAsText().setLinkUrl(0, 9, 'https://www.example.com/');

// Gets the URL of the link from the first character.
const link = text.getLinkUrl(0);

// Logs the link URL to the console.
console.log(link);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`String|null` --- The link URL.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getNextSibling()`

Retrieves the element's next sibling element.

The next sibling has the same parent and follows the current element.

#### Return


`https://developers.google.com/apps-script/reference/document/element|null` --- The next sibling element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getParent()`

Retrieves the element's parent element.

The parent element contains the current element.

#### Return


`https://developers.google.com/apps-script/reference/document/container-element|null` --- The parent element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getPreviousSibling()`

Retrieves the element's previous sibling element.

The previous sibling has the same parent and precedes the current element.

#### Return


`https://developers.google.com/apps-script/reference/document/element|null` --- The previous sibling element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getText()`

Retrieves the contents of the element as a text string.

#### Return


`String` --- the contents of the element as text string

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getTextAlignment()`

Gets the text alignment. The available types of alignment are `DocumentApp.TextAlignment.NORMAL`, `DocumentApp.TextAlignment.SUBSCRIPT`, and `DocumentApp.TextAlignment.SUPERSCRIPT`.

#### Return


`https://developers.google.com/apps-script/reference/document/text-alignment|null` --- 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

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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`.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the text alignment of the tab's body to NORMAL.
const text =
    body.editAsText().setTextAlignment(DocumentApp.TextAlignment.NORMAL);

// Gets the text alignment of the ninth character.
const alignment = text.getTextAlignment(8);

// Logs the text alignment to the console.
console.log(alignment.toString());
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The offset of the character. |

#### Return


`https://developers.google.com/apps-script/reference/document/text-alignment|null` --- The type of text alignment, or `null` if the text alignment has never been set.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getTextAttributeIndices()`

Retrieves the set of text indices that correspond to the start of distinct text formatting
runs.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the text indices at which text formatting changes.
const indices = body.editAsText().getTextAttributeIndices();

// Logs the indices to the console.
console.log(indices.toString());
```

#### Return


`Integer[]` --- The set of text indices at which text formatting changes.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `getType()`

Retrieves the element's `https://developers.google.com/apps-script/reference/document/element-type`.

Use `getType()` to determine the exact type of a given element.

```javascript
const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Obtain the first element in the active tab's body.

const 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


`https://developers.google.com/apps-script/reference/document/element-type` --- The element type.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `insertText(offset, text)`

Inserts the specified text at the given character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Inserts the text, 'Sample inserted text', at the start of the body content.
const text = body.editAsText().insertText(0, 'Sample inserted text');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset at which to insert the text. |
| `text` | `String` | The text to insert. |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isAtDocumentEnd()`

Determines whether the element is at the end of the `https://developers.google.com/apps-script/reference/document/document`.

#### Return


`Boolean` --- Whether the element is at the end of the tab.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isBold()`

Retrieves the bold setting.

#### Return


`Boolean|null` --- whether the text is bold, or null if the element contains multiple values for this
attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isBold(offset)`

Retrieves the bold setting at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Bolds the first 4 characters in the tab body.
const text = body.editAsText().setBold(0, 3, true);

// Gets whether or not the text is bold.
const bold = text.editAsText().isBold(0);

// Logs the text's bold setting to the console
console.log(bold);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`Boolean|null` --- The bold setting.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isItalic()`

Retrieves the italic setting.

#### Return


`Boolean|null` --- whether the text is italic, or null if the element contains multiple values for this
attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isItalic(offset)`

Retrieves the italic setting at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 13 characters of the tab body to italic.
const text = body.editAsText().setItalic(0, 12, true);

// Gets whether the fifth character in the tab body is set to
// italic and logs it to the console.
const italic = text.isItalic(4);
console.log(italic);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`Boolean|null` --- The italic setting.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isStrikethrough()`

Retrieves the strikethrough setting.

#### Return


`Boolean|null` --- whether the text is strikethrough, or null if the element contains multiple values for
this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isStrikethrough(offset)`

Retrieves the strikethrough setting at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 17 characters of the tab body to strikethrough.
const text = body.editAsText().setStrikethrough(0, 16, true);

// Gets whether the first character in the tab body is set to
// strikethrough and logs it to the console.
const strikethrough = text.isStrikethrough(0);
console.log(strikethrough);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`Boolean|null` --- The strikethrough setting.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isUnderline()`

Retrieves the underline setting.

#### Return


`Boolean|null` --- whether the text is underlined, or null if the element contains multiple values for
this attribute

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `isUnderline(offset)`

Retrieves the underline setting at the specified character offset.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 13 characters of the tab body to underline.
const text = body.editAsText().setUnderline(0, 12, false);

// Gets whether the first character in the tab body is set to
// underline and logs it to the console
const underline = text.editAsText().isUnderline(0);
console.log(underline);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `offset` | `Integer` | The character offset. |

#### Return


`Boolean|null` --- The underline setting.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `merge()`

Merges the element with the preceding sibling of the same type.

Only elements of the same `https://developers.google.com/apps-script/reference/document/element-type` can 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.

```javascript
const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
const par1 = body.appendParagraph('Paragraph 1.');
const par2 = body.appendParagraph('Paragraph 2.');
// Merge the newly added paragraphs into a single paragraph.
par2.merge();

// Example 2: Merge table cells
// Create a two-dimensional array containing the table's cell contents.
const cells = [
  ['Row 1, Cell 1', 'Row 1, Cell 2'],
  ['Row 2, Cell 1', 'Row 2, Cell 2'],
];
// Build a table from the array.
const table = body.appendTable(cells);
// Get the first row in the table.
const row = table.getRow(0);
// Get the two cells in this row.
const cell1 = row.getCell(0);
const cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
const merged = cell2.merge();
```

#### Return


`https://developers.google.com/apps-script/reference/document/text#|null` --- The merged element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `removeFromParent()`

Removes the element from its parent.

```javascript
const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Remove all images in the active tab's body.
const imgs = body.getImages();
for (let i = 0; i < imgs.length; i++) {
  imgs[i].removeFromParent();
}
```

#### Return


`https://developers.google.com/apps-script/reference/document/text#|null` --- The removed element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `replaceText(searchPattern, replacement)`

Replaces all occurrences of a given text pattern with a given replacement string, using regular
expressions.

The search pattern is passed as a string, not a JavaScript regular expression object.
Because of this you'll need to escape any backslashes in the pattern.

This methods uses Google's [RE2](https://github.com/google/re2) regular
expression library, which limits the [supported syntax](https://github.com/google/re2/wiki/Syntax).

The provided regular expression pattern is independently matched against each text block
contained in the current element.

```javascript
const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().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


`https://developers.google.com/apps-script/reference/document/element` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Declares style attributes for font size and font family.
const style = {};
style[DocumentApp.Attribute.FONT_SIZE] = 20;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Impact';

// Sets the style attributes to the first 9 characters in the tab's body.
const text = body.setAttributes(0, 8, style);
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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.

```javascript
const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Define a custom paragraph style.
const style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
    DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;

// Append a plain paragraph.
const 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setBackgroundColor(startOffset, endOffsetInclusive, color)`

Sets the background color for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the background color of the first 3 characters in the
// tab body to hex color #0000FF.
const text = body.editAsText().setBackgroundColor(0, 2, '#0000FF');
```

#### 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, formatted in CSS notation (like `'#ffffff'`). |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setBackgroundColor(color)`

Sets the background color.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `color` | `String` | the background color, formatted in CSS notation (like `'#ffffff'`) |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setBold(bold)`

Sets the bold setting.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `bold` | `Boolean` | the bold setting |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setBold(startOffset, endOffsetInclusive, bold)`

Sets the bold setting for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 11 characters in the tab's body to bold.
const text = body.editAsText().setBold(0, 10, true);
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)`

Sets the font family for the specified character range. The name can be any font from the Font
menu in Docs or [Google Fonts](https://www.google.com/fonts), and is case-sensitive.
Unrecognized font names will render as Arial. The methods `https://developers.google.com/apps-script/reference/document/text#getFontFamily(Integer)` and
`setFontFamily(fontFamilyName)` now use string names for fonts instead of the `https://developers.google.com/apps-script/reference/document/font-family` enum. Although this enum is
deprecated, it will remain available for compatibility with older scripts.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the font of the first 4 characters in the tab's body to Roboto.
const text = body.editAsText().setFontFamily(0, 3, 'Roboto');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `startOffset` | `Integer` | The text range's start offset. |
| `endOffsetInclusive` | `Integer` | The text range's end offset. |
| `fontFamilyName` | `String` | The name of the font family, from the Font menu in Docs or Google Fonts. |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setFontFamily(fontFamilyName)`

Sets the font family. The name can be any font from the Font menu in Docs or [Google Fonts](https://www.google.com/fonts), and is case-sensitive. Unrecognized font
names will render as Arial. The methods `https://developers.google.com/apps-script/reference/document/text#getFontFamily()` and `setFontFamily(fontFamilyName)` now use string names for fonts instead of the `https://developers.google.com/apps-script/reference/document/font-family` enum. Although this enum is
deprecated, it will remain available for compatibility with older scripts.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `fontFamilyName` | `String` | the name of the font family, from the Font menu in Docs or Google Fonts |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setFontSize(startOffset, endOffsetInclusive, size)`

Sets the font size for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the size of the first 11 characters in the tab's body to 12.
const text = body.editAsText().setFontSize(0, 10, 12);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `startOffset` | `Integer` | The text range's start offset. |
| `endOffsetInclusive` | `Integer` | The text range's end offset. |
| `size` | `Number` | The font size. |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setFontSize(size)`

Sets the font size.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `size` | `Number` | the font size |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setForegroundColor(startOffset, endOffsetInclusive, color)`

Sets the foreground color for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the foreground color of the first 2 characters in the
// tab's body to hex color #FF0000.
const text = body.editAsText().setForegroundColor(0, 1, '#FF0000');

// Gets the foreground color for the second character in the tab's body.
const foregroundColor = text.getForegroundColor(1);

//  Logs the foreground color to the console.
console.log(foregroundColor);
```

#### 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, formatted in CSS notation (like `'#ffffff'`). |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setForegroundColor(color)`

Sets the foreground color.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `color` | `String` | the foreground color, formatted in CSS notation (like `'#ffffff'`) |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setItalic(italic)`

Sets the italic setting.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `italic` | `Boolean` | the italic setting |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setItalic(startOffset, endOffsetInclusive, italic)`

Sets the italic setting for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 11 characters in the tab's body to italic.
const text = body.editAsText().setItalic(0, 10, true);
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setLinkUrl(startOffset, endOffsetInclusive, url)`

Sets the link URL for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Applies a link to the first 11 characters in the body.
const text = body.editAsText().setLinkUrl(0, 10, 'https://example.com');
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setLinkUrl(url)`

Sets the link url.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `url` | `String` | the link url |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setStrikethrough(strikethrough)`

Sets the strikethrough setting.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `strikethrough` | `Boolean` | the strikethrough setting |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setStrikethrough(startOffset, endOffsetInclusive, strikethrough)`

Sets the strikethrough setting for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 11 characters in the tab's body to strikethrough.
const text = body.editAsText().setStrikethrough(0, 10, true);
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setText(text)`

Sets the text contents.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Replaces the contents of the body with the text, 'New body text.'
const text = body.editAsText().setText('New body text.');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `text` | `String` | The new text contents. |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `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`.

```javascript
// Make the first character in the first paragraph of the active tab be
// superscript.
const documentTab =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const text = documentTab.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` | `https://developers.google.com/apps-script/reference/document/text-alignment` | The type of text alignment to apply. |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setTextAlignment(textAlignment)`

Sets the text alignment. The available types of alignment are `DocumentApp.TextAlignment.NORMAL`, `DocumentApp.TextAlignment.SUBSCRIPT`, and `DocumentApp.TextAlignment.SUPERSCRIPT`.

```javascript
// Make the entire first paragraph in the active tab be superscript.
const documentTab =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `textAlignment` | `https://developers.google.com/apps-script/reference/document/text-alignment` | the type of text alignment to apply |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setUnderline(underline)`

Sets the underline setting.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `underline` | `Boolean` | the underline setting |

#### Return


`https://developers.google.com/apps-script/reference/document/text#` --- the current element

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`

*** ** * ** ***

### `setUnderline(startOffset, endOffsetInclusive, underline)`

Sets the underline setting for the specified character range.

```javascript
// Opens the Docs file by its URL. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the URL with your own.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/DOCUMENT_ID/edit',
);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Sets the first 11 characters in the tab's body to underline.
const text = body.editAsText().setUnderline(0, 10, true);
```

#### 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


`https://developers.google.com/apps-script/reference/document/text#` --- The current element.

#### Authorization

Scripts that use this method require authorization with one or more of the following [scopes](https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes):

- `https://www.googleapis.com/auth/documents.currentonly`
- `https://www.googleapis.com/auth/documents`