代表段落的元素。Paragraph
可能包含 Equation
、Footnote
、HorizontalRule
、InlineDrawing
、InlineImage
、PageBreak
和 Text
元素。如要進一步瞭解文件結構,請參閱 Google 文件擴充指南。
「Paragraphs
」不得包含換行字元。換行字元 (「\n」) 會轉換為換行符號 (「<!--」字元)。
var body = DocumentApp.getActiveDocument().getBody(); // Append a document 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)
從指定的映像檔 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('abc123456'); // Gets the document body. const body = doc.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 document, anchored to the first paragraph. paragraph.addPositionedImage(image);
參數
姓名 | 類型 | 說明 |
---|---|---|
image | BlobSource | 圖片資料。 |
回攻
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('abc123456'); // Gets the document body. const body = doc.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('abc123456'); // Gets the document body. const body = doc.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);
參數
姓名 | 類型 | 說明 |
---|---|---|
image | BlobSource | 圖片資料。 |
回攻
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('abc123456'); // Gets the document body. const body = doc.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);
參數
姓名 | 類型 | 說明 |
---|---|---|
image | InlineImage | 圖片資料。 |
回攻
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('abc123456'); // Gets the document body. const body = doc.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('abc123456'); // Gets the document body. const body = doc.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);
參數
姓名 | 類型 | 說明 |
---|---|---|
pageBreak | PageBreak | 要附加的分頁符號。 |
回攻
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('abc123456'); // Gets the document body. const body = doc.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.');
參數
姓名 | 類型 | 說明 |
---|---|---|
text | String | 文字內容。 |
回攻
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('abc123456'); // Gets the document body. const body = doc.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);
參數
姓名 | 類型 | 說明 |
---|---|---|
text | Text | 要附加的文字元素。 |
回攻
Text
:附加的文字元素。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
clear()
copy()
editAsText()
取得目前元素的 Text
版本即可進行編輯。
使用 editAsText
操控元素內容做為 RTF 格式。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)
搜尋元素內容,在指定類型的子系中。
參數
姓名 | 類型 | 說明 |
---|---|---|
elementType | ElementType | 要搜尋的元素類型 |
回攻
RangeElement
:代表搜尋元素位置的搜尋結果
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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; } }
參數
姓名 | 類型 | 說明 |
---|---|---|
elementType | ElementType | 要搜尋的元素類型 |
from | RangeElement | 搜尋結果來源 |
回攻
RangeElement
:表示搜尋元素下一個位置的搜尋結果
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern)
使用規則運算式搜尋指定文字模式的元素內容。
但系統只支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
提供的規則運算式模式會與目前元素中包含的每個文字區塊分開比對。
參數
姓名 | 類型 | 說明 |
---|---|---|
searchPattern | String | 要搜尋的模式 |
回攻
RangeElement
:指出搜尋文字的位置,如果沒有任何相符項目,則為 null
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
從特定搜尋結果模式搜尋指定文字模式的元素內容。
但系統只支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
提供的規則運算式模式會與目前元素中包含的每個文字區塊分開比對。
參數
姓名 | 類型 | 說明 |
---|---|---|
searchPattern | String | 要搜尋的模式 |
from | RangeElement | 搜尋結果來源 |
回攻
RangeElement
:指出搜尋文字下一個位置的搜尋結果,如果沒有相符項目則傳回 null
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getAlignment()
// 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 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());
回攻
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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
:元素的屬性。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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."); }
參數
姓名 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 要擷取的子元素索引 |
回攻
Element
:指定索引的子元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChildIndex(child)
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('abc123456'); // Gets the document body. const body = doc.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
:標題。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getIndentEnd()
擷取結尾縮排。
回攻
Number
:結尾縮排,以點為單位
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getIndentFirstLine()
擷取第一個行縮排。
回攻
Number
— 首行縮排,以點表示
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getIndentStart()
擷取起始縮排。
回攻
Number
:開始縮排
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLineSpacing()
擷取點的行距。
回攻
Number
:行距
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLinkUrl()
擷取連結網址。
回攻
String
:連結網址,如果元素包含多個屬性的值,則為空值
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNextSibling()
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
:兒童人數
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParent()
擷取元素的父項元素。
父項元素包含目前元素。
回攻
ContainerElement
:父項元素,
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPositionedImage(id)
透過圖片 ID 取得 PositionedImage
。
參數
姓名 | 類型 | 說明 |
---|---|---|
id | String | 圖片 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()
getSpacingAfter()
擷取元素之後的間距 (以點為單位)。
回攻
Number
:元素後方的間距 (以點為單位)
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getSpacingBefore()
擷取元素前面的間距 (以點為單位)。
回攻
Number
:元素前方的間距 (以點為單位)
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
和 DocumentApp.TextAlignment.SUPERSCRIPT
。
回攻
TextAlignment
:文字對齊類型;如果文字包含多種類型的文字對齊,或是從未設定文字對齊方式,則設為 null
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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
— 元素類型。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertHorizontalRule(childIndex)
在指定索引中建立並插入 HorizontalRule
。
參數
姓名 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 插入元素的索引 |
回攻
HorizontalRule
:新的水平規則元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertInlineImage(childIndex, image)
在指定索引上,從指定的圖片 blob 建立及插入新的 InlineImage
。
參數
姓名 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 插入元素的索引 |
image | BlobSource | 圖片資料 |
回攻
InlineImage
:插入的內嵌圖片元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertInlineImage(childIndex, image)
在指定的索引插入指定的 InlineImage
。
參數
姓名 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 插入元素的索引 |
image | InlineImage | 圖片資料 |
回攻
InlineImage
:插入的內嵌圖片元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertPageBreak(childIndex)
在指定索引中建立並插入新的 PageBreak
。
注意:PageBreaks
不得包含 TableCells
。
如果表格儲存格含有目前元素,系統就會擲回例外狀況。
參數
姓名 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 插入元素的索引 |
回攻
PageBreak
:新的分頁符號元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertPageBreak(childIndex, pageBreak)
在指定的索引插入指定的 PageBreak
。
注意:PageBreaks
不得包含 TableCells
。
如果表格儲存格含有目前元素,系統就會擲回例外狀況。
參數
姓名 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 插入元素的索引 |
pageBreak | PageBreak | 插入裝置的 page 中斷點 |
回攻
PageBreak
:插入的分頁符號元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertText(childIndex, text)
insertText(childIndex, text)
isAtDocumentEnd()
isLeftToRight()
擷取從左到右的設定。
回攻
Boolean
:從左到右的設定
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
merge()
將元素與相同類型的上述同層級項目合併。
只能合併同一個 ElementType
的元素。目前元素中包含的任何子項元素都會移至前一個同層級元素。
系統會從文件中移除目前元素。
var body = DocumentApp.getActiveDocument().getBody(); // Example 1: Merge paragraphs // Append two paragraphs to the document. 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
:合併的元素。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeChild(child)
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(); }
回攻
Paragraph
:已移除的元素。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removePositionedImage(id)
依圖片 ID 移除 PositionedImage
。
參數
姓名 | 類型 | 說明 |
---|---|---|
id | String | 圖片 ID |
回攻
Boolean
:指定圖片是否已移除
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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");
參數
姓名 | 類型 | 說明 |
---|---|---|
searchPattern | String | 用於搜尋的規則運算式模式 |
replacement | String | 要取代的文字 |
回攻
Element
:目前的元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setAlignment(alignment)
可設定 HorizontalAlignment
。
參數
姓名 | 類型 | 說明 |
---|---|---|
alignment | HorizontalAlignment | 水平對齊 |
回攻
Paragraph
:目前的元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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);
參數
姓名 | 類型 | 說明 |
---|---|---|
attributes | Object | 元素的屬性。 |
回攻
Paragraph
:目前元素。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setHeading(heading)
可設定 ParagraphHeading
。
參數
姓名 | 類型 | 說明 |
---|---|---|
heading | ParagraphHeading | 標題 |
回攻
Paragraph
:目前的元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setIndentEnd(indentEnd)
setIndentFirstLine(indentFirstLine)
setIndentStart(indentStart)
setLeftToRight(leftToRight)
setLineSpacing(multiplier)
setLinkUrl(url)
setSpacingAfter(spacingAfter)
setSpacingBefore(spacingBefore)
setText(text)
將段落的內容設為文字。
注意:現有內容會遭到清除。
參數
姓名 | 類型 | 說明 |
---|---|---|
text | String | 新的文字內容 |
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
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);
參數
姓名 | 類型 | 說明 |
---|---|---|
textAlignment | TextAlignment | 要套用的文字對齊類型 |
回攻
Paragraph
:目前的元素
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents