Class DocumentApp

DocumentApp

The document service creates and opens Documents that can be edited.

 // 
  
 Open 
  
 a 
  
 document 
  
 by 
  
 ID 
 . 
 // 
  
 TODO 
 ( 
 developer 
 ): 
  
 Replace 
  
 the 
  
 ID 
  
 with 
  
 your 
  
 own 
 . 
 var 
  
 doc 
  
 = 
  
 DocumentApp 
 . 
 openById 
 ( 
 'DOCUMENT_ID' 
 ); 
 // 
  
 Create 
  
 and 
  
 open 
  
 a 
  
 document 
 . 
 doc 
  
 = 
  
 DocumentApp 
 . 
 create 
 ( 
 'Document Name' 
 ); 

Properties

Property Type Description
Attribute
Attribute The Attribute enumeration.
ElementType
ElementType The ElementType enumeration.
FontFamily
FontFamily The FontFamily enumeration.
GlyphType
GlyphType The GlyphType enumeration.
HorizontalAlignment
HorizontalAlignment The HorizontalAlignment enumeration.
ParagraphHeading
ParagraphHeading The ParagraphHeading enumeration.
PositionedLayout
PositionedLayout The PositionedLayout enumeration.
TextAlignment
TextAlignment The TextAlignment enumeration.
VerticalAlignment
VerticalAlignment The VerticalAlignment enumeration.

Methods

Method Return type Brief description
Document Creates and returns a new document.
Document Returns the document to which the script is container-bound .
Ui Returns an instance of the document's user-interface environment that allows the script to add features like menus, dialogs, and sidebars.
Document Returns the document with the specified ID.
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 
 . 
 var 
  
 doc 
  
 = 
  
 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 following scopes :

  • https://www.googleapis.com/auth/documents

getActiveDocument()

Returns the document to which the script is container-bound . To interact with document to which the script is not container-bound, use openById(id) or openByUrl(url) instead.

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

Return

Document — the document instance

Authorization

Scripts that use this method require authorization with one or more of the following 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 to the document. For more information, see the guides to menus and dialogs and sidebars .

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

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, use getActiveDocument() instead.

 // 
  
 Open 
  
 a 
  
 document 
  
 by 
  
 ID 
 . 
 // 
  
 TODO 
 ( 
 developer 
 ): 
  
 Replace 
  
 the 
  
 ID 
  
 with 
  
 your 
  
 own 
 . 
 var 
  
 doc 
  
 = 
  
 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 following 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 getActiveDocument() instead.

 // 
  
 Open 
  
 a 
  
 document 
  
 by 
  
 URL 
 . 
 var 
  
 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

Document — the document instance

Authorization

Scripts that use this method require authorization with one or more of the following scopes :

  • https://www.googleapis.com/auth/documents