Document

A document, containing one or more `https://developers.google.com/apps-script/reference/document/tab` objects, each of which contains rich text and
elements such as tables and lists.

Documents may be opened or created using `DocumentApp`.

```javascript
// Open a document by ID.
let doc = DocumentApp.openById('<my-id>');

// Create and open a document.
doc = DocumentApp.create('Document Title');
```

Methods on the `Document` class that directly access and modify text contents operate on
either the active tab (in scripts [bound](https://developers.google.com/apps-script/scripts_containers) to a
particular document) or the first tab (if an active one isn't available). Scripts relying on
these methods (for example, `https://developers.google.com/apps-script/reference/document/document#getBody()`) can be migrated to support tabs using
`https://developers.google.com/apps-script/reference/document/document#getTabs()` and then `https://developers.google.com/apps-script/reference/document/tab#asDocumentTab()`.

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/document/document#addBookmark(Position)` | `https://developers.google.com/apps-script/reference/document/bookmark` | Adds a `https://developers.google.com/apps-script/reference/document/bookmark` at the given `https://developers.google.com/apps-script/reference/document/position` to the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#addEditor(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Adds the given user to the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#addEditor(User)` | `https://developers.google.com/apps-script/reference/document/document#` | Adds the given user to the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#addEditors(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Adds the given array of users to the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#addFooter()` | `https://developers.google.com/apps-script/reference/document/footer-section` | Adds a footer section, if none exists, to the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#addHeader()` | `https://developers.google.com/apps-script/reference/document/header-section` | Adds a header section, if none exists, to the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#addNamedRange(String,Range)` | `https://developers.google.com/apps-script/reference/document/named-range` | Adds a `https://developers.google.com/apps-script/reference/document/named-range`, which is a `https://developers.google.com/apps-script/reference/document/range` that has a name and ID to use for later retrieval, in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#addViewer(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Adds the given user to the list of viewers for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#addViewer(User)` | `https://developers.google.com/apps-script/reference/document/document#` | Adds the given user to the list of viewers for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#addViewers(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Adds the given array of users to the list of viewers for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#getActiveTab()` | `https://developers.google.com/apps-script/reference/document/tab|null` | Gets the user's currently active `https://developers.google.com/apps-script/reference/document/tab` in the document. |
| `https://developers.google.com/apps-script/reference/document/document#getAs(String)` | `https://developers.google.com/apps-script/reference/base/blob.html` | Retrieves the current `Document` contents as a blob of the specified type. |
| `https://developers.google.com/apps-script/reference/document/document#getBlob()` | `https://developers.google.com/apps-script/reference/base/blob.html` | Retrieves the current `Document` contents as a blob. |
| `https://developers.google.com/apps-script/reference/document/document#getBody()` | `https://developers.google.com/apps-script/reference/document/body` | Retrieves the first tab's `https://developers.google.com/apps-script/reference/document/body` or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's `DocumentBodySection`. |
| `https://developers.google.com/apps-script/reference/document/document#getBookmark(String)` | `https://developers.google.com/apps-script/reference/document/bookmark|null` | Gets the `https://developers.google.com/apps-script/reference/document/bookmark` with the given ID in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getBookmarks()` | `https://developers.google.com/apps-script/reference/document/bookmark` | Gets all `https://developers.google.com/apps-script/reference/document/bookmark` objects in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getCursor()` | `https://developers.google.com/apps-script/reference/document/position|null` | Gets the user's cursor in the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getEditors()` | `https://developers.google.com/apps-script/reference/base/user.html` | Gets the list of editors for this `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#getFooter()` | `https://developers.google.com/apps-script/reference/document/footer-section|null` | Retrieves the first tab's footer section or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's footer section. |
| `https://developers.google.com/apps-script/reference/document/document#getFootnotes()` | `https://developers.google.com/apps-script/reference/document/footnote` | Retrieves all the `https://developers.google.com/apps-script/reference/document/footnote` elements in the first tab's body or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's body. |
| `https://developers.google.com/apps-script/reference/document/document#getHeader()` | `https://developers.google.com/apps-script/reference/document/header-section|null` | Retrieves the first tab's header section or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's header section. |
| `https://developers.google.com/apps-script/reference/document/document#getId()` | `String` | Retrieves the document's unique identifier. |
| `https://developers.google.com/apps-script/reference/document/document#getLanguage()` | `String|null` | Gets the document's language code. |
| `https://developers.google.com/apps-script/reference/document/document#getName()` | `String` | Retrieves the title of the document. |
| `https://developers.google.com/apps-script/reference/document/document#getNamedRangeById(String)` | `https://developers.google.com/apps-script/reference/document/named-range|null` | Gets the `https://developers.google.com/apps-script/reference/document/named-range` with the given ID in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getNamedRanges()` | `https://developers.google.com/apps-script/reference/document/named-range` | Gets all `https://developers.google.com/apps-script/reference/document/named-range` objects in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getNamedRanges(String)` | `https://developers.google.com/apps-script/reference/document/named-range` | Gets all `https://developers.google.com/apps-script/reference/document/named-range` objects with the given name in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getSelection()` | `https://developers.google.com/apps-script/reference/document/range|null` | Gets the user's selection in the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#getSupportedLanguageCodes()` | `String[]` | Gets all language codes that are supported in Google Docs files. |
| `https://developers.google.com/apps-script/reference/document/document#getTab(String)` | `https://developers.google.com/apps-script/reference/document/tab|null` | Gets the `https://developers.google.com/apps-script/reference/document/tab` with the specified ID. |
| `https://developers.google.com/apps-script/reference/document/document#getTabs()` | `https://developers.google.com/apps-script/reference/document/tab` | Gets all unnested `https://developers.google.com/apps-script/reference/document/tab`s that are part of the document. |
| `https://developers.google.com/apps-script/reference/document/document#getUrl()` | `String` | Retrieves the URL to access the current document. |
| `https://developers.google.com/apps-script/reference/document/document#getViewers()` | `https://developers.google.com/apps-script/reference/base/user.html` | Gets the list of viewers and commenters for this `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#newPosition(Element,Integer)` | `https://developers.google.com/apps-script/reference/document/position` | Creates a new `https://developers.google.com/apps-script/reference/document/position`, which is a reference to a location in the tab, relative to a specific element in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#newRange()` | `https://developers.google.com/apps-script/reference/document/range-builder` | Creates a builder used to construct `https://developers.google.com/apps-script/reference/document/range` objects from tab elements in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. |
| `https://developers.google.com/apps-script/reference/document/document#removeEditor(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Removes the given user from the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#removeEditor(User)` | `https://developers.google.com/apps-script/reference/document/document#` | Removes the given user from the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#removeViewer(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Removes the given user from the list of viewers and commenters for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#removeViewer(User)` | `https://developers.google.com/apps-script/reference/document/document#` | Removes the given user from the list of viewers and commenters for the `https://developers.google.com/apps-script/reference/document/document#`. |
| `https://developers.google.com/apps-script/reference/document/document#saveAndClose()` | `void` | Saves the current `Document`. |
| `https://developers.google.com/apps-script/reference/document/document#setActiveTab(String)` | `void` | Sets the user's selected `https://developers.google.com/apps-script/reference/document/tab` in the current document to the tab with the specified ID. |
| `https://developers.google.com/apps-script/reference/document/document#setCursor(Position)` | `https://developers.google.com/apps-script/reference/document/document#` | Sets the user's cursor, given a `https://developers.google.com/apps-script/reference/document/position`. |
| `https://developers.google.com/apps-script/reference/document/document#setLanguage(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Sets the document's language code. |
| `https://developers.google.com/apps-script/reference/document/document#setName(String)` | `https://developers.google.com/apps-script/reference/document/document#` | Sets the document title. |
| `https://developers.google.com/apps-script/reference/document/document#setSelection(Range)` | `https://developers.google.com/apps-script/reference/document/document#` | Sets the user's selection in the active tab, given a `https://developers.google.com/apps-script/reference/document/range`. |

## Detailed documentation

### `addBookmark(position)`

Adds a `https://developers.google.com/apps-script/reference/document/bookmark` at the given `https://developers.google.com/apps-script/reference/document/position` to the first tab or, for scripts that are
[bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To add a
bookmark to any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#addBookmark(Position)` method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the active or first tab's body and adds a paragraph.
const paragraph = doc.getBody().appendParagraph('My new paragraph.');

// Creates a position at the first character of the paragraph text.
const position = doc.newPosition(paragraph.getChild(0), 0);

// Adds a bookmark at the first character of the paragraph text.
const bookmark = doc.addBookmark(position);

// Logs the bookmark ID to the console.
console.log(bookmark.getId());
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `position` | `https://developers.google.com/apps-script/reference/document/position` | The position of the new bookmark. |

#### Return


`https://developers.google.com/apps-script/reference/document/bookmark` --- The new bookmark.

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

*** ** * ** ***

### `addEditor(emailAddress)`

Adds the given user to the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. If the user was already
on the list of viewers, this method promotes the user out of the list of viewers.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `emailAddress` | `String` | The email address of the user to add. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `addEditor(user)`

Adds the given user to the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. If the user was already
on the list of viewers, this method promotes the user out of the list of viewers.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `user` | `https://developers.google.com/apps-script/reference/base/user.html` | A representation of the user to add. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `addEditors(emailAddresses)`

Adds the given array of users to the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. If any of the
users were already on the list of viewers, this method promotes them out of the list of
viewers.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `emailAddresses` | `String[]` | An array of email addresses of the users to add. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `addFooter()`

Adds a footer section, if none exists, to the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To add a footer
section to any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#addFooter()` method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Adds a footer to the document's active or first tab.
const footer = doc.addFooter();

// Sets the footer text to 'This is a footer.'
footer.setText('This is a footer');
```

#### Return


`https://developers.google.com/apps-script/reference/document/footer-section` --- The tab footer.

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

*** ** * ** ***

### `addHeader()`

Adds a header section, if none exists, to the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To add a header
section to any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#addHeader()` method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Adds a header to the document's active or first tab.
const header = doc.addHeader();

// Sets the header text to 'This is a header.'
header.setText('This is a header');
```

#### Return


`https://developers.google.com/apps-script/reference/document/header-section` --- The tab header.

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

*** ** * ** ***

### `addNamedRange(name, range)`

Adds a `https://developers.google.com/apps-script/reference/document/named-range`, which is a `https://developers.google.com/apps-script/reference/document/range` that has a name and ID to use for
later retrieval, in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To add a `NamedRange` in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#addNamedRange(String,Range)` method. Names aren't
necessarily unique; several different ranges in the same document can share the same name, much
like a class in HTML. By contrast, IDs are unique within the document, like an ID in HTML.
After you add a `NamedRange` to a document, you can't modify it, you can only remove it.

Any script that accesses the document can access a `NamedRange`. To avoid unintended
conflicts between scripts, consider prefixing range names with a unique string.

```javascript
// Creates a named range that includes every table in the active tab.
const doc = DocumentApp.getActiveDocument();
const rangeBuilder = doc.newRange();
const tables = doc.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
// Adds the named range to the document's active tab.
doc.addNamedRange('Document tables', rangeBuilder.build());
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `name` | `String` | The name for the range, which doesn't need to be unique; range names must be between 1-256 characters. |
| `range` | `https://developers.google.com/apps-script/reference/document/range` | The range of elements to associate with the name; the range can be [the active selection](https://developers.google.com/apps-script/reference/document/document#getSelection()), a [search result](https://developers.google.com/apps-script/reference/document/body#findText(String)), or manually constructed with `https://developers.google.com/apps-script/reference/document/document#newRange()`. |

#### Return


`https://developers.google.com/apps-script/reference/document/named-range` --- The `NamedRange`.

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

*** ** * ** ***

### `addViewer(emailAddress)`

Adds the given user to the list of viewers for the `https://developers.google.com/apps-script/reference/document/document#`. If the user was already
on the list of editors, this method has no effect.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `emailAddress` | `String` | The email address of the user to add. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `addViewer(user)`

Adds the given user to the list of viewers for the `https://developers.google.com/apps-script/reference/document/document#`. If the user was already
on the list of editors, this method has no effect.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `user` | `https://developers.google.com/apps-script/reference/base/user.html` | A representation of the user to add. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `addViewers(emailAddresses)`

Adds the given array of users to the list of viewers for the `https://developers.google.com/apps-script/reference/document/document#`. If any of the
users were already on the list of editors, this method has no effect for them.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `emailAddresses` | `String[]` | An array of email addresses of the users to add. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `getActiveTab()`

Gets the user's currently active `https://developers.google.com/apps-script/reference/document/tab` in the document. A script can only access the
active tab of the user who is running the script, and only if the script is [bound](https://developers.google.com/apps-script/scripts_containers) to the document.

```javascript
// Display a dialog box that shows the title of the tab that the
// user is currently viewing.
const tab = DocumentApp.getActiveDocument().getActiveTab();
DocumentApp.getUi().alert(`ID of selected tab: ${tab.getTitle()}`);
```

#### Return


`https://developers.google.com/apps-script/reference/document/tab|null` --- The user's currently active `https://developers.google.com/apps-script/reference/document/tab`, or `null` if the script is not bound to
the document.

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

*** ** * ** ***

### `getAs(contentType)`

Retrieves the current `Document` contents as a blob of the specified type.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the document as a PDF.
const pdf = doc.getAs('application/pdf');

// Logs the name of the PDF to the console.
console.log(pdf.getName());
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `contentType` | `String` | The MIME type to convert to; `'application/pdf'` and `'text/markdown'` are supported. |

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- The current document as a blob.

*** ** * ** ***

### `getBlob()`

Retrieves the current `Document` contents as a blob.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Retrieves the current document's contents as a blob and logs it to the
// console.
console.log(doc.getBlob().getContentType());
```

#### Return


`https://developers.google.com/apps-script/reference/base/blob.html` --- The current document as a blob.

*** ** * ** ***

### `getBody()`

Retrieves the first tab's `https://developers.google.com/apps-script/reference/document/body` or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's `DocumentBodySection`. To get the `DocumentBodySection` of any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getBody()` method.

Tabs may contain different types of sections (for example, `https://developers.google.com/apps-script/reference/document/header-section`, `https://developers.google.com/apps-script/reference/document/footer-section`). The active section for a tab is the `https://developers.google.com/apps-script/reference/document/body`.

Element methods in `Document` delegate to the active `https://developers.google.com/apps-script/reference/document/body`.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the active or first tab's body.
const body = doc.getBody();

// Gets the body text and logs it to the console.
console.log(body.getText());
```

#### Return


`https://developers.google.com/apps-script/reference/document/body` --- The tab body section.

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

*** ** * ** ***

### `getBookmark(id)`

Gets the `https://developers.google.com/apps-script/reference/document/bookmark` with the given ID in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To get a
bookmark in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getBookmark(String)` method. This method returns `null` if no such `Bookmark` exists within the tab.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the bookmark by its ID in the document's active or first tab.
const bookmark = doc.getBookmark('id.xyz654321');

// If the bookmark exists, logs the character offset of its position to the
// console. otherwise, logs 'No bookmark exists with the given ID.' to the
// console.
if (bookmark) {
  console.log(bookmark.getPosition().getOffset());
} else {
  console.log('No bookmark exists with the given ID.');
}
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `id` | `String` | The ID for the `Bookmark`. |

#### Return


`https://developers.google.com/apps-script/reference/document/bookmark|null` --- The `Bookmark` with the given ID, or `null` if no such `Bookmark`
exists within 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`

*** ** * ** ***

### `getBookmarks()`

Gets all `https://developers.google.com/apps-script/reference/document/bookmark` objects in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To get all
bookmarks in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getBookmarks()` method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
const doc = DocumentApp.openById('123abc');

// Gets all of the bookmarks in the document's active or first tab.
const bookmarks = doc.getBookmarks();

// Logs the number of bookmarks in the tab to the console.
console.log(bookmarks.length);
```

#### Return


`https://developers.google.com/apps-script/reference/document/bookmark` --- An array of the `Bookmark` objects in 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`

*** ** * ** ***

### `getCursor()`

Gets the user's cursor in the active tab. A script can only access the cursor of the user who
is running the script, and only if the script is [bound](https://developers.google.com/apps-script/scripts_containers) to the document.

```javascript
// Insert some text at the cursor position and make it bold.
const cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
  // Attempt to insert text at the cursor position. If the insertion returns
  // null, the cursor's containing element doesn't allow insertions, so show the
  // user an error message.
  const element = cursor.insertText('ಠ‿ಠ');
  if (element) {
    element.setBold(true);
  } else {
    DocumentApp.getUi().alert('Cannot insert text here.');
  }
} else {
  DocumentApp.getUi().alert('Cannot find a cursor.');
}
```

#### Return


`https://developers.google.com/apps-script/reference/document/position|null` --- A representation of the user's cursor, or `null` if the user does not have a
cursor placed in the tab or if the script is not bound to the document.

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

*** ** * ** ***

### `getEditors()`

Gets the list of editors for this `https://developers.google.com/apps-script/reference/document/document#`.

#### Return


`https://developers.google.com/apps-script/reference/base/user.html` --- An array of users with edit permission.

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

*** ** * ** ***

### `getFooter()`

Retrieves the first tab's footer section or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's footer
section. To get the footer section of any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getFooter()` method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the text of the active or first tab's footer and logs it to the console.
console.log(doc.getFooter().getText());
```

#### Return


`https://developers.google.com/apps-script/reference/document/footer-section|null` --- The tab's footer.

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

*** ** * ** ***

### `getFootnotes()`

Retrieves all the `https://developers.google.com/apps-script/reference/document/footnote` elements in the first tab's body or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's body. To get
all the footnotes in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getFootnotes()` method.

Calls to `getFootnotes` cause an iteration over the tab's elements. For large tabs,
avoid unnecessary calls to this method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the first footnote in the active or first tab's body.
const footnote = doc.getFootnotes()[0];

// Logs footnote contents to the console.
console.log(footnote.getFootnoteContents().getText());
```

#### Return


`https://developers.google.com/apps-script/reference/document/footnote` --- The tab's footnotes.

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

*** ** * ** ***

### `getHeader()`

Retrieves the first tab's header section or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab's header
section. To get the header section of any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getHeader()` method.

```javascript
// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the text of the active or first tab's header and logs it to the console.
console.log(doc.getHeader().getText());
```

#### Return


`https://developers.google.com/apps-script/reference/document/header-section|null` --- The tab's header.

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

*** ** * ** ***

### `getId()`

Retrieves the document's unique identifier. The document ID is used with `DocumentApp.openById()` to open a specific document instance.

#### Return


`String` --- The document's ID.

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

*** ** * ** ***

### `getLanguage()`

Gets the document's language code. This is the language shown in the document editor's **File
\> Language**, which may not be the actual language that the document contains.

#### Return


`String|null` --- The document language, or `null` if not defined.

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

*** ** * ** ***

### `getName()`

Retrieves the title of the document.

#### Return


`String` --- The document title.

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

*** ** * ** ***

### `getNamedRangeById(id)`

Gets the `https://developers.google.com/apps-script/reference/document/named-range` with the given ID in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To get the
`NamedRange` with the given ID in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getNamedRangeById(String)`
method. This method returns `null` if no such `NamedRange` exists in the tab. Names
are not necessarily unique, even across tabs; several different ranges in the same tab may
share the same name, much like a class in HTML. By contrast, IDs are unique within the tab,
like an ID in HTML.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `id` | `String` | The range's ID, which is unique within the tab. |

#### Return


`https://developers.google.com/apps-script/reference/document/named-range|null` --- The `NamedRange` with the given ID, or `null` if no such range exists in
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`

*** ** * ** ***

### `getNamedRanges()`

Gets all `https://developers.google.com/apps-script/reference/document/named-range` objects in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To get all
`NamedRange` objects in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getNamedRanges()` method.

A `NamedRange` can be accessed by any script that accesses the tab. To avoid
unintended conflicts between scripts, consider prefixing range names with a unique string.

#### Return


`https://developers.google.com/apps-script/reference/document/named-range` --- An array of the `NamedRange` objects in the tab, possibly including multiple
ranges with the same name.

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

*** ** * ** ***

### `getNamedRanges(name)`

Gets all `https://developers.google.com/apps-script/reference/document/named-range` objects with the given name in the first tab or, for scripts that
are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To get
all `NamedRange` objects in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#getNamedRanges(String)` method. Names are not necessarily unique, even across tabs; several different ranges in
the same tab may share the same name, much like a class in HTML. By contrast, IDs are unique
within the tab, like an ID in HTML.

A `NamedRange` can be accessed by any script that accesses the document. To avoid
unintended conflicts between scripts, consider prefixing range names with a unique string.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `name` | `String` | The range's name, which is not necessarily unique. |

#### Return


`https://developers.google.com/apps-script/reference/document/named-range` --- An array of the `NamedRange` objects in the tab with the given name.

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

*** ** * ** ***

### `getSelection()`

Gets the user's selection in the active tab. A script can only access the selection of the user
who is running the script, and only if the script is [bound](https://developers.google.com/apps-script/scripts_containers) to the document.

```javascript
// Display a dialog box that tells the user how many elements are included in
// the selection.
const selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
  const elements = selection.getRangeElements();
  DocumentApp.getUi().alert(`Number of selected elements: ${elements.length}`);
} else {
  DocumentApp.getUi().alert('Nothing is selected.');
}
```

#### Return


`https://developers.google.com/apps-script/reference/document/range|null` --- A representation of the user's selection, or `null` if the user does not have
anything selected in the tab, if only the end of a paragraph is selected, if only the end
of a paragraph and a new line are selected, or if the script is not bound to the document.

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

*** ** * ** ***

### `getSupportedLanguageCodes()`

Gets all language codes that are supported in Google Docs files.

#### Return


`String[]` --- An array of language codes.

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

*** ** * ** ***

### `getTab(tabId)`

Gets the `https://developers.google.com/apps-script/reference/document/tab` with the specified ID. This method returns `null` if no such `Tab` exists. Can access tabs at any nesting level.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `tabId` | `String` | The ID of the tab to get. |

#### Return


`https://developers.google.com/apps-script/reference/document/tab|null` --- The `Tab` with the specified ID, or `null` if no such `Tab` exists.

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

*** ** * ** ***

### `getTabs()`

Gets all unnested `https://developers.google.com/apps-script/reference/document/tab`s that are part of the document.

Tabs can contain child tabs, a tab nested within another tab. Child tabs are accessible
using `https://developers.google.com/apps-script/reference/document/tab#getChildTabs()`.

#### Return


`https://developers.google.com/apps-script/reference/document/tab` --- The list of all `Tab`s that are part of the document.

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

*** ** * ** ***

### `getUrl()`

Retrieves the URL to access the current document.

```javascript
const doc = DocumentApp.getActiveDocument();

// Send out the link to open the document.
MailApp.sendEmail('<email-address>', doc.getName(), doc.getUrl());
```

#### Return


`String` --- The URL to access the current document.

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

*** ** * ** ***

### `getViewers()`

Gets the list of viewers and commenters for this `https://developers.google.com/apps-script/reference/document/document#`.

#### Return


`https://developers.google.com/apps-script/reference/base/user.html` --- An array of users with view or comment permission.

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

*** ** * ** ***

### `newPosition(element, offset)`

Creates a new `https://developers.google.com/apps-script/reference/document/position`, which is a reference to a location in the tab, relative to a
specific element in the first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a document, the active tab. To create a
`Position` relative to a location in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#newPosition(Element,Integer)`
method. The user's cursor is represented as a `Position`, among other uses.

```javascript
// Append a paragraph to the active tab, then place the user's cursor after the
// first word of the new paragraph.
const doc = DocumentApp.getActiveDocument();
const paragraph = doc.getBody().appendParagraph('My new paragraph.');
const position = doc.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `element` | `https://developers.google.com/apps-script/reference/document/element` | The element that should contain the new `Position`; this must be either a `https://developers.google.com/apps-script/reference/document/text` element or a container element like `https://developers.google.com/apps-script/reference/document/paragraph`. |
| `offset` | `Integer` | For `https://developers.google.com/apps-script/reference/document/text` elements, the number of characters before the `Position`; for other elements, the number of child elements before the `Position` within the same container element. |

#### Return


`https://developers.google.com/apps-script/reference/document/position` --- The new `Position`.

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

*** ** * ** ***

### `newRange()`

Creates a builder used to construct `https://developers.google.com/apps-script/reference/document/range` objects from tab elements in the
first tab or, for scripts that are [bound](https://developers.google.com/apps-script/scripts_containers) to a
document, the active tab. To create a builder used to construct `DocumentRange` objects
from tab elements in any tab, use the `https://developers.google.com/apps-script/reference/document/document-tab#newRange()` method.

```javascript
// Change the user's selection to a range that includes every table in the
// active tab.
const doc = DocumentApp.getActiveDocument();
const rangeBuilder = doc.newRange();
const tables = doc.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());
```

#### Return


`https://developers.google.com/apps-script/reference/document/range-builder` --- The new builder.

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

*** ** * ** ***

### `removeEditor(emailAddress)`

Removes the given user from the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. This method doesn't
block users from accessing the `https://developers.google.com/apps-script/reference/document/document#` if they belong to a class of users who have
general access---for example, if the `https://developers.google.com/apps-script/reference/document/document#` is shared with the user's entire
domain, or if the `https://developers.google.com/apps-script/reference/document/document#` is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of viewers.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `emailAddress` | `String` | The email address of the user to remove. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `removeEditor(user)`

Removes the given user from the list of editors for the `https://developers.google.com/apps-script/reference/document/document#`. This method doesn't
block users from accessing the `https://developers.google.com/apps-script/reference/document/document#` if they belong to a class of users who have
general access---for example, if the `https://developers.google.com/apps-script/reference/document/document#` is shared with the user's entire
domain, or if the `https://developers.google.com/apps-script/reference/document/document#` is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of viewers.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `user` | `https://developers.google.com/apps-script/reference/base/user.html` | A representation of the user to remove. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#`, for chaining.

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

*** ** * ** ***

### `removeViewer(emailAddress)`

Removes the given user from the list of viewers and commenters for the `https://developers.google.com/apps-script/reference/document/document#`. This
method has no effect if the user is an editor, not a viewer or commenter. This method also
doesn't block users from accessing the `https://developers.google.com/apps-script/reference/document/document#` if they belong to a class of users who
have general access---for example, if the `https://developers.google.com/apps-script/reference/document/document#` is shared with the user's
entire domain, or if the `https://developers.google.com/apps-script/reference/document/document#` is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of editors.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `emailAddress` | `String` | The email address of the user to remove. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#` for chaining.

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

*** ** * ** ***

### `removeViewer(user)`

Removes the given user from the list of viewers and commenters for the `https://developers.google.com/apps-script/reference/document/document#`. This
method has no effect if the user is an editor, not a viewer. This method also doesn't block
users from accessing the `https://developers.google.com/apps-script/reference/document/document#` if they belong to a class of users who have general
access---for example, if the `https://developers.google.com/apps-script/reference/document/document#` is shared with the user's entire domain, or
if the `https://developers.google.com/apps-script/reference/document/document#` is in a shared drive that the user can access.

For Drive files, this also removes the user from the list of editors.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `user` | `https://developers.google.com/apps-script/reference/base/user.html` | A representation of the user to remove. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `https://developers.google.com/apps-script/reference/document/document#` for chaining.

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

*** ** * ** ***

### `saveAndClose()`

Saves the current `Document`. Causes pending updates to be flushed and applied.

The `saveAndClose()` method is automatically invoked at the end of script execution
for each open editable `Document`.

A closed `Document` can't be edited. Use `DocumentApp.openById()` to reopen a
given document for editing.

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

*** ** * ** ***

### `setActiveTab(tabId)`

Sets the user's selected `https://developers.google.com/apps-script/reference/document/tab` in the current document to the tab with the specified ID.

```javascript
const doc = DocumentApp.getActiveDocument();

// Sets the user's selected tab by its ID.
// TODO(developer): Replace the ID with your own.
const tab = doc.setActiveTab('123abc');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `tabId` | `String` | The ID of the tab to set as active. |

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

*** ** * ** ***

### `setCursor(position)`

Sets the user's cursor, given a `https://developers.google.com/apps-script/reference/document/position`. A script can only access the cursor of the
user who is running the script, and only if the script is [bound](https://developers.google.com/apps-script/scripts_containers) to the document.

Providing a `https://developers.google.com/apps-script/reference/document/position` from an inactive `https://developers.google.com/apps-script/reference/document/tab` switches the user's active tab.

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

// Append a paragraph, then place the user's cursor after the first word of the
// new paragraph.
const paragraph = documentTab.getBody().appendParagraph('My new paragraph.');
const position = documentTab.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `position` | `https://developers.google.com/apps-script/reference/document/position` | The new cursor location. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `Document`, for chaining.

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

*** ** * ** ***

### `setLanguage(languageCode)`

Sets the document's language code. This is the language shown in the document editor's **File
\> Language** , which may not be the actual language that the document contains. Use `https://developers.google.com/apps-script/reference/document/document#getSupportedLanguageCodes()` to get all the valid language codes.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `languageCode` | `String` | The language code. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `Document`, for chaining.

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

*** ** * ** ***

### `setName(name)`

Sets the document title.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `name` | `String` | The new document title. |

#### Return


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

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

*** ** * ** ***

### `setSelection(range)`

Sets the user's selection in the active tab, given a `https://developers.google.com/apps-script/reference/document/range`. A script can only
access the selection of the user who is running the script, and only if the script is [bound](https://developers.google.com/apps-script/scripts_containers) to the document.

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

// Change the user's selection to a range that includes every table in the
// document.
const rangeBuilder = documentTab.newRange();
const tables = documentTab.getBody().getTables();
for (let i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `range` | `https://developers.google.com/apps-script/reference/document/range` | The new range of elements to select. |

#### Return


`https://developers.google.com/apps-script/reference/document/document#` --- This `Document`, for chaining.

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