Google ドキュメント ドキュメントのタブの内容。Body
には、List
、Paragraph
、Table
、Table
の各要素を含めることができます。ドキュメント構造の詳細については、Google ドキュメントの拡張に関するガイドをご覧ください。
通常、Body
には、Header
、Footer
、Footnote
要素を除くタブのコンテンツ全体が含まれます。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Append a paragraph and a page break to the tab's body section directly. body.appendParagraph('A paragraph.'); body.appendPageBreak();
メソッド
サポート終了のメソッド
メソッド | 戻り値の型 | 概要 |
---|---|---|
| Footnote[] | セクションに含まれるすべての Footnotes を取得します。 |
| String | リンクの URL を取得します。 |
| Element | 要素の次の兄弟要素を取得します。 |
| Element | 要素の前の兄弟要素を取得します。 |
| Boolean | 要素が Document の末尾にあるかどうかを判断します。 |
| Body | リンク URL を設定します。 |
詳細なドキュメント
appendHorizontalRule()
新しい Horizontal
を作成して追加します。
Horizontal
は新しい Paragraph
に含まれます。
戻る
Horizontal
- 新しい横線。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendImage(image)
指定されたイメージ ブロブから新しい Inline
を作成して追加します。
イメージは新しい Paragraph
に格納されます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
image | Blob | 画像データ。 |
戻る
Inline
- 追加された画像。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendImage(image)
指定された Inline
を追加します。
Inline
は新しい Paragraph
に含まれます。
既存の Inline
のコピーを追加する場合は、このバージョンの append
を使用します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
image | Inline | 画像データ。 |
戻る
Inline
- 追加された画像。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendListItem(listItem)
appendListItem(text)
appendPageBreak()
appendPageBreak(pageBreak)
指定された Page
を追加します。
Page
は新しい Paragraph
に含まれます。
既存の Page
のコピーを追加する場合は、このバージョンの append
を使用します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
page | Page | 追加する改ページ。 |
戻る
Page
- 追加された改ページ。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendParagraph(paragraph)
appendParagraph(text)
appendTable()
appendTable(cells)
指定された文字列値ごとに Table
を含む新しい Table
を追加します。
また、Google ドキュメントのドキュメントは表で終わることができないため、この方法では表の後に空の段落も追加されます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
cells | String[][] | 新しいテーブルに追加する表セルのテキスト コンテンツ。 |
戻る
Table
- 追加されたテーブル。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendTable(table)
指定された Table
を追加します。
既存の Table
のコピーを追加する場合は、このバージョンの append
を使用します。また、Google ドキュメントのドキュメントは表で終わることができないため、この方法では表の後に空の段落も追加されます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
table | Table | 追加するテーブル。 |
戻る
Table
- 追加されたテーブル。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
clear()
copy()
editAsText()
編集用に現在の要素の Text
バージョンを取得します。
要素の内容をリッチ テキストとして操作するには、edit
を使用します。edit
モードでは、テキスト以外の要素(Inline
や Horizontal
など)は無視されます。
削除されたテキスト範囲に完全に含まれる子要素は、要素から削除されます。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Insert two paragraphs separated by a paragraph containing an // horizontal rule. body.insertParagraph(0, 'An editAsText sample.'); body.insertHorizontalRule(0); body.insertParagraph(0, 'An example.'); // Delete " sample.\n\n An" removing the horizontal rule in the process. body.editAsText().deleteText(14, 25);
戻る
Text
- 現在の要素のテキスト バージョン
findElement(elementType)
要素の内容で、指定したタイプの子孫を検索します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
element | Element | 検索する要素のタイプ。 |
戻る
Range
- 検索要素の位置を示す検索結果。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findElement(elementType, from)
指定された Range
から、指定されたタイプの子孫を要素の内容で検索します。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Define the search parameters. let searchResult = null; // Search until the paragraph is found. while ( (searchResult = body.findElement( DocumentApp.ElementType.PARAGRAPH, searchResult, ))) { const par = searchResult.getElement().asParagraph(); if (par.getHeading() === DocumentApp.ParagraphHeading.HEADING1) { // Found one, update and stop. par.setText('This is the first header.'); break; } }
パラメータ
名前 | 型 | 説明 |
---|---|---|
element | Element | 検索する要素のタイプ。 |
from | Range | 検索結果から検索する検索結果。 |
戻る
Range
- 検索要素の次の位置を示す検索結果。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern)
正規表現を使用して、要素のコンテンツで指定されたテキスト パターンを検索します。
JavaScript 正規表現機能のサブセット(キャプチャ グループやモード修飾子など)は完全にはサポートされていません。
指定された正規表現パターンは、現在の要素に含まれる各テキスト ブロックと個別に照合されます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
search | String | 検索するパターン |
戻る
Range
- 検索テキストの位置を示す検索結果。一致がない場合、null
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
指定された検索結果から、要素のコンテンツで指定されたテキスト パターンを検索します。
JavaScript 正規表現機能のサブセット(キャプチャ グループやモード修飾子など)は完全にはサポートされていません。
指定された正規表現パターンは、現在の要素に含まれる各テキスト ブロックと個別に照合されます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
search | String | 検索するパターン |
from | Range | 検索結果 |
戻る
Range
- 検索テキストの次の位置を示す検索結果。一致がない場合、null です。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getAttributes()
要素の属性を取得します。
結果は、有効な要素属性ごとにプロパティを含むオブジェクトです。各プロパティ名は、Document
列挙型の項目に対応しています。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Append a styled paragraph. const par = body.appendParagraph('A bold, italicized paragraph.'); par.setBold(true); par.setItalic(true); // Retrieve the paragraph's attributes. const atts = par.getAttributes(); // Log the paragraph attributes. for (const att in atts) { Logger.log(`${att}:${atts[att]}`); }
戻る
Object
- 要素の属性。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChild(childIndex)
指定された子インデックスの子要素を取得します。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Obtain the first element in the tab. const firstChild = body.getChild(0); // If it's a paragraph, set its contents. if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) { firstChild.asParagraph().setText('This is the first paragraph.'); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
child | Integer | 取得する子要素のインデックス。 |
戻る
Element
- 指定されたインデックスの子要素。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChildIndex(child)
getHeadingAttributes(paragraphHeading)
指定された Paragraph
の属性セットを取得します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
paragraph | Paragraph | 属性を取得する見出し。 |
戻る
Object
- 属性とその現在の値のマップ。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getImages()
セクションに含まれるすべての Inline
を取得します。
戻る
Inline
- セクション画像。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getListItems()
セクションに含まれるすべての List
を取得します。
戻る
List
- セクション リストアイテム。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginBottom()
下部余白(ポイント単位)を取得します。
戻る
Number
- 下余白(ポイント単位)。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginLeft()
左側余白(ポイント単位)を取得します。
戻る
Number
- 左余白(ポイント単位)。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginRight()
右余白を取得します。
戻る
Number
- 右余白(ポイント単位)。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginTop()
上余白を取得します。
戻る
Number
- 上余白(ポイント単位)。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNumChildren()
子の数を取得します。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Log the number of elements in the tab. Logger.log(`There are ${body.getNumChildren()} elements in the tab's body.`);
戻る
Integer
- 子の数。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPageHeight()
ページの高さをポイント単位で取得します。
戻る
Number
- ページの高さ(ポイント単位)。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPageWidth()
ページの幅(ポイント単位)を取得します。
戻る
Number
- ページの幅(ポイント単位)。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParagraphs()
セクションに含まれるすべての Paragraphs
(List
を含む)を取得します。
戻る
Paragraph[]
- セクションの段落。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParent()
要素の親要素を取得します。
親要素には現在の要素が含まれています。
戻る
Container
- 親要素。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTables()
getText()
要素のコンテンツをテキスト文字列として取得します。
戻る
String
- 要素の内容(テキスト文字列)
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTextAlignment()
テキストの配置を取得します。使用できる配置のタイプは、Document
、Document
、Document
です。
戻る
Text
- テキストの配置のタイプ。テキストに複数のタイプのテキストの配置が含まれている場合、またはテキストの配置が設定されていない場合は null
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getType()
要素の Element
を取得します。
get
を使用して、特定の要素の正確な型を判断します。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Obtain the first element in the active tab's body. const firstChild = body.getChild(0); // Use getType() to determine the element's type. if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) { Logger.log('The first element is a paragraph.'); } else { Logger.log('The first element is not a paragraph.'); }
戻る
Element
- 要素のタイプ。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertHorizontalRule(childIndex)
指定したインデックスに新しい Horizontal
を作成して挿入します。
Horizontal
は新しい Paragraph
に含まれます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
child | Integer | 要素を挿入するインデックス。 |
戻る
Horizontal
- 新しい横線。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertImage(childIndex, image)
指定されたイメージ ブロブから Inline
を作成して、指定されたインデックスに挿入します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
child | Integer | 要素を挿入するインデックス。 |
image | Blob | 画像データ。 |
戻る
Inline
- 挿入されたインライン画像。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertImage(childIndex, image)
指定された Inline
を指定されたインデックスに挿入します。
イメージは新しい Paragraph
に格納されます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
child | Integer | 要素を挿入するインデックス。 |
image | Inline | 挿入する画像。 |
戻る
Inline
- 挿入されたインライン画像。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertListItem(childIndex, listItem)
insertListItem(childIndex, text)
insertPageBreak(childIndex)
insertPageBreak(childIndex, pageBreak)
指定された Page
を指定されたインデックスに挿入します。
Page
は新しい Paragraph
に含まれます。
パラメータ
名前 | 型 | 説明 |
---|---|---|
child | Integer | 要素を挿入するインデックス。 |
page | Page | 挿入する改ページ。 |
戻る
Page
- 挿入された改ページ。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertParagraph(childIndex, paragraph)
insertParagraph(childIndex, text)
insertTable(childIndex)
insertTable(childIndex, cells)
insertTable(childIndex, table)
removeChild(child)
replaceText(searchPattern, replacement)
正規表現を使用して、指定したテキスト パターンのすべての出現を指定された置換文字列に置き換えます。
検索パターンは、JavaScript 正規表現オブジェクトではなく、文字列として渡されます。そのため、パターン内のバックスラッシュをエスケープする必要があります。
このメソッドは Google の RE2 正規表現ライブラリを使用します。これにより、サポートされる構文が制限されます。
指定された正規表現パターンは、現在の要素に含まれる各テキスト ブロックに対して個別に照合されます。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText('^.*Apps ?Script.*$', 'Apps Script');
パラメータ
名前 | 型 | 説明 |
---|---|---|
search | String | 検索する正規表現パターン |
replacement | String | 置換として使用するテキスト |
戻る
Element
- 現在の要素
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setAttributes(attributes)
要素の属性を設定します。
指定された attributes パラメータは、各プロパティ名が Document
列挙型のアイテムで、各プロパティ値が適用される新しい値であるオブジェクトである必要があります。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Define a custom paragraph style. const style = {}; style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT; style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri'; style[DocumentApp.Attribute.FONT_SIZE] = 18; style[DocumentApp.Attribute.BOLD] = true; // Append a plain paragraph. const par = body.appendParagraph('A paragraph with custom style.'); // Apply the custom style. par.setAttributes(style);
パラメータ
名前 | 型 | 説明 |
---|---|---|
attributes | Object | 要素の属性。 |
戻る
Body
- 現在の要素。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setHeadingAttributes(paragraphHeading, attributes)
指定された Paragraph
の属性を設定します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
paragraph | Paragraph | 属性を設定する見出し。 |
attributes | Object | 属性と、それらに設定する値のマップ。 |
戻る
Body
- 現在の要素。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setMarginBottom(marginBottom)
setMarginLeft(marginLeft)
setMarginRight(marginRight)
setMarginTop(marginTop)
setPageHeight(pageHeight)
setPageWidth(pageWidth)
setText(text)
setTextAlignment(textAlignment)
テキストの配置を設定します。使用できる配置のタイプは、Document
、Document
、Document
です。
// Make the entire first paragraph in the active tab be superscript. const documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab(); const text = documentTab.getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
パラメータ
名前 | 型 | 説明 |
---|---|---|
text | Text | 適用するテキストの配置の種類 |
戻る
Body
- 現在の要素
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
サポート終了のメソッド
getFootnotes()
getFootnotes()
非推奨。この要素には Footnote
を含めることはできません。
セクションに含まれるすべての Footnotes
を取得します。
戻る
Footnote[]
- セクションの脚注。
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLinkUrl()
getLinkUrl()
非推奨。セクション全体にリンクを適用するのではなく、セクション内の要素にリンクを適用します。
リンクの URL を取得します。
戻る
String
- リンク URL。要素にこの属性の値が複数含まれている場合は null
承認
このメソッドを使用するスクリプトには、次のスコープの 1 つ以上による承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNextSibling()
getNextSibling()
getPreviousSibling()
getPreviousSibling()
isAtDocumentEnd()
isAtDocumentEnd()
setLinkUrl(url)
setLinkUrl(url)