Class DocumentTab

DocumentTab

リッチテキストと、テーブルやリストなどの要素を含むドキュメントタブ。

Document.getTabs()[tabIndex].asDocumentTab() を使用してドキュメント タブを取得します。

// Get a specific document tab based on the tab ID.
var documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();

メソッド

メソッド戻り値の型概要
addBookmark(position)Bookmark指定された PositionBookmark を追加します。
addFooter()FooterSectionタブのフッター セクションを追加します(存在しない場合)。
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 オブジェクトを作成するために使用されるビルダーを作成します。

詳細なドキュメント

addBookmark(position)

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

// Opens the Docs file by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 は 一意である必要があります。NamedRange を追加した後に 削除のみ可能です。

タブにアクセスするすべてのスクリプトが NamedRange にアクセスできます。予期せぬ事象の発生と スクリプト間で競合する場合は、範囲名の先頭に一意の文字列を付けることを検討してください。

// Creates a named range that includes every table in the tab with tab ID 't.0'.
var documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();
var rangeBuilder = documentTab.newRange();
var tables = documentTab.getBody().getTables();
for (var 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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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 by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').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

getHeader()

タブのヘッダー セクションを取得します(存在する場合)。

// Opens the Docs file by its ID and retrieves the tab with ID 't.0'. If you created your
// script from within a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the doc ID and the tab ID with your own.
const documentTab = DocumentApp.openById('abc123456').getTab('t.0').asDocumentTab();

// Gets the text of the tab's header and logs it to the console.
console.log(documentTab.getHeader().getText());

戻る

HeaderSection - タブのヘッダー。

承認

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

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

getNamedRangeById(id)

指定された ID の NamedRange を取得します。該当するものがない場合、このメソッドは null を返します。 NamedRange がタブに存在します。タブ間でも、名前が一意であるとは限りません。 クラスのように、同じドキュメント内の異なる範囲の複数の範囲が同じ名前を共有する場合がある 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

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.
var doc = DocumentApp.openById('abc123456');
var documentTab = doc.getTab('t.0').asDocumentTab();
var paragraph = documentTab.getBody().appendParagraph('My new paragraph.');
var 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.
var doc = DocumentApp.openById('abc123456');
var documentTab = doc.getTab('t.0').asDocumentTab();
var rangeBuilder = documentTab.newRange();
var tables = documentTab.getBody().getTables();
for (var 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