Class Document

文档

一个文档,包含一个或多个 Tab 对象,每个对象都包含富文本和 (如表格和列表)。

您可以使用 DocumentApp 打开或创建文档。

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

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

Document 类中可直接访问和修改文本内容的方法 活动标签页(在脚本中绑定到 或第一个标签页(如果没有有效标签页)。脚本依赖 这些方法(例如 getBody())可进行迁移,以支持标签页使用方法 先按 getTabs(),然后按 Tab.asDocumentTab()

方法

方法返回类型简介
addBookmark(position)Bookmark将指定 Position 处的 Bookmark 添加到第一个标签页;或者,对于 绑定到一个文档,即活跃标签页。
addEditor(emailAddress)Document将指定用户添加到 Document 的编辑者列表中。
addEditor(user)Document将指定用户添加到 Document 的编辑者列表中。
addEditors(emailAddresses)DocumentDocument 的编辑器列表添加给定的一组用户。
addFooter()FooterSection如果不存在页脚部分,则向第一个标签页添加页脚部分;对于绑定到文档的脚本,则添加使用中的标签页。
addHeader()HeaderSection将标头部分(如果不存在)添加到第一个标签页,或者为绑定到文档的脚本添加活动标签页。
addNamedRange(name, range)NamedRange添加了 NamedRange,这是具有要使用的名称和 ID 的 Range 之后检索,在第一个标签页中,或者对于绑定到文档的脚本,在活动标签页中。
addViewer(emailAddress)Document将指定用户添加到 Document 的查看者列表中。
addViewer(user)Document将指定用户添加到 Document 的查看者列表中。
addViewers(emailAddresses)Document将指定的一组用户添加到 Document 的查看者列表。
getActiveTab()Tab获取文档中用户当前处于活跃状态的 Tab
getAs(contentType)Blob将当前 Document 内容检索为指定类型的 blob。
getBlob()Blob以 blob 的形式检索当前 Document 内容。
getBody()Body检索第一个标签页的 Body;对于绑定到文档的脚本,检索活动标签页的 DocumentBodySection
getBookmark(id)Bookmark获取第一个标签页中具有指定 ID 的 Bookmark,或者获取活动标签页(对于绑定到文档的脚本)。
getBookmarks()Bookmark[]获取第一个标签页中的所有 Bookmark 对象;对于绑定到文档的脚本,获取当前标签页中的所有 Bookmark 对象。
getCursor()Position获取用户在活动标签页中的光标。
getEditors()User[]获取此 Document 的编辑器列表。
getFooter()FooterSection检索第一个标签页的页脚部分;对于绑定到文档的脚本,检索当前标签页的页脚 部分。
getFootnotes()Footnote[]检索第一个标签页正文中的所有 Footnote 元素;对于绑定到文档的脚本,检索当前标签页正文中的所有 Footnote 元素。
getHeader()HeaderSection检索第一个标签页的标题部分;对于绑定到文档的脚本,检索当前标签页的标题部分 部分。
getId()String检索文档的唯一标识符。
getLanguage()String获取文档的语言代码。
getName()String检索文档的标题。
getNamedRangeById(id)NamedRange获取第一个标签页中具有指定 ID 的 NamedRange,或者获取活动标签页(对于绑定到文档的脚本)。
getNamedRanges()NamedRange[]获取第一个标签页中的所有 NamedRange 对象;对于绑定到文档的脚本,获取当前标签页中的所有 NamedRange 对象。
getNamedRanges(name)NamedRange[]获取第一个标签页中具有指定名称的所有 NamedRange 对象;或者,对于 绑定到文档(即活动标签页)
getSelection()Range获取用户在活动标签页中的选择。
getSupportedLanguageCodes()String[]获取 Google 文档文件中支持的所有语言代码。
getTab(tabId)Tab获取具有指定 ID 的 Tab
getTabs()Tab[]获取文档中的所有未嵌套 Tab
getUrl()String检索用于访问当前文档的网址。
getViewers()User[]获取此 Document 的查看者和评论者的列表。
newPosition(element, offset)Position创建一个新的 Position,这是相对于标签页中某个位置的引用。 特定元素;对于绑定到文档的脚本,则为活动标签页。
newRange()RangeBuilder创建一个构建器,用于根据Range 或者,对于绑定到 文档,即活动标签页。
removeEditor(emailAddress)DocumentDocument 的编辑者列表中移除指定用户。
removeEditor(user)DocumentDocument 的编辑者列表中移除指定用户。
removeViewer(emailAddress)DocumentDocument 的查看者和评论者列表中移除指定用户。
removeViewer(user)DocumentDocument 的查看者和评论者列表中移除指定用户。
saveAndClose()void保存当前的 Document
setActiveTab(tabId)void将用户在当前文档中选择的 Tab 设为具有指定 ID 的标签页。
setCursor(position)Document在给定 Position 的情况下设置用户的光标。
setLanguage(languageCode)Document设置文档的语言代码。
setName(name)Document设置文档标题。
setSelection(range)Document在给定 Range 的情况下,设置用户在活动标签页中的选择。

详细文档

addBookmark(position)

将指定 Position 处的 Bookmark 添加到第一个标签页;或者,对于 绑定到一个文档,即活跃标签页。要将 添加到任何标签页,请使用 DocumentTab.addBookmark(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 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());

}

参数

名称类型说明
positionPosition新书签的位置。

返回

Bookmark - 新书签。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addEditor(emailAddress)

将指定用户添加到 Document 的编辑者列表中。如果用户已经 ,则此方法会将该用户从查看者列表中进行升级。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addEditor(user)

将指定用户添加到 Document 的编辑者列表中。如果用户已经 ,则此方法会将该用户从查看者列表中进行升级。

参数

名称类型说明
userUser表示要添加的用户。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addEditors(emailAddresses)

Document 的编辑器列表添加给定的一组用户。如果存在 用户已经存在于查看者列表中,则此方法会将他们从 观看者。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址的数组。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addFooter()

如果不存在页脚部分,则向第一个标签页添加页脚部分;对于绑定到文档的脚本,则添加使用中的标签页。添加页脚 部分,请使用 DocumentTab.addFooter() 方法。

// 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's active or first tab.
const footer = doc.addFooter();

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

返回

FooterSection - 标签页页脚。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addHeader()

将标头部分(如果不存在)添加到第一个标签页,或者为绑定到文档的脚本添加活动标签页。添加标题 部分,请使用 DocumentTab.addHeader() 方法。

// 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's active or first tab.
const header = doc.addHeader();

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

返回

HeaderSection - 标签页标题。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addNamedRange(name, range)

添加了 NamedRange,这是具有要使用的名称和 ID 的 Range 之后检索,在第一个标签页中,或者对于绑定到文档的脚本,在活动标签页中。如需在任何标签页中添加 NamedRange,请使用 DocumentTab.addNamedRange(name, range) 方法。名称不正确 必须唯一;同一文档中几个不同的范围可以共用一个名称, 就像 HTML 中的类一样。相比之下,ID 在文档中是唯一的,就像 HTML 中的 ID 一样。 将 NamedRange 添加到文档后,您便无法修改它,而只能将其移除。

访问文档的任何脚本都可以访问 NamedRange。为避免出现意外情况, 冲突,请考虑在范围名称前添加一个唯一字符串作为前缀。

// Creates a named range that includes every table in the active tab.
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]);
}
// Adds the named range to the document's active tab.
doc.addNamedRange('Document tables', rangeBuilder.build());

参数

名称类型说明
nameString范围的名称,无需具有唯一性;范围名称必须为 介于 1 到 256 个字符之间。
rangeRange与名称关联的元素范围;该范围可以是有效选择搜索结果,也可以是使用 newRange() 手动构建的范围。

返回

NamedRange - NamedRange

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addViewer(emailAddress)

将指定用户添加到 Document 的查看者列表中。如果用户已经 ,则此方法不会产生任何效果。

参数

名称类型说明
emailAddressString要添加的用户的电子邮件地址。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addViewer(user)

将指定用户添加到 Document 的查看者列表中。如果用户已经 ,则此方法不会产生任何效果。

参数

