一个包含富文本和表格、列表等元素的文档标签页。
使用 Document.getTabs()[tabIndex].asDocumentTab() 检索文档标签页。
// Get a specific document tab based on the tab ID. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();
方法
| 方法 | 返回类型 | 简介 |
|---|---|---|
add | Bookmark | 在给定的 Position 处添加 Bookmark。 |
add | Footer | 添加标签页页脚部分(如果不存在)。 |
add | Header | 添加标签页标题部分(如果不存在)。 |
add | Named | 添加一个 Named,这是一个 Range,具有名称和 ID,可用于后续检索。 |
get | Body | 检索标签页的 Body。 |
get | Bookmark|null | 获取具有指定 ID 的 Bookmark。 |
get | Bookmark[] | 获取标签页中的所有 Bookmark 对象。 |
get | Footer | 检索标签页的页脚部分(如果存在)。 |
get | Footnote[]|null | 检索标签页正文中的所有 Footnote 元素。 |
get | Header | 检索标签页的标题部分(如果有)。 |
get | Named | 获取具有指定 ID 的 Named。 |
get | Named | 获取标签页中的所有 Named 对象。 |
get | Named | 获取标签页中具有指定名称的所有 Named 对象。 |
new | Position | 创建新的 Position,它是标签页中相对于特定元素的位置的引用。 |
new | Range | 创建一个用于从标签页元素构建 Range 对象的构建器。 |
详细文档
add Bookmark(position)
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Gets the tab body and adds a paragraph. const paragraph = documentTab.getBody().appendParagraph('My new paragraph.'); // Creates a position at the first character of the paragraph text. const position = documentTab.newPosition(paragraph.getChild(0), 0); // Adds a bookmark at the first character of the paragraph text. const bookmark = documentTab.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
add Header()
添加标签页标题部分(如果不存在)。
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Adds a header to the tab. const header = documentTab.addHeader(); // Sets the header text to 'This is a header.' header.setText('This is a header');
返回
Header - 标签页标题。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
add Named Range(name, range)
添加一个 Named,这是一个 Range,其中包含名称和 ID,以便日后检索。名称不一定唯一,即使在不同标签页中也是如此;同一文档中的多个不同范围可以共享同一名称,这与 HTML 中的类非常相似。相比之下,ID 在文档中是唯一的,就像 HTML 中的 ID 一样。添加 Named 后,您无法修改,只能移除。
任何访问标签页的脚本都可以访问 Named。为避免脚本之间发生意外冲突,请考虑为范围名称添加唯一字符串作为前缀。
// Creates a named range that includes every table in a tab by its ID. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); const rangeBuilder = documentTab.newRange(); const tables = documentTab.getBody().getTables(); for (let i = 0; i < tables.length; i++) { rangeBuilder.addElement(tables[i]); } documentTab.addNamedRange('Tab t.0 tables', rangeBuilder.build());
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 范围的名称,不必是唯一的;范围名称的长度必须介于 1-256 个字符之间。 |
range | Range | 要与名称关联的元素范围;该范围可以是搜索结果,也可以使用 new 手动构建。 |
返回
Named - Named。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Body()
检索标签页的 Body。
标签页可能包含不同类型的版块(例如 Header、Footer)。标签页的有效版块为 Body。
Document 中的元素方法委托给 Body。
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Gets the tab body. const body = documentTab.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
get Bookmark(id)
获取具有指定 ID 的 Bookmark。如果相应 Bookmark 不存在于此标签页中,此方法会返回 null。
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Gets the bookmark by its ID. const bookmark = documentTab.getBookmark('id.xyz654321'); // If the bookmark exists within the tab, 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|null - 具有指定 ID 的 Bookmark;如果标签页中不存在此类 Bookmark,则返回 null。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Bookmarks()
获取标签页中的所有 Bookmark 对象。
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Gets all of the bookmarks in the tab. const bookmarks = documentTab.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
get Footnotes()
检索标签页正文中的所有 Footnote 元素。
对 get 的调用会导致对标签页的元素进行迭代。对于大型标签页,请避免不必要地调用此方法。
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Gets the first footnote. const footnote = documentTab.getFootnotes()[0]; // Logs footnote contents to the console. console.log(footnote.getFootnoteContents().getText());
返回
Footnote[]|null - 标签页的脚注。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Header()
检索标签页的标题部分(如果有)。
// Opens the Docs file and retrieves the tab by its IDs. If you created your // script from within a Google Docs file, you can use // DocumentApp.getActiveDocument().getActiveTab() instead. // TODO(developer): Replace the IDs with your own. const documentTab = DocumentApp.openById('123abc').getTab('123abc').asDocumentTab(); // Gets the text of the tab's header and logs it to the console. console.log(documentTab.getHeader().getText());
返回
Header - 标签页的标题。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Named Range By Id(id)
获取具有指定 ID 的 Named。如果相应 Named 不存在于标签页中,此方法会返回 null。名称不一定唯一,即使在不同标签页中也是如此;同一文档中的多个不同范围可以共用同一名称,这与 HTML 中的类非常相似。相比之下,ID 在标签页中是唯一的,就像 HTML 中的 ID 一样。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
id | String | 范围的 ID,在相应标签页中具有唯一性。 |
返回
Named - 具有指定 ID 的 Named;如果相应标签页中不存在此类范围,则返回 null。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Named Ranges()
获取标签页中的所有 Named 对象。
任何访问相应标签页的脚本都可以访问 Named。为避免脚本之间发生意外冲突,请考虑为范围名称添加唯一字符串作为前缀。
返回
Named - 工作表中的 Named 对象数组,可能包含多个同名范围。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Named Ranges(name)
获取标签页中具有指定名称的所有 Named 对象。名称不一定唯一,即使在不同标签页中也是如此;同一文档中的多个不同范围可以共用同一名称,就像 HTML 中的类一样。相比之下,ID 在标签页中是唯一的,就像 HTML 中的 ID 一样。
任何访问相应标签页的脚本都可以访问 Named。为避免脚本之间发生意外冲突,请考虑为范围名称添加唯一字符串作为前缀。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
name | String | 范围的名称,不一定具有唯一性。 |
返回
Named - 具有指定名称的标签页中 Named 对象的数组。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
new Position(element, offset)
创建新的 Position,它是标签页中相对于特定元素的位置的引用。用户的光标以 Position 表示,还有其他用途。
// Append a paragraph, then place the user's cursor after the first word of the // new paragraph. // TODO(developer): Replace the IDs with your own. const doc = DocumentApp.openById('123abc'); const documentTab = doc.getTab('123abc').asDocumentTab(); const paragraph = documentTab.getBody().appendParagraph('My new paragraph.'); const position = documentTab.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
new Range()
创建用于从标签页元素构建 Range 对象的构建器。
// Change the user's selection to a range that includes every table in the tab. // TODO(developer): Replace the IDs with your own. const doc = DocumentApp.openById('123abc'); const documentTab = doc.getTab('123abc').asDocumentTab(); const rangeBuilder = documentTab.newRange(); const tables = documentTab.getBody().getTables(); for (let i = 0; i < tables.length; i++) { rangeBuilder.addElement(tables[i]); } doc.setSelection(rangeBuilder.build());
返回
Range - 新构建器。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents