Class DocumentApp Stay organized with collections Save and categorize content based on your preferences.
Page Summary
The Document service allows for creating and opening editable Google Documents using Apps Script.
You can create a new document with a specified name or open an existing document by its ID or URL.
The service provides access to various properties representing document formatting and structure enumerations.
Methods are available to get the active document to which a script is bound or to interact with the document's user interface for custom features.
The document service creates and opensDocuments that can be edited.
// Open a document by ID.// TODO(developer): Replace the ID with your own.letdoc=DocumentApp.openById('DOCUMENT_ID');// Create and open a document.doc=DocumentApp.create('Document Name');
Properties
| Property | Type | Description |
|---|---|---|
Attribute | Attribute | TheAttribute enumeration. |
Element | Element | TheElement enumeration. |
Font | | The enumeration. |
Glyph | Glyph | TheGlyph enumeration. |
Horizontal | Horizontal | TheHorizontal enumeration. |
Paragraph | Paragraph | TheParagraph enumeration. |
Positioned | Positioned | ThePositioned enumeration. |
Text | Text | TheText enumeration. |
Vertical | Vertical | TheVertical enumeration. |
Methods
| Method | Return type | Brief description |
|---|---|---|
create(name) | Document | Creates and returns a new document. |
get | Document | Returns the document to which the script iscontainer-bound. |
get | Ui | Returns an instance of the document's user-interface environment that allows the script to addfeatures like menus, dialogs, and sidebars. |
open | Document | Returns the document with the specified ID. |
open | Document | Opens and returns the document with the specified URL. |
Detailed documentation
create(name)
Creates and returns a new document.
// Create and open a new document.constdoc=DocumentApp.create('Document Name');
Parameters
| Name | Type | Description |
|---|---|---|
name | String | The new document's name. |
Return
Document — The new document instance.
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/documents
getActiveDocument()
Returns the document to which the script iscontainer-bound. To interact with document to whichthe script is not container-bound, useopen oropeninstead.
// Get the document to which this script is bound.constdoc=DocumentApp.getActiveDocument();
Return
Document — the document instance
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/documents.currentonlyhttps://www.googleapis.com/auth/documents
getUi()
Returns an instance of the document's user-interface environment that allows the script to addfeatures like menus, dialogs, and sidebars. A script can only interact with the UI for thecurrent instance of an open document, and only if the script isbound to the document. For more information, see theguides tomenus anddialogs and sidebars.
// Add a custom menu to the active document, including a separator and a// sub-menu.functiononOpen(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
Ui — 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,useget instead.
// Open a document by ID.// TODO(developer): Replace the ID with your own.constdoc=DocumentApp.openById('DOCUMENT_ID');
Parameters
| Name | Type | Description |
|---|---|---|
id | String | The ID of the document to open. |
Return
Document — The document instance.
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/documents
openByUrl(url)
Opens and returns the document with the specified URL. If the script is container-bound to thedocument, useget instead.
// Open a document by URL.constdoc=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
Document — the document instance
Authorization
Scripts that use this method require authorization with one or more of the followingscopes:
https://www.googleapis.com/auth/documents
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.