Class DocumentTab

文件分頁

文件分頁,內含多媒體文字和表格、清單等元素。

使用 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();

方法

方法傳回類型簡短說明
addBookmark(position)Bookmark在指定 Position 新增 Bookmark
addFooter()FooterSection如果沒有分頁註腳區段,則新增該區段。
addHeader()HeaderSection如果沒有分頁標題區段,則新增該區段。
addNamedRange(name, range)NamedRange新增 NamedRange,也就是具有名稱和 ID 的 Range,供日後擷取。
getBody()Body擷取分頁的 Body
getBookmark(id)Bookmark|null取得具有指定 ID 的 Bookmark
getBookmarks()Bookmark[]取得分頁中的所有 Bookmark 物件。
getFooter()FooterSection|null擷取分頁的頁尾部分 (如有)。
getFootnotes()Footnote[]|null擷取分頁主體中的所有 Footnote 元素。
getHeader()HeaderSection|null擷取分頁的標題區段 (如有)。
getNamedRangeById(id)NamedRange|null取得具有指定 ID 的 NamedRange
getNamedRanges()NamedRange[]取得分頁中的所有 NamedRange 物件。
getNamedRanges(name)NamedRange[]取得具有指定名稱的分頁中的所有 NamedRange 物件。
newPosition(element, offset)Position建立新的 Position,這是指分頁中相對於特定元素的位置。
newRange()RangeBuilder建立建構工具,用於從分頁元素建構 Range 物件。

內容詳盡的說明文件

addBookmark(position)

在指定 Position 新增 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 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());

參數

名稱類型說明
positionPosition新書籤的位置。

回攻員

Bookmark:新書籤。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

addFooter()

如果沒有分頁頁尾區段,則新增該區段。

// 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 footer to the tab.
const footer = documentTab.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 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');

回攻員

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

參數

名稱類型說明
nameString範圍名稱 (不必是專用的),長度必須介於 1 至 256 個字元之間。
rangeRange要與名稱建立關聯的元素範圍;範圍可以是搜尋結果,也可以使用 newRange() 手動建構。

回攻員

NamedRange - NamedRange

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getBody()

擷取分頁的 Body

分頁可能包含不同類型的區段 (例如 HeaderSectionFooterSection)。分頁的有效區段為 Body

DocumentTab 中的元素方法會委派給 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

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

參數

名稱類型說明
idStringBookmark 的 ID。

回攻員

Bookmark|null:具有指定 ID 的 Bookmark,或如果分頁中沒有這類 Bookmark,則為 null

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getBookmarks()

取得分頁中的所有 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

getFooter()

擷取分頁的頁尾部分 (如有)。

// 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 footer and logs it to the console.
console.log(documentTab.getFooter().getText());

回攻員

FooterSection|null:分頁的頁尾。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getFootnotes()

擷取分頁主體中的所有 Footnote 元素。

getFootnotes 的呼叫會導致對分頁元素的疊代。如果是大型分頁,請避免不必要地呼叫這個方法。

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

getHeader()

擷取分頁的標題區段 (如有)。

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

回攻員

HeaderSection|null - 分頁的標題。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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|null - 具有指定 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

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

參數

名稱類型說明
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 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());

回攻員

RangeBuilder:新的建構工具。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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