名称类型说明
userUser表示要添加的用户。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

addViewers(emailAddresses)

将指定的一组用户添加到 Document 的查看者列表。如果存在 用户已经位于编辑者列表中,则此方法对他们没有影响。

参数

名称类型说明
emailAddressesString[]要添加的用户的电子邮件地址的数组。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getActiveTab()

获取文档中用户当前处于活跃状态的 Tab。脚本只能访问 当前运行该脚本的用户的“当前活动”标签页,且仅限该脚本绑定到文档时。

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

返回

Tab - 用户当前处于活跃状态的 Tab,或者 null(如果脚本未绑定到 文档。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getAs(contentType)

将当前 Document 内容检索为指定类型的 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');

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

参数

名称类型说明
contentTypeString要转换为的 MIME 类型;支持 'application/pdf''text/markdown'

返回

Blob - 以 blob 形式表示的当前文档。


getBlob()

以 blob 的形式检索当前 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.
// 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());

返回

Blob - 以 blob 形式表示的当前文档。


getBody()

检索第一个标签页的 Body;对于绑定到文档的脚本,检索活动标签页的 DocumentBodySection。如需获取任何标签页的 DocumentBodySection,请使用 DocumentTab.getBody() 方法。

标签页可以包含不同类型的部分(例如 HeaderSectionFooterSection)。标签页的活动部分是 Body

Document 中的元素方法委托给有效的 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 active or first tab's body.
const body = doc.getBody();

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

返回

Body - 标签页正文部分。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getBookmark(id)

获取第一个标签页中具有指定 ID 的 Bookmark,或者获取活动标签页(对于绑定到文档的脚本)。要获得 书签,请使用 DocumentTab.getBookmark(id) 方法。如果标签页中不存在此类 Bookmark,此方法会返回 null

// 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 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.');
}

参数

名称类型说明
idStringBookmark 的 ID。

返回

Bookmark - 具有指定 ID 的 Bookmark,如果没有 Bookmark,则为 null

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getBookmarks()

获取第一个标签页中的所有 Bookmark 对象;对于绑定到文档的脚本,获取当前标签页中的所有 Bookmark 对象。要获取所有 书签,请使用 DocumentTab.getBookmarks() 方法。

// 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's active or first tab.
const bookmarks = doc.getBookmarks();

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

返回

Bookmark[] - 标签页中的 Bookmark 对象的数组。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getCursor()

获取用户在活动标签页中的光标。脚本只能访问 是运行脚本,并且仅当该脚本绑定到文档时才运行。

// 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.');
}

返回

Position — 用户光标的表示形式,或者null 如果用户没有 光标放置于该标签页中,或者脚本未与文档绑定。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getEditors()

获取此 Document 的编辑器列表。

返回

User[] - 具有修改权限的用户数组。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getFooter()

检索第一个标签页的页脚部分;对于绑定到文档的脚本,检索当前标签页的页脚 部分。如需获取任何标签页的页脚部分,请使用 DocumentTab.getFooter() 方法。

// 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 active or first tab's footer and logs it to the console.
console.log(doc.getFooter().getText());

返回

FooterSection - 标签页的页脚。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getFootnotes()

检索第一个标签页正文中的所有 Footnote 元素;对于绑定到文档的脚本,检索当前标签页正文中的所有 Footnote 元素。要获得 任何标签页中的所有脚注,请使用 DocumentTab.getFootnotes() 方法。

调用 getFootnotes 会迭代标签页的元素。对于大标签页 避免对此方法进行不必要的调用。

// 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 in the active or first tab's body.
const footnote = doc.getFootnotes()[0];

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

返回

Footnote[] - 标签页的脚注。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getHeader()

检索第一个标签页的标题部分;对于绑定到文档的脚本,检索当前标签页的标题部分 部分。如需获取任何标签页的标题部分,请使用 DocumentTab.getHeader() 方法。

// 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 active or first tab's header and logs it to the console.
console.log(doc.getHeader().getText());

返回

HeaderSection - 标签页的标题。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getId()

检索文档的唯一标识符。文档 ID 与 DocumentApp.openById() 用于打开特定文档实例。

