文档,包含富文本以及表格和列表等元素。
您可以使用 DocumentApp
打开或创建文档。
// Open a document by ID. var doc = DocumentApp.openById("<my-id>"); // Create and open a document. doc = DocumentApp.create("Document Title");
方法
详细文档
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 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()); }
参数
名称 | 类型 | 说明 |
---|---|---|
position | Position | 新书签的位置。 |
弃踢回攻
Bookmark
- 新书签。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
addEditor(emailAddress)
addEditor(user)
addEditors(emailAddresses)
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());
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 范围的名称,不必是唯一的;范围名称必须介于 1-256 个字符之间。 |
range | Range | 与名称相关联的元素范围;范围可以是有效选择、搜索结果,也可以使用 newRange() 手动构建。 |
弃踢回攻
NamedRange
- NamedRange
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
addViewer(emailAddress)
addViewer(user)
addViewers(emailAddresses)
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());
参数
名称 | 类型 | 说明 |
---|---|---|
contentType | String | 要转换为的 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
。
文档可能包含不同类型的部分(例如 HeaderSection
、FooterSection
)。文档的有效部分是 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.'); }
参数
名称 | 类型 | 说明 |
---|---|---|
id | String | Bookmark 的 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()
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 一样。
参数
名称 | 类型 | 说明 |
---|---|---|
id | String | 范围的 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
。为避免脚本之间发生意外冲突,不妨考虑在范围名称前添加一个唯一字符串作为前缀。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 范围的名称(不一定是唯一的) |
弃踢回攻
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()
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);
参数
名称 | 类型 | 说明 |
---|---|---|
element | Element | 将包含新 Position 的元素;它必须是 Text 元素或 Paragraph 这样的容器元素 |
offset | Integer | 对于 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
。
对于云端硬盘文件,此操作会同时将用户从查看者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要移除的用户的电子邮件地址。 |
弃踢回攻
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeEditor(user)
从 Document
的编辑者列表中移除指定用户。如果用户属于具有常规访问权限的用户类(例如,与用户的整个网域共享 Document
,或者 Document
位于用户可以访问的共享云端硬盘中),则此方法不会阻止用户访问 Document
。
对于云端硬盘文件,此操作会同时将用户从查看者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要移除的用户。 |
弃踢回攻
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeViewer(emailAddress)
从 Document
的查看者和评论者列表中移除指定用户。如果用户是编辑者,而不是查看者或评论者,则此方法不会产生任何影响。如果用户属于具有常规访问权限的一类用户(例如,与用户的整个网域共享 Document
,或者 Document
位于用户可以访问的共享云端硬盘中),则此方法也不会阻止用户访问 Document
。
对于云端硬盘文件,此操作会同时将用户从编辑者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
emailAddress | String | 要移除的用户的电子邮件地址。 |
弃踢回攻
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeViewer(user)
从 Document
的查看者和评论者列表中移除指定用户。如果用户是编辑者(而不是查看者),则此方法不会产生任何影响。如果用户属于具有常规访问权限的用户类(例如,如果 Document
与用户的整个网域共享,或者 Document
位于用户可以访问的共享云端硬盘中),则此方法也不会阻止用户访问 Document
。
对于云端硬盘文件,此操作会同时将用户从编辑者列表中移除。
参数
名称 | 类型 | 说明 |
---|---|---|
user | User | 表示要移除的用户。 |
弃踢回攻
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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);
参数
名称 | 类型 | 说明 |
---|---|---|
position | Position | 新的光标位置 |
弃踢回攻
Document
- 此 Document
,用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setLanguage(languageCode)
设置文档的语言代码。这是文档编辑器的文件 > 语言中显示的语言,可能不是文档包含的实际语言。使用 getSupportedLanguageCodes()
可获取所有有效的语言代码。
参数
名称 | 类型 | 说明 |
---|---|---|
languageCode | String | 语言代码。 |
弃踢回攻
Document
- 此 Document
,用于链接。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setName(name)
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());
参数
名称 | 类型 | 说明 |
---|---|---|
range | Range | 要选择的新元素范围 |
弃踢回攻
Document
- 此 Document
,用于链接
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents