Class Document

Document

A document, containing rich text and elements such as tables and lists.

Documents may be opened or created using DocumentApp.

// Open a document by ID.
var doc = DocumentApp.openById("<my-id>");

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

Methods

MethodReturn typeBrief description
addBookmark(position)BookmarkAdds a Bookmark at the given Position.
addEditor(emailAddress)DocumentAdds the given user to the list of editors for the Document.
addEditor(user)DocumentAdds the given user to the list of editors for the Document.
addEditors(emailAddresses)DocumentAdds the given array of users to the list of editors for the Document.
addFooter()FooterSectionAdds a document footer section, if none exists.
addHeader()HeaderSectionAdds a document header section, if none exists.
addNamedRange(name, range)NamedRangeAdds a NamedRange, which is a Range that has a name and ID to use for later retrieval.
addViewer(emailAddress)DocumentAdds the given user to the list of viewers for the Document.
addViewer(user)DocumentAdds the given user to the list of viewers for the Document.
addViewers(emailAddresses)DocumentAdds the given array of users to the list of viewers for the Document.
getAs(contentType)BlobRetrieves the current Document contents as a blob of the specified type.
getBlob()BlobRetrieves the current Document contents as a blob.
getBody()BodyRetrieves the active document's Body.
getBookmark(id)BookmarkGets the Bookmark with the given ID.
getBookmarks()Bookmark[]Gets all Bookmark objects in the document.
getCursor()PositionGets the user's cursor in the active document.
getEditors()User[]Gets the list of editors for this Document.
getFooter()FooterSectionRetrieves the document's footer section, if one exists.
getFootnotes()Footnote[]Retrieves all the Footnote elements in the document body.
getHeader()HeaderSectionRetrieves the document's header section, if one exists.
getId()StringRetrieves the document's unique identifier.
getLanguage()StringGets the document's language code.
getName()StringRetrieves the title of the document.
getNamedRangeById(id)NamedRangeGets the NamedRange with the given ID.
getNamedRanges()NamedRange[]Gets all NamedRange objects in the document.
getNamedRanges(name)NamedRange[]Gets all NamedRange objects in the document with the given name.
getSelection()RangeGets the user's selection in the active document.
getSupportedLanguageCodes()String[]Gets all language codes that are supported in Google Docs files.
getUrl()StringRetrieves the URL to access the current document.
getViewers()User[]Gets the list of viewers and commenters for this Document.
newPosition(element, offset)PositionCreates a new Position, which is a reference to a location in the document, relative to a specific element.
newRange()RangeBuilderCreates a builder used to construct Range objects from document elements.
removeEditor(emailAddress)DocumentRemoves the given user from the list of editors for the Document.
removeEditor(user)DocumentRemoves the given user from the list of editors for the Document.
removeViewer(emailAddress)DocumentRemoves the given user from the list of viewers and commenters for the Document.
removeViewer(user)DocumentRemoves the given user from the list of viewers and commenters for the Document.
saveAndClose()voidSaves the current Document.
setCursor(position)DocumentSets the user's cursor in the active document, given a Position.
setLanguage(languageCode)DocumentSets the document's language code.
setName(name)DocumentSets the document title.
setSelection(range)DocumentSets the user's selection in the active document, given a Range.

Detailed documentation

addBookmark(position)

Adds a Bookmark at the given Position.

// 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('abc123456');

// Gets the document 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

NameTypeDescription
positionPositionThe position of the new bookmark.

Return

Bookmark — The new bookmark.

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

addEditor(emailAddress)

Adds the given user to the list of editors for the Document. If the user was already on the list of viewers, this method promotes the user out of the list of viewers.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Document — This Document, for chaining.

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

addEditor(user)

Adds the given user to the list of editors for the Document. If the user was already on the list of viewers, this method promotes the user out of the list of viewers.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Document — This Document, for chaining.

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

addEditors(emailAddresses)