返回

String - 文档的 ID。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getLanguage()

获取文档的语言代码。这是文档编辑器的文件 &gt;语言,这可能不是文档包含的实际语言。

返回

String - 文档语言;如果未定义,则为 null

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getName()

检索文档的标题。

返回

String - 文档标题。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getNamedRangeById(id)

获取第一个标签页中具有指定 ID 的 NamedRange,或者获取活动标签页(对于绑定到文档的脚本)。要获取 在任何标签页中使用具有指定 ID 的 NamedRange,请使用 DocumentTab.getNamedRangeById(id) 方法。如果标签页中不存在此类 NamedRange,此方法会返回 null。名称 不一定是唯一的,即使在不同的标签页上也是如此;同一个标签页中有多个不同的范围 与 HTML 中的类很相似。相比之下,ID 在标签页中是唯一的 像 HTML 中的 ID 一样

参数

名称类型说明
idString范围的 ID,在该标签页中是唯一的。

返回

NamedRange - 具有指定 ID 的 NamedRange,如果 中不存在此类范围,则为 null 标签页。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getNamedRanges()

获取第一个标签页中的所有 NamedRange 对象;对于绑定到文档的脚本,获取当前标签页中的所有 NamedRange 对象。要获取所有 NamedRange 对象,请使用 DocumentTab.getNamedRanges() 方法。

访问该标签页的任何脚本都可以访问 NamedRange。为避免 脚本之间可能会发生意外冲突,请考虑为范围名称添加唯一字符串作为前缀。

返回

NamedRange[] - 标签页中的 NamedRange 对象数组,可能包括多个 同名的范围

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getNamedRanges(name)

获取第一个标签页中具有指定名称的所有 NamedRange 对象;或者,对于 绑定到文档(即活动标签页)。要获得 任何标签页中的所有 NamedRange 对象,请使用 DocumentTab.getNamedRanges(name) 方法。名称不一定是唯一的,即使在不同的标签页中也是如此;多个不同的范围 同一个标签页可以重名,与 HTML 中的类非常相似。相比之下,ID 是唯一的 就像 HTML 中的 ID 一样。

访问文档的任何脚本都可以访问 NamedRange。为避免 脚本之间可能会发生意外冲突,请考虑为范围名称添加唯一字符串作为前缀。

参数

名称类型说明
nameString范围的名称,不一定是唯一的。

返回

NamedRange[] - 具有指定名称的标签页中的 NamedRange 对象数组。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getSelection()

获取用户在活动标签页中的选择。脚本只能访问 脚本的运行者,并且仅当脚本已绑定到文档时才发送。

// 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.');
}

返回

Range — 表示用户所做选择,或者null如果用户未 标签页中选中的任何内容,如果只选择了段落结尾的内容,而只选择段落结尾的内容 段落位置和新行都选中,或者脚本未绑定到文档。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getSupportedLanguageCodes()

获取 Google 文档文件中支持的所有语言代码。

返回

String[] - 一组语言代码。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getTab(tabId)

获取具有指定 ID 的 Tab。如果不存在此类 Tab,此方法会返回 null。可以访问任何嵌套级别的标签页。

参数

名称类型说明
tabIdString要获取的标签页的 ID。

返回

Tab - 具有指定 ID 的 Tab,如果不存在此类 Tab,则为 null

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getTabs()

获取文档中的所有未嵌套 Tab

标签页可以包含子标签页,也就是嵌套在另一个标签页中的标签页。子标签页可供访问 使用 Tab.getChildTabs()

返回

Tab[] - 文档中所有 Tab 的列表。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getUrl()

检索用于访问当前文档的网址。

var doc = DocumentApp.getActiveDocument();

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

返回

String - 用于访问当前文档的网址。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getViewers()

获取此 Document 的查看者和评论者的列表。

返回

User[] - 具有查看或评论权限的用户数组。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

newPosition(element, offset)

创建一个新的 Position,这是相对于标签页中某个位置的引用。 特定元素;对于绑定到文档的脚本,则为活动标签页。要创建 Position(相对于任何标签页中的位置),请使用 DocumentTab.newPosition(element, offset) 方法。用户的光标以 Position 的形式表示为其他用途。

// Append a paragraph to the active tab, 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);

参数

名称类型说明
elementElement应包含新 Position 的元素;该参数必须是 Text 元素或容器元素(如 Paragraph)。
offsetInteger对于 Text 元素,Position 之前的字符数; 对于其他元素,Position 相同的容器元素。

返回

Position - 新的 Position

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

newRange()

创建一个构建器,用于根据Range 或者,对于绑定到 文档,即活动标签页。创建用于构建 DocumentRange 对象的构建器 ,请使用 DocumentTab.newRange() 方法。

// Change the user's selection to a range that includes every table in the active tab.
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());

返回

RangeBuilder - 新的构建器。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

removeEditor(emailAddress)

Document 的编辑者列表中移除指定用户。此方法 如果用户属于符合以下条件的某个类别,则禁止该用户访问Document 例如,如果 Document 与用户的整个 网域,或者如果 Document 位于用户有权访问的共享云端硬盘中。

对于云端硬盘文件,此操作还会将相应用户从查看者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

removeEditor(user)

Document 的编辑者列表中移除指定用户。此方法 如果用户属于符合以下条件的某个类别,则禁止该用户访问Document 例如,如果 Document 与用户的整个 网域,或者如果 Document 位于用户有权访问的共享云端硬盘中。

对于云端硬盘文件,此操作还会将相应用户从查看者列表中移除。

参数

名称类型说明
userUser要移除的用户的表示法。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

removeViewer(emailAddress)

Document 的查看者和评论者列表中移除指定用户。本次 方法则不起作用。此方法还会 如果用户属于Document 具有常规访问权限,例如,如果 Document 与用户的 也可以指定 Document 在用户可以访问的共享云端硬盘中。

对于云端硬盘文件,此操作也会将用户从编辑者列表中移除。

参数

名称类型说明
emailAddressString要移除的用户的电子邮件地址。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

removeViewer(user)

Document 的查看者和评论者列表中移除指定用户。本次 方法则不起作用。此方法也不会阻止 如果用户属于对具有常规性访问权限的某一类用户,则他们无法访问 Document。 访问权限 - 例如,如果 Document 与用户的整个网域共享,或者 如果Document位于用户可以访问的共享云端硬盘中。

对于云端硬盘文件,此操作也会将用户从编辑者列表中移除。

参数

名称类型说明
userUser要移除的用户的表示法。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

saveAndClose()

保存当前的 Document。会导致刷新并应用待处理的更新。

脚本执行结束时会自动调用 saveAndClose() 方法 Document

无法修改已关闭的Document。使用DocumentApp.openById()重新打开 指定文档以供修改。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setActiveTab(tabId)

将用户在当前文档中选择的 Tab 设为具有指定 ID 的标签页。

const doc = DocumentApp.getActiveDocument()

// TODO(developer): Replace the ID with your own.
// Sets the user's selected tab to the tab with ID "t.0".
const tab = doc.setActiveTab('t.0');

参数

名称类型说明
tabIdString要设为活动的标签页的 ID。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setCursor(position)

在给定 Position 的情况下设置用户的光标。脚本只能访问 运行脚本的用户,并且仅当该脚本绑定到文档时。

从非活跃 Tab 提供 Position 会切换用户的活跃标签页。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();

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

参数

名称类型说明
positionPosition新的光标位置。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setLanguage(languageCode)

设置文档的语言代码。这是文档编辑器的文件 &gt;语言,这可能不是文档包含的实际语言。使用 getSupportedLanguageCodes() 可获取所有有效的语言代码。

参数

名称类型说明
languageCodeString语言代码。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setName(name)

设置文档标题。

参数

名称类型说明
nameString新文档标题。

返回

Document - 当前文档。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setSelection(range)

在给定 Range 的情况下,设置用户在活动标签页中的选择。一个脚本只能 查看运行脚本的用户的选择,且仅当脚本绑定到文档时才可访问。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();

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

参数

名称类型说明
rangeRange要选择的新元素范围。

返回

Document - 此 Document,用于链接。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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