Class DocumentTab

[Document] タブ

ドキュメントタブ: リッチテキストと、表やリストなどの要素が含まれます。

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タブの footer セクションが存在しない場合は追加します。
addHeader()HeaderSectionタブヘッダー セクションが存在しない場合は、タブヘッダー セクションを追加します。
addNamedRange(name, range)NamedRangeNamedRange を追加します。これは、後で取得するために使用する名前と ID を持つ Range です。
getBody()Bodyタブの Body を取得します。
getBookmark(id)Bookmark指定された ID の Bookmark を取得します。
getBookmarks()Bookmark[]タブ内のすべての Bookmark オブジェクトを取得します。
getFooter()FooterSectionタブのフッター セクション(存在する場合)を取得します。
getFootnotes()Footnote[]タブの本文内のすべての Footnote 要素を取得します。
getHeader()HeaderSectionタブのヘッダー セクション(存在する場合)を取得します。
getNamedRangeById(id)NamedRange指定された ID の NamedRange を取得します。
getNamedRanges()NamedRange[]タブ内のすべての NamedRange オブジェクトを取得します。
getNamedRanges(name)NamedRange[]指定された名前のタブ内のすべての NamedRange オブジェクトを取得します。
newPosition(element, offset)Position特定の要素を基準としたタブ内の位置への参照である、新しい Position を作成します。
newRange()RangeBuilderタブ要素から Range オブジェクトを作成するために使用されるビルダーを作成します。

詳細なドキュメント





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 - 指定された 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 - タブのフッター。

承認

このメソッドを使用するスクリプトには、次のスコープの 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[] - タブの脚注。

承認

このメソッドを使用するスクリプトには、次のスコープの 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 - 指定された 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