Adds the given array of users to the list of editors for the Document. If any of the users were already on the list of viewers, this method promotes them out of the list of viewers.

Parameters

NameTypeDescription
emailAddressesString[]An array of email addresses of the users to add.

Return

Document — This Document, for chaining.

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

addFooter()

Adds a document footer section, if none exists.

// 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('abc123456');

// Adds a footer to the document.
const footer = doc.addFooter();

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

Return

FooterSection — The document footer.

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

addHeader()

Adds a document header section, if none exists.

// 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('abc123456');

// Adds a header to the document.
const header = doc.addHeader();

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

Return

HeaderSection — The document header.

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

addNamedRange(name, range)

Adds a NamedRange, which is a Range that has a name and ID to use for later retrieval. 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.

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

Parameters

NameTypeDescription
nameStringThe name for the range, which doesn't need to be unique; range names must be between 1-256 characters.
rangeRangeThe range of elements to associate with the name; the range can be the active selection, a search result, or manually constructed with newRange().

Return

NamedRange — The NamedRange.

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

addViewer(emailAddress)

Adds the given user to the list of viewers for the Document. If the user was already on the list of editors, this method has no effect.

Parameters

NameTypeDescription
emailAddressStringThe email address of the user to add.

Return

Document — This Document, for chaining.

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

addViewer(user)

Adds the given user to the list of viewers for the Document. If the user was already on the list of editors, this method has no effect.

Parameters

NameTypeDescription
userUserA representation of the user to add.

Return

Document — This Document, for chaining.

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

addViewers(emailAddresses)

Adds the given array of users to the list of viewers for the Document. If any of the users were already on the list of editors, this method has no effect for them.

Parameters

NameTypeDescription
emailAddressesString[]An array of email addresses of the users to add.

Return

Document — This Document, for chaining.

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

getAs(contentType)

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

// 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('abc123456');

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

NameTypeDescription
contentTypeStringThe MIME type to convert to; currently only 'application/pdf' is supported.

Return

Blob — The current document as a blob.


getBlob()

Retrieves the current Document contents as a blob.

// 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('abc123456');

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

Return

Blob — The current document as a blob.


getBody()

Retrieves the active document's Body.

Documents may contain different types of sections (e.g. HeaderSection, FooterSection). The active section for a document is the Body.

Element methods in Document delegate to the active Body.

// 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('abc123456');

// Gets the document body.
const body = doc.getBody();

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

Return

Body — The active document body section.

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

getBookmark(id)

Gets the Bookmark with the given ID. This method returns null if no such Bookmark exists.

// 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('abc123456');

// Gets the bookmark by its ID.
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

NameTypeDescription
idStringThe ID for the Bookmark.

Return

Bookmark — The Bookmark with the given ID, or null if no such Bookmark exists.

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

getBookmarks()

Gets all Bookmark objects in the document.

// 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('abc123456');

// Gets all of the bookmarks in the document.
const bookmarks = doc.getBookmarks();

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

Return

Bookmark[] — An array of the Bookmark objects in the document.

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

getCursor()

Gets the user's cursor in the active document. A script can only access the cursor of the user who is running the script, and only if the script is bound to the document.

// Insert some text at the cursor position and make it bold.
var 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.
  var 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

Position — A representation of the user's cursor, or null if the user does not have a cursor placed in the document 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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getEditors()

Gets the list of editors for this Document.

Return

User[] — An array of users with edit permission.

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

getFooter()

Retrieves the document's footer section, if one exists.

// 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('abc123456');

// Gets the text of the document's footer and logs it to the console.
console.log(doc.getFooter().getText());

Return

FooterSection — The document footer.

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

getFootnotes()

Retrieves all the Footnote elements in the document body.

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

// 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('abc123456');

// Gets the first footnote.
const footnote = doc.getFootnotes()[0];

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

Return

Footnote[] — The document footnotes.

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

getHeader()

Retrieves the document's header section, if one exists.

