Class Paragraph

段落

段落を表す要素。Paragraph には、EquationFootnoteHorizontalRuleInlineDrawingInlineImagePageBreak、 と Text 要素を使用します。ドキュメント構造について詳しくは、Google ドキュメントの拡張ガイドをご覧ください。

Paragraphs に改行文字を含めることはできません。改行文字(\n)は、 改行文字(「\r」)に変換されます。

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Append a tab header paragraph.
var header = body.appendParagraph("A Document");
header.setHeading(DocumentApp.ParagraphHeading.HEADING1);

// Append a section header paragraph.
var section = body.appendParagraph("Section 1");
section.setHeading(DocumentApp.ParagraphHeading.HEADING2);

// Append a regular paragraph.
body.appendParagraph("This is a typical paragraph.");

メソッド

メソッド戻り値の型概要
addPositionedImage(image)PositionedImage指定された画像 blob から新しい PositionedImage を作成して挿入します。
appendHorizontalRule()HorizontalRule新しい HorizontalRule を作成して追加します。
appendInlineImage(image)InlineImage指定された画像 blob から新しい InlineImage を作成して追加します。
appendInlineImage(image)InlineImage指定された InlineImage を追加します。
appendPageBreak()PageBreak新しい PageBreak を作成して追加します。
appendPageBreak(pageBreak)PageBreak指定された PageBreak を追加します。
appendText(text)Text指定されたコンテンツを含む新しい Text 要素を作成して追加します。
appendText(text)Text指定された Text 要素を追加します。
clear()Paragraph要素の内容をクリアします。
copy()Paragraph現在の要素の分離されたディープコピーを返します。
editAsText()Text編集用に、現在の要素の Text バージョンを取得します。
findElement(elementType)RangeElement要素のコンテンツで、指定された型の子孫を検索します。
findElement(elementType, from)RangeElement要素のコンテンツで、指定された型の子孫を (RangeElement が指定されている)。
findText(searchPattern)RangeElement正規表現を使用して、指定されたテキスト パターンの要素のコンテンツを検索します。
findText(searchPattern, from)RangeElement要素の内容の中から、指定したテキスト パターンから始まるものを検索します。 表示されます。
getAlignment()HorizontalAlignmentHorizontalAlignment を取得します。
getAttributes()Object要素の属性を取得します。
getChild(childIndex)Element指定された子インデックスの子要素を取得します。
getChildIndex(child)Integer指定された子要素の子インデックスを取得します。
getHeading()ParagraphHeadingParagraphHeading を取得します。
getIndentEnd()Number終了インデントをポイントで取得します。
getIndentFirstLine()Number最初の行のインデントをポイントで取得します。
getIndentStart()Number開始インデントを取得します。
getLineSpacing()Number行間隔をポイント単位で取得します。
getLinkUrl()Stringリンクの URL を取得します。
getNextSibling()Element要素の次の兄弟要素を取得します。
getNumChildren()Integer子の数を取得します。
getParent()ContainerElement要素の親要素を取得します。
getPositionedImage(id)PositionedImageイメージの ID によって PositionedImage を取得します。
getPositionedImages()PositionedImage[]段落に固定された PositionedImage オブジェクトをすべて取得します。
getPreviousSibling()Element要素の前の兄弟要素を取得します。
getSpacingAfter()Number要素の後の間隔をポイント単位で取得します。
getSpacingBefore()Number要素の前の間隔をポイント単位で取得します。
getText()String要素のコンテンツをテキスト文字列として取得します。
getTextAlignment()TextAlignmentテキストの配置を取得します。
getType()ElementType要素の ElementType を取得します。
insertHorizontalRule(childIndex)HorizontalRuleHorizontalRule を作成して、指定されたインデックスに挿入します。
insertInlineImage(childIndex, image)InlineImage指定された画像 blob から新しい InlineImage を作成して、指定された位置に挿入します。 あります。
insertInlineImage(childIndex, image)InlineImage指定された InlineImage を指定されたインデックスに挿入します。
insertPageBreak(childIndex)PageBreak新しい PageBreak を作成して指定されたインデックスに挿入します。
insertPageBreak(childIndex, pageBreak)PageBreak指定された PageBreak を指定されたインデックスに挿入します。
insertText(childIndex, text)Text新しいテキスト要素を作成して、指定されたインデックスに挿入します。
insertText(childIndex, text)Text指定された Text 要素を指定されたインデックスに、指定されたテキストとともに挿入します。 できます。
isAtDocumentEnd()Boolean要素が Document の最後にあるかどうかを決定します。
isLeftToRight()Boolean左から右の設定を取得します。
merge()Paragraph要素を、先行する同じ型の兄弟要素と結合します。
removeChild(child)Paragraph指定した子要素を削除します。
removeFromParent()Paragraph要素を親から削除します。
removePositionedImage(id)Boolean画像の ID で PositionedImage を削除します。
replaceText(searchPattern, replacement)Element通常の文字列を使用して、指定したテキスト パターンをすべて、指定した置換文字列に置き換えます。 表します。
setAlignment(alignment)ParagraphHorizontalAlignment を設定します。
setAttributes(attributes)Paragraph要素の属性を設定します。
setHeading(heading)ParagraphParagraphHeading を設定します。
setIndentEnd(indentEnd)Paragraph終了インデントをポイント単位で設定します。
setIndentFirstLine(indentFirstLine)Paragraph先頭行のインデントをポイント単位で設定します。
setIndentStart(indentStart)Paragraph開始インデントをポイント単位で設定します。
setLeftToRight(leftToRight)Paragraph左から右に設定します。
setLineSpacing(multiplier)Paragraph行間隔を、行間隔に使用する行数を示す数量で設定します。
setLinkUrl(url)Paragraphリンク URL を設定します。
setSpacingAfter(spacingAfter)Paragraph要素の後の間隔をポイント単位で設定します。
setSpacingBefore(spacingBefore)Paragraph要素の前の間隔をポイント単位で設定します。
setText(text)void段落の内容をテキストとして設定します。
setTextAlignment(textAlignment)Paragraphテキストの配置を設定します。

詳細なドキュメント

addPositionedImage(image)

指定された画像 blob から新しい PositionedImage を作成して挿入します。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Fetches the specified image URL.
const image =
  UrlFetchApp.fetch('https://fonts.gstatic.com/s/i/productlogos/apps_script/v10/web-24dp/logo_apps_script_color_1x_web_24dp.png');

// Adds the image to the tab, anchored to the first paragraph.
paragraph.addPositionedImage(image);

パラメータ

名前説明
imageBlobSource画像データ。

戻る

PositionedImage - 新しい配置された画像。

承認

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

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

appendHorizontalRule()

新しい HorizontalRule を作成して追加します。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Adds a horizontal line under the first paragraph.
paragraph.appendHorizontalRule();

戻る

HorizontalRule - 新しい横方向のルール。

承認

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

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

appendInlineImage(image)

指定された画像 blob から新しい InlineImage を作成して追加します。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Fetches the image from the specified image URL.
const image = UrlFetchApp
  .fetch('https://fonts.gstatic.com/s/i/productlogos/apps_script/v10/web-96dp/logo_apps_script_color_1x_web_96dp.png');

// Adds the image to the first paragraph.
paragraph.appendInlineImage(image);

パラメータ

名前説明
imageBlobSource画像データ。

戻る

InlineImage - 追加された画像。

承認

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

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

appendInlineImage(image)

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

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Makes a copy of the first image in the body.
const image = body.getImages()[0].copy();;

// Adds the image to the first paragraph.
paragraph.appendInlineImage(image);

パラメータ

名前説明
imageInlineImage画像データ。

戻る

InlineImage - 追加された画像。

承認

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

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

appendPageBreak()

新しい PageBreak を作成して追加します。

注: PageBreaksTableCells 内に含めることはできません。 現在の要素が表のセルに含まれている場合は、例外がスローされます。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Adds a page break after the first paragraph.
paragraph.appendPageBreak();

戻る

PageBreak - 新しい改ページ要素。

承認

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

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

appendPageBreak(pageBreak)

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

注: PageBreaksTableCells 内に含めることはできません。 現在の要素が表のセル内に存在する場合、スクリプトは例外をスローします。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Adds a page break after the first paragraph.
const pageBreak = paragraph.appendPageBreak();

// Makes a copy of the page break.
const newPageBreak = pageBreak.copy();

// Adds the copied page break to the paragraph.
paragraph.appendPageBreak(newPageBreak);

パラメータ

名前説明
pageBreakPageBreak追加する改ページ。

戻る

PageBreak - 追加された改ページ要素。

承認

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

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

appendText(text)

指定されたコンテンツを含む新しい Text 要素を作成して追加します。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Adds a string to the paragraph.
paragraph.appendText('This is a new sentence.');

パラメータ

名前説明
textStringテキストのコンテンツ。

戻る

Text - 新しいテキスト要素。

承認

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

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

appendText(text)

指定された Text 要素を追加します。

 // 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the text from the first paragraph in the body.
const paragraph1 = body.getParagraphs()[0];
const text = paragraph1.getText();

// Gets the third paragraph in the body.
const paragraph3 = body.getParagraphs()[2];

// Adds the text from the first paragraph to the third paragraph.
paragraph3.appendText(text);

パラメータ

名前説明
textText追加するテキスト要素。

戻る

Text - 追加されたテキスト要素。

承認

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

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

clear()

要素の内容をクリアします。

戻る

Paragraph - 現在の要素。


copy()

現在の要素の分離されたディープコピーを返します。

その要素に含まれている子要素もすべてコピーされます。新しい要素には できます。

戻る

Paragraph - 新しいコピー。

承認

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

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

editAsText()

編集用に、現在の要素の Text バージョンを取得します。

editAsText を使用して、要素のコンテンツをリッチテキストとして操作します。editAsText モードでは、テキスト以外の要素(InlineImageHorizontalRule など)は無視されます。

削除されたテキスト範囲内に完全に含まれている子要素は要素から削除されます。

var 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)

要素のコンテンツで、指定された型の子孫を検索します。

パラメータ

名前説明
elementTypeElementType検索する要素のタイプ。

戻る

RangeElement - 検索要素の位置を示す検索結果。

承認

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

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

findElement(elementType, from)

要素のコンテンツで、指定された型の子孫を (RangeElement が指定されている)。

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().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;
  }
}

パラメータ

名前説明
elementTypeElementType検索する要素のタイプ。
fromRangeElement検索元の検索結果。

戻る

RangeElement - 検索要素の次の位置を示す検索結果。

承認

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

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

findText(searchPattern)

正規表現を使用して、指定されたテキスト パターンの要素のコンテンツを検索します。

以下の JavaScript 正規表現機能の一部は、完全にはサポートされていません。 キャプチャ グループとモード修飾子を使用します。

指定した正規表現のパターンが、テキスト ブロックごとに個別に照合される 返されます。

パラメータ

名前説明
searchPatternString検索するパターン

戻る

RangeElement - 検索テキストの位置を示す検索結果。テキストがない場合は null マッチ

承認

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

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

findText(searchPattern, from)

要素の内容の中から、指定したテキスト パターンから始まるものを検索します。 表示されます。

以下の JavaScript 正規表現機能の一部は、完全にはサポートされていません。 キャプチャ グループとモード修飾子を使用します。

指定した正規表現のパターンが、テキスト ブロックごとに個別に照合される 返されます。

パラメータ

名前説明
searchPatternString検索するパターン
fromRangeElement検索元の検索結果

戻る

RangeElement - 検索テキストの次の位置を示す検索結果。それ以外の場合は null マッチ

承認

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

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

getAlignment()

HorizontalAlignment を取得します。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Gets the first paragraph from the body.
const paragraph = body.getParagraphs()[0];

// Sets the horizontal alignment to left for the first paragraph.
paragraph.setAlignment(DocumentApp.HorizontalAlignment.LEFT);

// Gets the horizontal alignment of the first paragraph and logs it to the console.
console.log(paragraph.getAlignment().toString());

戻る

HorizontalAlignment - アライメント。

承認

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

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

getAttributes()

要素の属性を取得します。

その結果は、各有効な要素属性のプロパティを含むオブジェクトとなり、 プロパティ名は、DocumentApp.Attribute 列挙型の項目に対応しています。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.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

getChild(childIndex)

指定された子インデックスの子要素を取得します。

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Obtain the first element in the tab.
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.");
}

パラメータ

名前説明
childIndexInteger取得する子要素のインデックス。

戻る

Element - 指定されたインデックスの子要素。

承認

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

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

getChildIndex(child)

指定された子要素の子インデックスを取得します。

パラメータ

名前説明
childElementインデックスを取得する子要素。

戻る

Integer - 子インデックス。

承認

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

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

getHeading()

ParagraphHeading を取得します。

// 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(DOCUMENT_ID);

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab(TAB_ID).asDocumentTab().getBody();

// Adds a paragraph to the body.
const paragraph = body.appendParagraph('Title heading');

// Sets the paragraph heading style to 'Title.'
paragraph.setHeading(DocumentApp.ParagraphHeading.TITLE);

// Gets the heading style and logs it to the console.
console.log(paragraph.getHeading().toString());

戻る

ParagraphHeading - 見出し。

承認

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

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

getIndentEnd()

終了インデントをポイントで取得します。

戻る

Number - 終了インデント(ポイント単位)

承認

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

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

getIndentFirstLine()

最初の行のインデントをポイントで取得します。

戻る

Number - 最初の行のインデント(ポイント単位)

承認

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

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

getIndentStart()

開始インデントを取得します。

戻る

Number - 開始インデント

承認

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

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

getLineSpacing()

行間隔をポイント単位で取得します。

戻る

Number - 行間隔(ポイント単位)

承認

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

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

getLinkUrl()

リンクの URL を取得します。

戻る

String - リンク URL、または要素にこの属性に複数の値が含まれている場合は null

承認

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

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

getNextSibling()

要素の次の兄弟要素を取得します。

次の兄弟要素は同じ親を持ち、現在の要素に従います。

戻る

Element - 次の兄弟要素。

承認

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

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

getNumChildren()

子の数を取得します。

var 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

getParent()

要素の親要素を取得します。

親要素には現在の要素が含まれます。

戻る

ContainerElement - 親要素。

承認

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

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

getPositionedImage(id)

イメージの ID によって PositionedImage を取得します。

パラメータ

名前説明
idStringイメージ ID

戻る

PositionedImage - 配置された画像

承認

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

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

getPositionedImages()

段落に固定された PositionedImage オブジェクトをすべて取得します。

戻る

PositionedImage[] - 配置された画像のリスト

承認

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

getSpacingAfter()

要素の後の間隔をポイント単位で取得します。

戻る

Number - 要素の後の間隔(ポイント単位)

承認

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

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

getSpacingBefore()

要素の前の間隔をポイント単位で取得します。

戻る

Number - 要素の前の間隔(ポイント単位)

承認

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

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

getText()

要素のコンテンツをテキスト文字列として取得します。

戻る

String - テキスト文字列としての要素の内容

承認

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

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

getTextAlignment()

テキストの配置を取得します。使用できるアライメントのタイプは、DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT です。

戻る

TextAlignment - テキストの配置のタイプ。テキストに複数のタイプのテキストが含まれている場合は null またはテキストの配置が設定されていない場合は、

承認

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

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

getType()

要素の ElementType を取得します。

getType() を使用して、指定された要素の正確な型を特定します。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Obtain the first element in the active tab's 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

insertHorizontalRule(childIndex)

HorizontalRule を作成して、指定されたインデックスに挿入します。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス

戻る

HorizontalRule - 新しい水平ルール要素

承認

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

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

insertInlineImage(childIndex, image)

指定された画像 blob から新しい InlineImage を作成して、指定された位置に挿入します。 あります。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス
imageBlobSource画像データ

戻る

InlineImage - 挿入されるインライン画像要素

承認

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

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

insertInlineImage(childIndex, image)

指定された InlineImage を指定されたインデックスに挿入します。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス
imageInlineImage画像データ

戻る

InlineImage - 挿入されるインライン画像要素

承認

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

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

insertPageBreak(childIndex)

新しい PageBreak を作成して指定されたインデックスに挿入します。

注: PageBreaksTableCells 内に含めることはできません。 現在の要素が表のセルに含まれている場合は、例外がスローされます。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス

戻る

PageBreak - 新しい改ページ要素

承認

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

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

insertPageBreak(childIndex, pageBreak)

指定された PageBreak を指定されたインデックスに挿入します。

注: PageBreaksTableCells 内に含めることはできません。 現在の要素が表のセルに含まれている場合は、例外がスローされます。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス
pageBreakPageBreak挿入するページ ブレーク

戻る

PageBreak - 挿入された改ページ要素

承認

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

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

insertText(childIndex, text)

新しいテキスト要素を作成して、指定されたインデックスに挿入します。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス
textStringテキスト コンテンツ

戻る

Text - 新しいテキスト要素

承認

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

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

insertText(childIndex, text)

指定された Text 要素を指定されたインデックスに、指定されたテキストとともに挿入します。 できます。

パラメータ

名前説明
childIndexInteger要素を挿入するインデックス
textText挿入するテキスト要素

戻る

Text - 挿入されたテキスト要素

承認

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

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

isAtDocumentEnd()

要素が Document の最後にあるかどうかを決定します。

戻る

Boolean - 要素がタブの最後にあるかどうか。

承認

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

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

isLeftToRight()

左から右の設定を取得します。

戻る

Boolean - 左から右の設定

承認

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

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

merge()

要素を、先行する同じ型の兄弟要素と結合します。

結合できるのは同じ ElementType の要素のみです。Deployment に含まれる子要素は、 現在の要素は前の兄弟要素に移動されます。

現在の要素がドキュメントから削除されます。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
var par1 = body.appendParagraph('Paragraph 1.');
var par2 = body.appendParagraph('Paragraph 2.');
// Merge the newly added paragraphs into a single paragraph.
par2.merge();

// Example 2: Merge table cells
// Create a two-dimensional array containing the table's 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.
var table = body.appendTable(cells);
// Get the first row in the table.
 var row = table.getRow(0);
// Get the two cells in this row.
var cell1 = row.getCell(0);
var cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
var merged = cell2.merge();

戻る

Paragraph - マージされた要素。

承認

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

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

removeChild(child)

指定した子要素を削除します。

パラメータ

名前説明
childElement削除する子要素です。

戻る

Paragraph - 現在の要素

承認

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

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

removeFromParent()

要素を親から削除します。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab()
var body = documentTab.getBody();

// Remove all images in the active tab's body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
  imgs[i].removeFromParent();
}

戻る

Paragraph - 削除された要素。

承認

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

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

removePositionedImage(id)

画像の ID で PositionedImage を削除します。

パラメータ

名前説明
idStringイメージ ID

戻る

Boolean - 指定した画像が削除されたかどうか

承認

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

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

replaceText(searchPattern, replacement)

通常の文字列を使用して、指定したテキスト パターンをすべて、指定した置換文字列に置き換えます。 表します。

検索パターンは、JavaScript 正規表現オブジェクトではなく、文字列として渡されます。 そのため、パターン内でバックスラッシュをエスケープする必要があります。

このメソッドは、Google の RE2 標準版を使用します。 式ライブラリを使用して、サポートされている構文を制限します。

指定した正規表現のパターンが、テキスト ブロックごとに個別に照合される 返されます。

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Clear the text surrounding "Apps Script", with or without text.
body.replaceText("^.*Apps ?Script.*$", "Apps Script");

パラメータ

名前説明
searchPatternString検索する正規表現パターン
replacementString置換として使用するテキスト

戻る

Element - 現在の要素

承認

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

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

setAlignment(alignment)

HorizontalAlignment を設定します。

パラメータ

名前説明
alignmentHorizontalAlignment水平方向の配置

戻る

Paragraph - 現在の要素

承認

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

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

setAttributes(attributes)

要素の属性を設定します。

指定する属性パラメータは、各プロパティ名がアイテムのアイテムである DocumentApp.Attribute 列挙値。各プロパティ値は新しい値になります。 適用されました。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.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);

パラメータ

名前説明
attributesObject要素の属性。

戻る

Paragraph - 現在の要素。

承認

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

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

setHeading(heading)

ParagraphHeading を設定します。

パラメータ

名前説明
headingParagraphHeading見出し

戻る

Paragraph - 現在の要素

承認

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

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

setIndentEnd(indentEnd)

終了インデントをポイント単位で設定します。

パラメータ

名前説明
indentEndNumber終了インデント(ポイント単位)

戻る

Paragraph - 現在の要素

承認

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

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

setIndentFirstLine(indentFirstLine)

先頭行のインデントをポイント単位で設定します。

パラメータ

名前説明
indentFirstLineNumber先頭行のインデント(ポイント単位)

戻る

Paragraph - 現在の要素

承認

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

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

setIndentStart(indentStart)

開始インデントをポイント単位で設定します。

パラメータ

名前説明
indentStartNumber開始インデント(ポイント単位)

戻る

Paragraph - 現在の要素

承認

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

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

setLeftToRight(leftToRight)

左から右に設定します。

パラメータ

名前説明
leftToRightBoolean左から右の設定

戻る

Paragraph - 現在の要素

承認

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

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

setLineSpacing(multiplier)

行間隔を、行間隔に使用する行数を示す数量で設定します。

パラメータ

名前説明
multiplierNumber行数

戻る

Paragraph - 現在の要素

承認

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

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

setLinkUrl(url)

リンク URL を設定します。

パラメータ

名前説明
urlStringリンクの URL

戻る

Paragraph - 現在の要素

承認

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

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

setSpacingAfter(spacingAfter)

要素の後の間隔をポイント単位で設定します。

パラメータ

名前説明
spacingAfterNumber要素の後の間隔(ポイント単位)

戻る

Paragraph - 現在の要素

承認

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

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

setSpacingBefore(spacingBefore)

要素の前の間隔をポイント単位で設定します。

パラメータ

名前説明
spacingBeforeNumber要素の前の間隔(ポイント単位)

戻る

Paragraph - 現在の要素

承認

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

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

setText(text)

段落の内容をテキストとして設定します。

注: 既存の内容は消去されます。

パラメータ

名前説明
textString新しいテキスト コンテンツを

承認

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

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

setTextAlignment(textAlignment)

テキストの配置を設定します。使用できるアライメントのタイプは、DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT です。

// Make the entire first paragraph in the active tab be superscript.
var documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
var text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

パラメータ

名前説明
textAlignmentTextAlignment適用するテキスト配置のタイプ

戻る

Paragraph - 現在の要素

承認

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

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