Class Document

文档

文档,包含富文本以及表格和列表等元素。

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

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

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

方法

方法返回类型简介
addBookmark(position)Bookmark在指定的 Position 处添加 Bookmark
addEditor(emailAddress)Document将指定用户添加到 Document 的编辑者列表中。
addEditor(user)Document将指定用户添加到 Document 的编辑者列表中。
addEditors(emailAddresses)Document将给定的用户数组添加到 Document 的编辑者列表中。
addFooter()FooterSection添加文档页脚部分(如果不存在)。
addHeader()HeaderSection添加文档标题部分(如果不存在)。
addNamedRange(name, range)NamedRange添加 NamedRange,它是具有名称和 ID 的 Range,可在日后进行检索时使用。
addViewer(emailAddress)Document将指定用户添加到 Document 的查看者列表中。
addViewer(user)Document将指定用户添加到 Document 的查看者列表中。
addViewers(emailAddresses)Document将给定用户数组添加到 Document 的查看者列表中。
getAs(contentType)Blob将当前的 Document 内容作为指定类型的 blob 进行检索。
getBlob()Blob以 blob 的形式检索当前的 Document 内容。
getBody()Body检索活动文档的 Body
getBookmark(id)Bookmark获取具有指定 ID 的 Bookmark
getBookmarks()Bookmark[]获取文档中的所有 Bookmark 对象。
getCursor()Position获取当前文档中用户的光标。
getEditors()User[]获取此 Document 的编辑者列表。
getFooter()FooterSection检索文档的页脚部分(如果有)。
getFootnotes()Footnote[]检索文档正文中的所有 Footnote 元素。
getHeader()HeaderSection检索文档的标头部分(如果存在)。
getId()String检索文档的唯一标识符。
getLanguage()String获取文档的语言代码。
getName()String检索文档的标题。
getNamedRangeById(id)NamedRange获取具有指定 ID 的 NamedRange
getNamedRanges()NamedRange[]获取文档中的所有 NamedRange 对象。
getNamedRanges(name)NamedRange[]获取文档中具有指定名称的所有 NamedRange 对象。
getSelection()Range获取用户在活动文档中所做的选择。
getSupportedLanguageCodes()String[]获取 Google 文档文件中支持的所有语言代码。
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
setCursor(position)Document根据 Position 设置用户在活动文档中光标。
setLanguage(languageCode)Document设置文档的语言代码。
setName(name)Document设置文档标题。
setSelection(range)Document根据 Range,设置用户在活动文档中所做的选择。

详细文档

addBookmark(position)

在指定的 Position 处添加 Bookmark

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

}

参数

名称类型说明
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()

添加文档页脚部分(若不存在)。

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

弃踢回攻

FooterSection - 文档页脚。

授权

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

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

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.
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,可在日后进行检索时使用。名称不一定是唯一的;同一文档中的多个不同范围可以共用相同的名称,这与 HTML 中的类非常相似。相比之下,ID 在文档中是唯一的,就像 HTML 中的 ID 一样。向文档添加 NamedRange 后,您无法修改它,只能将其移除。

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

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

参数

名称类型说明
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

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'

弃踢回攻

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

文档可能包含不同类型的部分(例如 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 document 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。如果不存在此类 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.
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 对象。

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

弃踢回攻

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

检索文档的页脚部分(如果有)。

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

弃踢回攻

FooterSection - 文档页脚。

授权

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

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

getFootnotes()

检索文档正文中的所有 Footnote 元素。

调用 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.
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()

检索文档的标头部分(如果存在)。

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

弃踢回攻

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

获取文档的语言代码。这是文档编辑器的文件 > 语言中显示的语言,可能不是文档包含的实际语言。

弃踢回攻

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。如果不存在此类 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[] - 文档中 NamedRange 对象的数组,可能包括多个同名范围

授权

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

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

getNamedRanges(name)

获取文档中具有指定名称的所有 NamedRange 对象。名称不必是唯一的;同一文档中的多个不同范围可以共用同一个名称,这与 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

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,有其他用途。

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

参数

名称类型说明
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 对象的构建器。

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

弃踢回攻

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。导致系统刷新和应用待处理的更新。

系统会在脚本执行结束时,为每个处于打开状态的可修改 Document 自动调用 saveAndClose() 方法。

无法修改已关闭的Document。使用 DocumentApp.openById() 可重新打开给定文档进行编辑。

授权

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

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

setCursor(position)

根据 Position 设置用户在活动文档中光标。仅当脚本已绑定到文档时,脚本才能访问运行该脚本的用户的游标。

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

参数

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

弃踢回攻

Document - 此 Document,用于链接

授权

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

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

setLanguage(languageCode)

设置文档的语言代码。这是文档编辑器的文件 > 语言中显示的语言,可能不是文档包含的实际语言。使用 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,设置用户在活动文档中所做的选择。仅当脚本已绑定到文档时,脚本才能访问运行该脚本的用户的所选内容。

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

参数

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

弃踢回攻

Document - 此 Document,用于链接

授权

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

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