ドキュメントタブ: リッチテキストと、表やリストなどの要素が含まれます。
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 | タブの footer セクションが存在しない場合は追加します。 |
add | Header | タブヘッダー セクションが存在しない場合は、タブヘッダー セクションを追加します。 |
add | Named | Named を追加します。これは、後で取得するために使用する名前と ID を持つ Range です。 |
get | Body | タブの Body を取得します。 |
get | Bookmark | 指定された ID の Bookmark を取得します。 |
get | Bookmark[] | タブ内のすべての Bookmark オブジェクトを取得します。 |
get | Footer | タブのフッター セクション(存在する場合)を取得します。 |
get | Footnote[] | タブの本文内のすべての Footnote 要素を取得します。 |
get | Header | タブのヘッダー セクション(存在する場合)を取得します。 |
get | Named | 指定された ID の Named を取得します。 |
get | Named | タブ内のすべての Named オブジェクトを取得します。 |
get | Named | 指定された名前のタブ内のすべての Named オブジェクトを取得します。 |
new | Position | 特定の要素を基準としたタブ内の位置への参照である、新しい Position を作成します。 |
new | Range | タブ要素から Range オブジェクトを作成するために使用されるビルダーを作成します。 |
詳細なドキュメント
add Bookmark(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());
パラメータ
| 名前 | 型 | 説明 |
|---|---|---|
position | Position | 新しいブックマークの位置。 |
戻る
Bookmark - 新しいブックマーク。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 - タブのヘッダー。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 - タブの本文セクション。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 - 指定された ID の Bookmark。タブ内にそのような Bookmark が存在しない場合は null。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 オブジェクトの配列。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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[] - タブの脚注。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 - タブのヘッダー。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Named Ranges()
タブ内のすべての Named オブジェクトを取得します。
Named には、タブにアクセスするすべてのスクリプトからアクセスできます。スクリプト間で意図しない競合が発生しないようにするには、範囲名に一意の文字列を接頭辞として追加することを検討してください。
戻る
Named - タブ内の Named オブジェクトの配列。同じ名前の範囲が複数含まれている場合があります。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 オブジェクトの配列。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
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 - 新しいビルダー。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents