Class Paragraph

段落

代表段落的元素。Paragraph 可能包含 EquationFootnoteHorizontalRuleInlineDrawingInlineImagePageBreakText 元素。如要進一步瞭解文件結構,請參閱擴充 Google 文件指南

Paragraphs 不得包含換行字元。換行字元 ("\n") 會轉換為換行符號字元 ("\r")。

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

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

// Append a section header paragraph.
const 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|null搜尋元素內容,找出指定型別的後代。
findElement(elementType, from)RangeElement|null從指定 RangeElement 開始,在元素內容中搜尋指定型別的後代。
findText(searchPattern)RangeElement|null使用規則運算式在元素內容中搜尋指定的文字模式。
findText(searchPattern, from)RangeElement|null從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。
getAlignment()HorizontalAlignment|null擷取 HorizontalAlignment
getAttributes()Object擷取元素的屬性。
getChild(childIndex)Element擷取指定子項索引處的子項元素。
getChildIndex(child)Integer擷取指定子項元素的子項索引。
getHeading()ParagraphHeading|null擷取 ParagraphHeading
getIndentEnd()Number|null擷取結尾縮排 (以點為單位)。
getIndentFirstLine()Number|null以點為單位擷取首行縮排。
getIndentStart()Number|null擷取起始縮排。
getLineSpacing()Number|null以點為單位擷取行距。
getLinkUrl()String|null擷取連結網址。
getNextSibling()Element|null擷取元素的下一個同層級元素。
getNumChildren()Integer擷取子項數量。
getParent()ContainerElement|null擷取元素的父項元素。
getPositionedImage(id)PositionedImage依圖片 ID 取得 PositionedImage
getPositionedImages()PositionedImage[]取得錨定至段落的所有 PositionedImage 物件。
getPreviousSibling()Element|null擷取元素的前一個同層級元素。
getSpacingAfter()Number|null以點為單位擷取元素後的間距。
getSpacingBefore()Number|null以點為單位,擷取元素前的間距。
getText()String以文字字串形式擷取元素內容。
getTextAlignment()TextAlignment|null取得文字對齊方式。
getType()ElementType擷取元素的 ElementType
insertHorizontalRule(childIndex)HorizontalRule在指定索引處建立及插入 HorizontalRule
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|null擷取由左至右設定。
merge()Paragraph|null將元素與相同類型的前一個同層級元素合併。
removeChild(child)Paragraph移除指定的子項元素。
removeFromParent()Paragraph|null從父項移除元素。
removePositionedImage(id)Boolean根據圖片 ID 移除 PositionedImage
replaceText(searchPattern, replacement)Element使用規則運算式,將指定文字模式的所有例項替換為指定替代字串。
setAlignment(alignment)Paragraph可設定 HorizontalAlignment
setAttributes(attributes)Paragraph設定元素的屬性。
setHeading(heading)Paragraph可設定 ParagraphHeading
setIndentEnd(indentEnd)Paragraph設定結尾縮排,以點為單位。
setIndentFirstLine(indentFirstLine)Paragraph以點為單位設定首行縮排。
setIndentStart(indentStart)Paragraph設定起始縮排,以點為單位。
setLeftToRight(leftToRight)Paragraph設定從左到右的文字方向。
setLineSpacing(multiplier)Paragraph設定行距,以數量表示用於間隔的行數。
setLinkUrl(url)Paragraph設定連結網址。
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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:新位置的圖片。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:新的水平線。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:附加的圖片。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:附加的圖片。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendPageBreak()

建立並附加新的 PageBreak

注意:PageBreaks 可能不包含在 TableCells 中。 如果目前的元素包含在表格儲存格中,系統會擲回例外狀況。

// 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:新的分頁符號元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendPageBreak(pageBreak)

附加指定的 PageBreak

注意:PageBreaks 無法包含在 TableCells 中。 如果目前元素存在於表格儲存格中,指令碼會擲回例外狀況。

// 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:附加的分頁符號元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:新的文字元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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:附加的文字元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

clear()

清除元素內容。

回攻員

Paragraph:目前的元素。


copy()

傳回目前元素的深層副本,且該副本已分離。

元素中的任何子項元素也會一併複製。新元素沒有父項。

回攻員

Paragraph:新副本。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

editAsText()

取得目前元素的 Text 版本,以供編輯。

使用 editAsText 將元素內容當做 RTF 文字來操控。editAsText 模式會忽略非文字元素 (例如 InlineImageHorizontalRule)。

如果子項元素完全包含在已刪除的文字範圍內,系統會從元素中移除這些子項元素。

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)

搜尋元素內容,找出指定型別的後代。

參數

名稱類型說明
elementTypeElementType要搜尋的元素類型。

回攻員

RangeElement|null:搜尋結果,指出搜尋元素的所在位置。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

findElement(elementType, from)

從指定的 RangeElement 開始,在元素內容中搜尋指定型別的後代。

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;
  }
}

參數

名稱類型說明
elementTypeElementType要搜尋的元素類型。
fromRangeElement要從中搜尋的搜尋結果。

回攻員

RangeElement|null:搜尋結果,指出搜尋元素的下一個位置。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

findText(searchPattern)

使用規則運算式在元素內容中搜尋指定的文字模式。

系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。

參數

名稱類型說明
searchPatternString要搜尋的模式

回攻員

RangeElement|null:搜尋結果,指出搜尋文字的位置;如果沒有相符項目,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

findText(searchPattern, from)

從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。

系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。

參數

名稱類型說明
searchPatternString要搜尋的模式
fromRangeElement要從哪個搜尋結果開始搜尋

回攻員

RangeElement|null:搜尋結果,指出搜尋文字的下一個位置;如果沒有相符項目,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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|null:對齊方式。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getAttributes()

擷取元素的屬性。

結果會是物件,其中包含每個有效元素屬性的屬性,且每個屬性名稱都對應至 DocumentApp.Attribute 列舉中的項目。

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:元素的屬性。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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.');
}

參數

名稱類型說明
childIndexInteger要擷取的子項元素索引。

回攻員

Element:指定索引的子項元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getChildIndex(child)

擷取指定子項元素的子項索引。

參數

名稱類型說明
childElement要擷取索引的子項元素。

回攻員

Integer - 子項索引。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').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|null:標題。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getIndentEnd()

擷取結尾縮排 (以點為單位)。

回攻員

Number|null - 結尾縮排,以點為單位

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getIndentFirstLine()

以點為單位擷取首行縮排。

回攻員

Number|null:首行縮排,以點為單位

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getIndentStart()

擷取起始縮排。

回攻員

Number|null - 起始縮排

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getLineSpacing()

以點為單位擷取行距。

回攻員

Number|null:行距 (以點為單位)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getLinkUrl()

擷取連結網址。

回攻員

String|null - 連結網址,如果元素包含這項屬性的多個值,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getNextSibling()

擷取元素的下一個同層級元素。

下一個同層級元素與目前元素具有相同的父項,且位於目前元素之後。

回攻員

Element|null:下一個同層級元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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:子項數量。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getParent()

擷取元素的父項元素。

父項元素包含目前的元素。

回攻員

ContainerElement|null:父項元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getPositionedImage(id)

依圖片 ID 取得 PositionedImage

參數

名稱類型說明
idString圖片 ID

回攻員

PositionedImage:已放置的圖片

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getPositionedImages()

取得錨定至段落的所有 PositionedImage 物件。

回攻員

PositionedImage[]:已定位的圖片清單

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getPreviousSibling()

擷取元素的前一個同層級元素。

前一個同層級元素與目前元素具有相同的父項,且位於目前元素之前。

回攻員

Element|null:上一個同層級元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getSpacingAfter()

以點為單位擷取元素後的間距。

回攻員

Number|null:元素後方的間距 (以點為單位)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getSpacingBefore()

以點為單位,擷取元素前的間距。

回攻員

Number|null:元素前的間距 (以點為單位)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getText()

以文字字串形式擷取元素內容。

回攻員

String:元素內容 (文字字串)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getTextAlignment()

取得文字對齊方式。可用的對齊類型包括 DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT

回攻員

TextAlignment|null:文字對齊類型,或 null (如果文字包含多種文字對齊類型,或從未設定文字對齊方式)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getType()

擷取元素的 ElementType

使用 getType() 判斷指定元素的確切類型。

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.');
}

回攻員

ElementType:元素類型。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertHorizontalRule(childIndex)

在指定索引處建立及插入 HorizontalRule

參數

名稱類型說明
childIndexInteger要插入元素的索引

回攻員

HorizontalRule:新的水平線元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertInlineImage(childIndex, image)

從指定的圖片 Blob 建立並插入新的 InlineImage,插入位置為指定的索引。

參數

名稱類型說明
childIndexInteger要插入元素的索引
imageBlobSource圖片資料

回攻員

InlineImage:插入的內嵌圖片元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertInlineImage(childIndex, image)

在指定索引處插入指定 InlineImage

參數

名稱類型說明
childIndexInteger要插入元素的索引
imageInlineImage圖片資料

回攻員

InlineImage:插入的內嵌圖片元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertPageBreak(childIndex)

在指定索引建立並插入新的 PageBreak

注意:PageBreaks 可能不包含在 TableCells 中。 如果目前的元素包含在表格儲存格中,系統會擲回例外狀況。

參數

名稱類型說明
childIndexInteger要插入元素的索引

回攻員

PageBreak:新的分頁符號元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertPageBreak(childIndex, pageBreak)

在指定索引處插入指定 PageBreak

注意:PageBreaks 可能不包含在 TableCells 中。 如果目前的元素包含在表格儲存格中,系統會擲回例外狀況。

參數

名稱類型說明
childIndexInteger要插入元素的索引
pageBreakPageBreak要插入的分頁符號

回攻員

PageBreak:插入的分頁符號元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertText(childIndex, text)

在指定索引處建立並插入新的文字元素。

參數

名稱類型說明
childIndexInteger要插入元素的索引
textString文字內容

回攻員

Text:新的文字元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertText(childIndex, text)

在指定索引處插入指定 Text 元素,並加入指定文字內容。

參數

名稱類型說明
childIndexInteger要插入元素的索引
textText要插入的文字元素

回攻員

Text:插入的文字元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

isAtDocumentEnd()

判斷元素是否位於 Document 的結尾。

回攻員

Boolean:元素是否位於分頁的結尾。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

isLeftToRight()

擷取由左至右設定。

回攻員

Boolean|null:從左到右設定

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

merge()

將元素與相同類型的前一個同層級元素合併。

只有相同 ElementType 的元素才能合併。目前元素中包含的所有子項元素都會移至前一個同層級元素。

目前元素會從文件中移除。

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

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
const par1 = body.appendParagraph('Paragraph 1.');
const 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.
const cells = [
  ['Row 1, Cell 1', 'Row 1, Cell 2'],
  ['Row 2, Cell 1', 'Row 2, Cell 2'],
];
// Build a table from the array.
const table = body.appendTable(cells);
// Get the first row in the table.
const row = table.getRow(0);
// Get the two cells in this row.
const cell1 = row.getCell(0);
const cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
const merged = cell2.merge();

回攻員

Paragraph|null:合併的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeChild(child)

移除指定的子項元素。

參數

名稱類型說明
childElement要移除的子元素

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeFromParent()

從父項移除元素。

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

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

回攻員

Paragraph|null:已移除的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removePositionedImage(id)

根據圖片 ID 移除 PositionedImage

參數

名稱類型說明
idString圖片 ID

回攻員

Boolean:指定圖片是否已移除

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

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');

參數

名稱類型說明
searchPatternString要搜尋的規則運算式模式
replacementString要用來取代的文字

回攻員

Element:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setAlignment(alignment)

可設定 HorizontalAlignment

參數

名稱類型說明
alignmentHorizontalAlignment水平對齊方式

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setAttributes(attributes)

設定元素的屬性。

指定的屬性參數必須是物件,其中每個屬性名稱都是 DocumentApp.Attribute 列舉中的項目,每個屬性值都是要套用的新值。

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

參數

名稱類型說明
attributesObject元素的屬性。

回攻員

Paragraph:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setHeading(heading)

可設定 ParagraphHeading

參數

名稱類型說明
headingParagraphHeading標題

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setIndentEnd(indentEnd)

設定結尾縮排,以點為單位。

參數

名稱類型說明
indentEndNumber結尾縮排 (以點為單位)

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setIndentFirstLine(indentFirstLine)

以點為單位設定首行縮排。

參數

名稱類型說明
indentFirstLineNumber首行縮排 (以點為單位)

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setIndentStart(indentStart)

設定起始縮排,以點為單位。

參數

名稱類型說明
indentStartNumber起始縮排 (以點為單位)

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setLeftToRight(leftToRight)

設定從左到右的文字方向。

參數

名稱類型說明
leftToRightBoolean由左至右設定

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setLineSpacing(multiplier)

設定行距,以數量表示用於間隔的行數。

參數

名稱類型說明
multiplierNumber行數

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setLinkUrl(url)

設定連結網址。

參數

名稱類型說明
urlString連結網址

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setSpacingAfter(spacingAfter)

以點為單位,設定元素後的間距。

參數

名稱類型說明
spacingAfterNumber元素後的間距 (以點為單位)

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setSpacingBefore(spacingBefore)

以點為單位,設定元素前的間距。

參數

名稱類型說明
spacingBeforeNumber元素前的間距 (以點為單位)

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setText(text)

將段落內容設為文字。

注意:現有內容會遭到清除。

參數

名稱類型說明
textString新文字內容

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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.
const documentTab =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

參數

名稱類型說明
textAlignmentTextAlignment要套用的文字對齊類型

回攻員

Paragraph:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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