// 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('abc123456');

// Gets the text of the document's header and logs it to the console.
console.log(doc.getHeader().getText());

Return

HeaderSection — The document header.

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

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://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 — 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://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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNamedRangeById(id)

Gets the NamedRange with the given ID. This method returns null if no such NamedRange exists. Names are not necessarily unique; several different ranges in the same document may share the same name, much like a class in HTML. By contrast, IDs are unique within the document, like an ID in HTML.

Parameters

NameTypeDescription
idStringthe range's ID, which is unique within the document

Return

NamedRange — the NamedRange with the given ID, or null if no such range exists

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

getNamedRanges()

Gets all NamedRange objects in the document.

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.

Return

NamedRange[] — an array of the NamedRange objects in the document, 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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNamedRanges(name)

Gets all NamedRange objects in the document with the given name. Names are not necessarily unique; several different ranges in the same document may share the same name, much like a class in HTML. By contrast, IDs are unique within the document, 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

NameTypeDescription
nameStringthe range's name, which is not necessarily unique

Return

NamedRange[] — an array of the NamedRange objects in the document with the given name

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

getSelection()

Gets the user's selection in the active document. A script can only access the selection of the user who is running the script, and only if the script is bound to the document.

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

Return

Range — A representation of the user's selection, or null if the user does not have anything selected in the document, 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://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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getUrl()

Retrieves the URL to access the current document.

var 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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getViewers()

Gets the list of viewers and commenters for this Document.

Return

User[] — 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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

newPosition(element, offset)

Creates a new Position, which is a reference to a location in the document, relative to a specific element. The user's cursor is represented as a Position, among other uses.

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

Parameters

NameTypeDescription
elementElementthe element that will contain the new Position; this must be either a Text element or a container element like Paragraph
offsetIntegerfor 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

Position — the new Position

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

newRange()

Creates a builder used to construct Range objects from document elements.

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

Return

RangeBuilder — the new builder

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

removeEditor(emailAddress)

Removes the given user from the list of editors for the Document. This method doesn't block users from accessing the Document if they belong to a class of users who have general access—for example, if the Document is shared with the user's entire domain, or if the 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

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

Document — This Document, for chaining.

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

removeEditor(user)

Removes the given user from the list of editors for the Document. This method doesn't block users from accessing the Document if they belong to a class of users who have general access—for example, if the Document is shared with the user's entire domain, or if the 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

NameTypeDescription
userUserA representation of the user to remove.

Return

Document — This Document, for chaining.

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

removeViewer(emailAddress)

Removes the given user from the list of viewers and commenters for the 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 Document if they belong to a class of users who have general access—for example, if the Document is shared with the user's entire domain, or if the 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

NameTypeDescription
emailAddressStringThe email address of the user to remove.

Return

Document — This Document for chaining.

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

removeViewer(user)

Removes the given user from the list of viewers and commenters for the 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 Document if they belong to a class of users who have general access—for example, if the Document is shared with the user's entire domain, or if the 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

NameTypeDescription
userUserA representation of the user to remove.

Return

Document — This Document for chaining.

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

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://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setCursor(position)

Sets the user's cursor in the active document, given a Position. A script can only access the cursor of the user who is running the script, and only if the script is bound to the document.

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

Parameters

NameTypeDescription
positionPositionthe new cursor location

Return

Document — this Document, for chaining

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

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 getSupportedLanguageCodes() to get all the valid language codes.

Parameters

NameTypeDescription
languageCodeStringThe language code.

Return

Document — This Document, for chaining.

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

setName(name)

Sets the document title.

Parameters

NameTypeDescription
nameStringthe new document title

Return

Document — the current document

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

setSelection(range)

Sets the user's selection in the active document, given a Range. A script can only access the selection of the user who is running the script, and only if the script is bound to the document.

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

Parameters

NameTypeDescription
rangeRangethe new range of elements to select

Return

Document — this Document, for chaining

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