テーブルを表す要素。Table
には TableRow
要素のみを含めることができます。ドキュメント構造の詳細については、Google ドキュメントの拡張ガイドをご覧ください。
多数の行またはセルを含む Table
を作成する場合は、次の例に示すように、文字列配列から作成することを検討してください。
var body = DocumentApp.getActiveDocument().getBody(); // Create a two-dimensional array containing the cell contents. var cells = [ ['Row 1, Cell 1', 'Row 1, Cell 2'], ['Row 2, Cell 1', 'Row 2, Cell 2'] ]; // Build a table from the array. body.appendTable(cells);
Methods
詳細なドキュメント
appendTableRow()
appendTableRow(tableRow)
指定された TableRow
を追加します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table in the document and copies the second row. const table = body.getTables()[0]; const row = table.getChild(1).copy(); // Adds the copied row to the bottom of the table. const tableRow = table.appendTableRow(row);
パラメータ
Name | 型 | 説明 |
---|---|---|
tableRow | TableRow | 追加するテーブルの行。 |
復路
TableRow
- 追加されるテーブルの行要素です。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
clear()
copy()
現在の要素のデタッチされたディープコピーを返します。
要素内の子要素もすべてコピーされます。新しい要素に親は設定されていません。
復路
Table
- 新しいコピー。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
editAsText()
編集するために、現在の要素の Text
バージョンを取得します。
要素のコンテンツをリッチテキストとして操作するには、editAsText
を使用します。editAsText
モードでは、テキスト以外の要素(InlineImage
や HorizontalRule
など)は無視されます。
削除されたテキスト範囲内に完全に含まれる子要素は、要素から削除されます。
var body = DocumentApp.getActiveDocument().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)
要素のコンテンツで指定されたタイプの子孫を検索します。
パラメータ
Name | 型 | 説明 |
---|---|---|
elementType | ElementType | 検索する要素のタイプ |
復路
RangeElement
- 検索要素の位置を示す検索結果
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findElement(elementType, from)
指定されたコンテンツ(指定されたタイプの子孫)の要素を、指定された RangeElement
から検索します。
// Get the body section of the active document. var body = DocumentApp.getActiveDocument().getBody(); // Define the search parameters. var searchType = DocumentApp.ElementType.PARAGRAPH; var searchHeading = DocumentApp.ParagraphHeading.HEADING1; var searchResult = null; // Search until the paragraph is found. while (searchResult = body.findElement(searchType, searchResult)) { var par = searchResult.getElement().asParagraph(); if (par.getHeading() == searchHeading) { // Found one, update and stop. par.setText('This is the first header.'); return; } }
パラメータ
Name | 型 | 説明 |
---|---|---|
elementType | ElementType | 検索する要素のタイプ |
from | RangeElement | 検索元の検索結果 |
復路
RangeElement
- 検索要素の次の位置を示す検索結果
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern)
正規表現を使用して、要素のコンテンツで指定されたテキスト パターンを検索します。
JavaScript の正規表現機能のサブセット(キャプチャ グループやモード修飾子など)は、完全にはサポートされていません。
指定された正規表現パターンは、現在の要素に含まれるテキスト ブロックごとに独立して照合されます。
パラメータ
Name | 型 | 説明 |
---|---|---|
searchPattern | String | 検索するパターンを指定します |
復路
RangeElement
- 検索テキストの位置を示す検索結果。一致がない場合は null 。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
特定のコンテンツから、指定されたテキスト パターンの要素のコンテンツを検索します。
JavaScript の正規表現機能のサブセット(キャプチャ グループやモード修飾子など)は、完全にはサポートされていません。
指定された正規表現パターンは、現在の要素に含まれるテキスト ブロックごとに独立して照合されます。
パラメータ
Name | 型 | 説明 |
---|---|---|
searchPattern | String | 検索するパターンを指定します |
from | RangeElement | 検索元の検索結果 |
復路
RangeElement
- 検索テキストの次の位置を示す検索結果。一致がない場合は null 。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getAttributes()
要素の属性を取得します。
その結果、各要素名が DocumentApp.Attribute
列挙型のアイテムに対応する、有効な要素属性ごとにプロパティを含むオブジェクトが作成されます。
var body = DocumentApp.getActiveDocument().getBody(); // Append a styled paragraph. var par = body.appendParagraph('A bold, italicized paragraph.'); par.setBold(true); par.setItalic(true); // Retrieve the paragraph's attributes. var atts = par.getAttributes(); // Log the paragraph attributes. for (var att in atts) { Logger.log(att + ":" + atts[att]); }
復路
Object
- 要素の属性。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getBorderColor()
枠線の色を取得します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Sets the border color of the first table. table.setBorderColor('#00FF00'); // Logs the border color of the first table to the console. console.log(table.getBorderColor());
復路
String
- CSS 表記('#ffffff'
など)で設定された枠線の色。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getBorderWidth()
枠線の太さをポイント数で取得します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Sets the border width of the first table. table.setBorderWidth(20); // Logs the border width of the first table. console.log(table.getBorderWidth());
復路
Number
- 枠線の幅(ポイント単位)。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getCell(rowIndex, cellIndex)
指定された行とセル インデックスの TableCell
を取得します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Gets the cell of the table's third row and second column. const cell = table.getCell(2, 1); // Logs the cell text to the console. console.log(cell.getText());
パラメータ
Name | 型 | 説明 |
---|---|---|
rowIndex | Integer | 取得するセルを含む行のインデックス。 |
cellIndex | Integer | 取得するセルのインデックス。 |
復路
TableCell
- テーブルのセル。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChild(childIndex)
指定された子インデックスの子要素を取得します。
// Get the body section of the active document. var body = DocumentApp.getActiveDocument().getBody(); // Obtain the first element in the document. var 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."); }
パラメータ
Name | 型 | 説明 |
---|---|---|
childIndex | Integer | 取得する子要素のインデックス |
復路
Element
- 指定したインデックスの子要素
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChildIndex(child)
指定された子要素の子インデックスを取得します。
パラメータ
Name | 型 | 説明 |
---|---|---|
child | Element | インデックスを取得する子要素 |
復路
Integer
- 子インデックス
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getColumnWidth(columnIndex)
指定したテーブル列の幅をポイント数で取得します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Sets the width of the second column to 100 points. const columnWidth = table.setColumnWidth(1, 100); // Gets the width of the second column and logs it to the console. console.log(columnWidth.getColumnWidth(1));
パラメータ
Name | 型 | 説明 |
---|---|---|
columnIndex | Integer | 列インデックス。 |
復路
Number
- 列の幅(ポイント)。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLinkUrl()
getNextSibling()
要素の次の兄弟要素を取得します。
次の兄弟は同じ親を持ち、現在の要素に従います。
復路
Element
- 次の兄弟要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNumChildren()
子の数を取得します。
// Get the body section of the active document. var body = DocumentApp.getActiveDocument().getBody(); // Log the number of elements in the document. Logger.log("There are " + body.getNumChildren() + " elements in the document body.");
復路
Integer
- 子の数
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNumRows()
TableRows
の数を取得します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Logs the number of rows of the first table to the console. console.log(table.getNumRows());
復路
Integer
- テーブルの行数。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParent()
要素の親要素を取得します。
親要素には現在の要素が含まれます。
復路
ContainerElement
- 親要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPreviousSibling()
要素の前の兄弟要素を取得します。
前の兄弟は同じ親を持ち、現在の要素の前にあります。
復路
Element
- 前の兄弟要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getRow(rowIndex)
指定された行インデックスの TableRow
を取得します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table and logs the text of first row to the console. const table = body.getTables()[0]; console.log(table.getRow(0).getText());
パラメータ
Name | 型 | 説明 |
---|---|---|
rowIndex | Integer | 取得する行のインデックス。 |
復路
TableRow
- テーブルの行。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getText()
getTextAlignment()
テキストの配置を取得します。配置に使用できるタイプは DocumentApp.TextAlignment.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
、DocumentApp.TextAlignment.SUPERSCRIPT
です。
復路
TextAlignment
- テキストの配置タイプ。テキストに複数の種類のテキストの配置が含まれている場合や、テキストの配置が一度も設定されていない場合は null
です。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getType()
要素の ElementType
を取得します。
getType()
を使用して、特定の要素の正確な型を判別します。
var body = DocumentApp.getActiveDocument().getBody(); // Obtain the first element in the document body. var 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.'); }
復路
ElementType
- 要素タイプ。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertTableRow(childIndex)
insertTableRow(childIndex, tableRow)
isAtDocumentEnd()
要素が Document
の最後にあるかどうかを判断します。
復路
Boolean
- 要素がドキュメントの末尾にあるかどうか。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeChild(child)
指定した子要素を削除します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Finds the first table row and removes it. const element = table.findElement(DocumentApp.ElementType.TABLE_ROW); table.removeChild(element.getElement());
パラメータ
Name | 型 | 説明 |
---|---|---|
child | Element | 削除する子要素。 |
復路
Table
- 現在の要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeFromParent()
親から要素を削除します。
var body = DocumentApp.getActiveDocument().getBody(); // Remove all images in the document body. var imgs = body.getImages(); for (var i = 0; i < imgs.length; i++) { imgs[i].removeFromParent(); }
復路
Table
- 削除された要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeRow(rowIndex)
指定された行インデックスの TableRow
を削除します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table and removes its second row. const table = body.getTables()[0]; table.removeRow(1);
パラメータ
Name | 型 | 説明 |
---|---|---|
rowIndex | Integer | 削除する行のインデックス。 |
復路
TableRow
- 削除された行。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
replaceText(searchPattern, replacement)
正規表現を使用して、指定されたテキスト パターンのすべての置換を特定の置換文字列に置き換えます。
検索パターンは JavaScript 正規表現オブジェクトではなく文字列として渡されます。 このため、パターン内のすべてのバックスラッシュをエスケープする必要があります。
このメソッドは、Google の RE2 正規表現ライブラリを使用します。このライブラリは、サポートされる構文を制限します。
指定された正規表現パターンは、現在の要素に含まれるテキスト ブロックごとに独立して照合されます。
var body = DocumentApp.getActiveDocument().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText("^.*Apps ?Script.*$", "Apps Script");
パラメータ
Name | 型 | 説明 |
---|---|---|
searchPattern | String | 検索する正規表現パターン |
replacement | String | 置換として使用するテキスト |
復路
Element
- 現在の要素
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setAttributes(attributes)
要素の属性を設定します。
指定する属性パラメータは、各プロパティ名が DocumentApp.Attribute
列挙型内のアイテムであり、各プロパティ値が適用する新しい値であるオブジェクトである必要があります。
var body = DocumentApp.getActiveDocument().getBody(); // Define a custom paragraph style. var 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. var par = body.appendParagraph('A paragraph with custom style.'); // Apply the custom style. par.setAttributes(style);
パラメータ
Name | 型 | 説明 |
---|---|---|
attributes | Object | 要素の属性。 |
復路
Table
- 現在の要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setBorderColor(color)
枠線の色を設定します。
// Opens the Docs file by its ID. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first table. const table = body.getTables()[0]; // Sets the border color of the table to green. table.setBorderColor('#00FF00');
パラメータ
Name | 型 | 説明 |
---|---|---|
color | String | 枠線の色。CSS 表記('#ffffff' など)で書式設定されます。 |
復路
Table
- 現在の要素。
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setBorderWidth(width)
枠線の太さをポイント単位で設定します。
パラメータ
Name | 型 | 説明 |
---|---|---|
width | Number | 枠線の太さ(ポイント) |
復路
Table
- 現在の要素
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setColumnWidth(columnIndex, width)
指定した列の幅をポイント単位で設定します。
パラメータ
Name | 型 | 説明 |
---|---|---|
columnIndex | Integer | 列インデックス |
width | Number | 枠線の太さ(ポイント) |
復路
Table
- 現在の要素
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setLinkUrl(url)
リンク URL を設定します。
パラメータ
Name | 型 | 説明 |
---|---|---|
url | String | リンク URL |
復路
Table
- 現在の要素
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setTextAlignment(textAlignment)
テキストの配置を設定します。配置に使用できるタイプは DocumentApp.TextAlignment.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
、DocumentApp.TextAlignment.SUPERSCRIPT
です。
// Make the entire first paragraph be superscript. var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
パラメータ
Name | 型 | 説明 |
---|---|---|
textAlignment | TextAlignment | 適用するテキストの配置タイプ |
復路
Table
- 現在の要素
認証
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents