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指定された PositionBookmark を追加します。
addFooter()FooterSectionタブのフッター セクションを追加します(存在しない場合)。
addHeader()HeaderSectionタブ ヘッダー セクションが存在しない場合は追加します。
addNamedRange(name, range)NamedRangeNamedRange を追加します。これは、後で取得するために使用する名前と 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)

指定された PositionBookmark を追加します。

// 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 - 新しいブックマーク。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - タブのフッター。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - タブのヘッダー。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - タブの本文セクション。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 オブジェクトの配列。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - タブのフッター。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - タブの脚注。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - タブのヘッダー。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getNamedRanges()

タブ内のすべての NamedRange オブジェクトを取得します。

NamedRange には、タブにアクセスするスクリプトからアクセスできます。スクリプト間の意図しない競合を回避するには、範囲名に一意の文字列を接頭辞として付けることを検討してください。

戻る

NamedRange[] - タブ内の NamedRange オブジェクトの配列。同じ名前の範囲が複数含まれている場合もあります。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

getNamedRanges(name)

指定された名前のタブにあるすべての NamedRange オブジェクトを取得します。名前は、タブ間でも必ずしも一意ではありません。HTML のクラスと同様に、同じドキュメント内の複数の異なる範囲で同じ名前を共有できます。一方、ID は HTML の ID のように、タブ内で一意です。

NamedRange には、タブにアクセスするスクリプトからアクセスできます。スクリプト間の意図しない競合を回避するには、範囲名に一意の文字列を接頭辞として付けることを検討してください。

パラメータ

名前説明
nameString範囲の名前。一意である必要はありません。

戻る

NamedRange[] - 指定された名前のタブにある NamedRange オブジェクトの配列。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 などのコンテナ要素のいずれかである必要があります。
offsetIntegerText 要素の場合は Position の前の文字数、その他の要素の場合は同じコンテナ要素内の Position の前の子要素の数。

戻る

Position - 新しい Position

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • 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 - 新しいビルダー。

承認

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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