文件分頁,內含多媒體文字和表格、清單等元素。
使用 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,也就是具有名稱和 ID 的 Range,供日後擷取。 |
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,也就是具有名稱和 ID 的 Range,可用於日後擷取。名稱不一定會是專屬名稱,即使是跨分頁也一樣;同一份文件中的多個不同範圍可以共用相同名稱,就像 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