DocumentApp

The document service creates and opens `https://developers.google.com/apps-script/reference/document/document` that can be edited.

```javascript
// Open a document by ID.
// TODO(developer): Replace the ID with your own.
let doc = DocumentApp.openById('DOCUMENT_ID');

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

### Properties

| Property | Type | Description |
|---|---|---|
| `Attribute` | `https://developers.google.com/apps-script/reference/document/attribute` | The `https://developers.google.com/apps-script/reference/document/attribute` enumeration. |
| `ElementType` | `https://developers.google.com/apps-script/reference/document/element-type` | The `https://developers.google.com/apps-script/reference/document/element-type` enumeration. |
| `FontFamily` | `https://developers.google.com/apps-script/reference/document/font-family` | The `https://developers.google.com/apps-script/reference/document/font-family` enumeration. |
| `GlyphType` | `https://developers.google.com/apps-script/reference/document/glyph-type` | The `https://developers.google.com/apps-script/reference/document/glyph-type` enumeration. |
| `HorizontalAlignment` | `https://developers.google.com/apps-script/reference/document/horizontal-alignment` | The `https://developers.google.com/apps-script/reference/document/horizontal-alignment` enumeration. |
| `ParagraphHeading` | `https://developers.google.com/apps-script/reference/document/paragraph-heading` | The `https://developers.google.com/apps-script/reference/document/paragraph-heading` enumeration. |
| `PositionedLayout` | `https://developers.google.com/apps-script/reference/document/positioned-layout` | The `https://developers.google.com/apps-script/reference/document/positioned-layout` enumeration. |
| `TextAlignment` | `https://developers.google.com/apps-script/reference/document/text-alignment` | The `https://developers.google.com/apps-script/reference/document/text-alignment` enumeration. |
| `VerticalAlignment` | `https://developers.google.com/apps-script/reference/document/vertical-alignment` | The `https://developers.google.com/apps-script/reference/document/vertical-alignment` enumeration. |

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/document/document-app#create(String)` | `https://developers.google.com/apps-script/reference/document/document` | Creates and returns a new document. |
| `https://developers.google.com/apps-script/reference/document/document-app#getActiveDocument()` | `https://developers.google.com/apps-script/reference/document/document` | Returns the document to which the script is [container-bound](https://developers.google.com/apps-script/scripts_containers). |
| `https://developers.google.com/apps-script/reference/document/document-app#getUi()` | `https://developers.google.com/apps-script/reference/base/ui.html` | Returns an instance of the document's user-interface environment that allows the script to add features like menus, dialogs, and sidebars. |
| `https://developers.google.com/apps-script/reference/document/document-app#openById(String)` | `https://developers.google.com/apps-script/reference/document/document` | Returns the document with the specified ID. |
| `https://developers.google.com/apps-script/reference/document/document-app#openByUrl(String)` | `https://developers.google.com/apps-script/reference/document/document` | Opens and returns the document with the specified URL. |

## Detailed documentation

### `create(name)`

Creates and returns a new document.

```javascript
// Create and open a new document.
const doc = DocumentApp.create('Document Name');
```

#### Parameters

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

#### Return


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

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

*** ** * ** ***

### `getActiveDocument()`

Returns the document to which the script is [container-bound](https://developers.google.com/apps-script/scripts_containers). To interact with document to which
the script is not container-bound, use `https://developers.google.com/apps-script/reference/document/document-app#openById(String)` or `https://developers.google.com/apps-script/reference/document/document-app#openByUrl(String)`
instead.

```javascript
// Get the document to which this script is bound.
const doc = DocumentApp.getActiveDocument();
```

#### Return


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

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

*** ** * ** ***

### `getUi()`

Returns an instance of the document's user-interface environment that allows the script to add
features like menus, dialogs, and sidebars. A script can only interact with the UI for the
current instance of an open document, and only if the script is [bound](https://developers.google.com/apps-script/scripts_containers) to the document. For more information, see the
guides to [menus](https://developers.google.com/apps-script/guides/menus) and [dialogs and sidebars](https://developers.google.com/apps-script/guides/dialogs).

```javascript
// Add a custom menu to the active document, including a separator and a
// sub-menu.
function onOpen(e) {
  DocumentApp.getUi()
      .createMenu('My Menu')
      .addItem('My menu item', 'myFunction')
      .addSeparator()
      .addSubMenu(
          DocumentApp.getUi()
              .createMenu('My sub-menu')
              .addItem('One sub-menu item', 'mySecondFunction')
              .addItem('Another sub-menu item', 'myThirdFunction'),
          )
      .addToUi();
}
```

#### Return


`https://developers.google.com/apps-script/reference/base/ui.html` --- an instance of this document's user-interface environment

*** ** * ** ***

### `openById(id)`

Returns the document with the specified ID. If the script is container-bound to the document,
use `https://developers.google.com/apps-script/reference/document/document-app#getActiveDocument()` instead.

```javascript
// Open a document by ID.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('DOCUMENT_ID');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `id` | `String` | The ID of the document to open. |

#### Return


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

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

*** ** * ** ***

### `openByUrl(url)`

Opens and returns the document with the specified URL. If the script is container-bound to the
document, use `https://developers.google.com/apps-script/reference/document/document-app#getActiveDocument()` instead.

```javascript
// Open a document by URL.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/1234567890abcdefghijklmnopqrstuvwxyz_a1b2c3/edit',
);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `url` | `String` | the URL of the document to open |

#### Return


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

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