代表 RTF 區域的元素。Document 中的所有文字都包含在 Text 元素中。
Text 元素可包含在 Equation、Equation、List 或 Paragraph 中,但本身不能包含任何其他元素。如要進一步瞭解文件結構,請參閱 擴充 Google 文件指南。
// Gets the body contents of the active tab. const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Use editAsText to obtain a single text element containing // all the characters in the tab. const text = body.editAsText(); // Insert text at the beginning of the tab. text.insertText(0, 'Inserted text.\n'); // Insert text at the end of the tab. text.appendText('\nAppended text.'); // Make the first half of the tab blue. text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');
方法
內容詳盡的說明文件
append Text(text)
Adds the specified text to the end of this text region.
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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 the text, 'Sample body text,' to the end of the tab body. const text = body.editAsText().appendText('Sample body text');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
text | String | 要附加的文字。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
copy()
delete Text(startOffset, endOffsetInclusive)
刪除特定範圍的文字。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Deletes the first 10 characters in the body. const text = body.editAsText().deleteText(0, 9);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 要刪除的第一個字元的字元偏移量。 |
end | Integer | 要刪除的最後一個字元的字元偏移。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
edit As Text()
取得目前元素的 Text 版本,以供編輯。
使用 edit 將元素內容當做 RTF 文字來操控。edit 模式會忽略非文字元素 (例如 Inline 和 Horizontal)。
如果子項元素完全包含在已刪除的文字範圍內,系統會從元素中移除這些子項元素。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Insert two paragraphs separated by a paragraph containing an // horizontal rule. body.insertParagraph(0, 'An editAsText sample.'); body.insertHorizontalRule(0); body.insertParagraph(0, 'An example.'); // Delete " sample.\n\n An" removing the horizontal rule in the process. body.editAsText().deleteText(14, 25);
回攻員
Text:目前元素的文字版本
find Text(searchPattern)
使用規則運算式在元素內容中搜尋指定的文字模式。
系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
search | String | 要搜尋的模式 |
回攻員
Range:搜尋結果,指出搜尋文字的位置;如果沒有相符項目,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Text(searchPattern, from)
從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。
系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
search | String | 要搜尋的模式 |
from | Range | 要從哪個搜尋結果開始搜尋 |
回攻員
Range:搜尋結果,指出搜尋文字的下一個位置;如果沒有相符項目,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Attributes()
擷取元素的屬性。
結果會是物件,其中包含每個有效元素屬性的屬性,且每個屬性名稱都對應至 Document 列舉中的項目。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Append a styled paragraph. const par = body.appendParagraph('A bold, italicized paragraph.'); par.setBold(true); par.setItalic(true); // Retrieve the paragraph's attributes. const atts = par.getAttributes(); // Log the paragraph attributes. for (const att in atts) { Logger.log(`${att}:${atts[att]}`); }
回攻員
Object:元素的屬性。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Attributes(offset)
擷取指定字元偏移的屬性。
結果會是物件,其中包含每個有效文字屬性的屬性,且每個屬性名稱都對應至 Document 列舉中的項目。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Declares style attributes. const style = {}; style[DocumentApp.Attribute.BOLD] = true; style[DocumentApp.Attribute.ITALIC] = true; style[DocumentApp.Attribute.FONT_SIZE] = 29; // Sets the style attributes to the tab's body. const text = body.editAsText(); text.setAttributes(style); // Gets the style attributes applied to the eleventh character in the // body and logs them to the console. const attributes = text.getAttributes(10); console.log(attributes);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
Object:元素的屬性。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Background Color()
擷取背景顏色設定。
回攻員
String|null:背景顏色,以 CSS 標記格式 (例如 '#ffffff') 格式化,如果元素包含這個屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Background Color(offset)
擷取指定字元偏移量的背景顏色。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID', ); // 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(); // Sets the background color of the first 3 characters in the body. const text = body.editAsText().setBackgroundColor(0, 2, '#FFC0CB'); // Gets the background color of the first character in the body. const backgroundColor = text.getBackgroundColor(0); // Logs the background color to the console. console.log(backgroundColor);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
String|null:背景顏色,格式為 CSS 標記 (例如 '#ffffff')。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Family()
擷取字型系列設定。名稱可以是 Google 文件「字型」選單或 Google 字型中的任何字型,且區分大小寫。方法 get 和 set 現在使用字串名稱做為字型,而非 列舉。雖然這個列舉已淘汰,但為了與舊版指令碼相容,仍可繼續使用。Font
回攻員
String|null:字型系列,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Family(offset)
擷取指定字元偏移量的字型系列。名稱可以是 Google 文件「字型」選單或 Google 字型中的任何字型,且會區分大小寫。方法 get 和 set 現在使用字串名稱做為字型,而不是 列舉。雖然這個列舉已淘汰,但仍可供使用,以確保與舊版指令碼相容。
Font
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the font of the first 16 characters to Impact. const text = body.editAsText().setFontFamily(0, 15, 'Impact'); // Gets the font family of the 16th character in the tab body. const fontFamily = text.getFontFamily(15); // Logs the font family to the console. console.log(fontFamily);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
String|null - 字型系列。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Size()
擷取字型大小設定。
回攻員
Number|null:字型大小,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Font Size(offset)
擷取指定字元偏移量的字型大小。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the font size of the first 13 characters to 15. const text = body.editAsText().setFontSize(0, 12, 15); // Gets the font size of the first character. const fontSize = text.getFontSize(0); // Logs the font size to the console. console.log(fontSize);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
Number|null:字型大小。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Foreground Color()
擷取前景顏色設定。
回攻員
String|null:前景色,格式為 CSS 標記 (如 '#ffffff'),如果元素包含這個屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Foreground Color(offset)
擷取指定字元偏移量的前景色。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the foreground color of the first 3 characters in the tab body. const text = body.editAsText().setForegroundColor(0, 2, '#0000FF'); // Gets the foreground color of the first character in the tab body. const foregroundColor = text.getForegroundColor(0); // Logs the foreground color to the console. console.log(foregroundColor);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
String|null:前景顏色,格式為 CSS 標記 (例如 '#ffffff')。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Link Url()
擷取連結網址。
回攻員
String|null - 連結網址,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Link Url(offset)
擷取指定字元偏移位置的連結網址。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Applies a link to the first 10 characters in the body. const text = body.editAsText().setLinkUrl(0, 9, 'https://www.example.com/'); // Gets the URL of the link from the first character. const link = text.getLinkUrl(0); // Logs the link URL to the console. console.log(link);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
String|null:連結網址。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Next Sibling()
get Parent()
擷取元素的父項元素。
父項元素包含目前的元素。
回攻員
Container:父項元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Previous Sibling()
get Text()
以文字字串形式擷取元素內容。
回攻員
String:元素內容 (文字字串)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Alignment()
取得文字對齊方式。可用的對齊類型包括 Document、Document 和 Document。
回攻員
Text:文字對齊類型,或 null (如果文字包含多種文字對齊類型,或從未設定文字對齊方式)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Alignment(offset)
取得單一字元的文字對齊方式。可用的對齊類型包括 Document、Document 和 Document。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the text alignment of the tab's body to NORMAL. const text = body.editAsText().setTextAlignment(DocumentApp.TextAlignment.NORMAL); // Gets the text alignment of the ninth character. const alignment = text.getTextAlignment(8); // Logs the text alignment to the console. console.log(alignment.toString());
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元的位移。 |
回攻員
Text:文字對齊方式的類型,如果從未設定文字對齊方式,則為 null。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Attribute Indices()
擷取對應於不同文字格式設定執行個體開頭的文字索引集。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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 indices at which text formatting changes. const indices = body.editAsText().getTextAttributeIndices(); // Logs the indices to the console. console.log(indices.toString());
回攻員
Integer[] - 文字格式變更的文字索引集。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Type()
擷取元素的 Element。
使用 get 判斷指定元素的確切類型。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Obtain the first element in the active tab's body. const firstChild = body.getChild(0); // Use getType() to determine the element's type. if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) { Logger.log('The first element is a paragraph.'); } else { Logger.log('The first element is not a paragraph.'); }
回攻員
Element:元素類型。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Text(offset, text)
在指定字元位移處插入指定文字。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Inserts the text, 'Sample inserted text', at the start of the body content. const text = body.editAsText().insertText(0, 'Sample inserted text');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 要插入文字的字元位移值。 |
text | String | 要插入的文字。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is At Document End()
is Bold()
擷取粗體設定。
回攻員
Boolean|null:文字是否為粗體,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Bold(offset)
擷取指定字元偏移的粗體設定。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Bolds the first 4 characters in the tab body. const text = body.editAsText().setBold(0, 3, true); // Gets whether or not the text is bold. const bold = text.editAsText().isBold(0); // Logs the text's bold setting to the console console.log(bold);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
Boolean|null:粗體設定。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Italic()
擷取斜體設定。
回攻員
Boolean|null:文字是否為斜體,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Italic(offset)
擷取指定字元偏移的斜體設定。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 13 characters of the tab body to italic. const text = body.editAsText().setItalic(0, 12, true); // Gets whether the fifth character in the tab body is set to // italic and logs it to the console. const italic = text.isItalic(4); console.log(italic);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
Boolean|null:斜體設定。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Strikethrough()
擷取刪除線設定。
回攻員
Boolean|null:文字是否為刪除線,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Strikethrough(offset)
擷取指定字元偏移的刪除線設定。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 17 characters of the tab body to strikethrough. const text = body.editAsText().setStrikethrough(0, 16, true); // Gets whether the first character in the tab body is set to // strikethrough and logs it to the console. const strikethrough = text.isStrikethrough(0); console.log(strikethrough);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
Boolean|null - 刪除線設定。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Underline()
擷取底線設定。
回攻員
Boolean|null:文字是否加上底線,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
is Underline(offset)
擷取指定字元偏移的底線設定。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 13 characters of the tab body to underline. const text = body.editAsText().setUnderline(0, 12, false); // Gets whether the first character in the tab body is set to // underline and logs it to the console const underline = text.editAsText().isUnderline(0); console.log(underline);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
offset | Integer | 字元偏移。 |
回攻員
Boolean|null - 底線設定。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
merge()
將元素與相同類型的前一個同層級元素合併。
只有相同 Element 的元素才能合併。目前元素中包含的所有子項元素都會移至前一個同層級元素。
目前元素會從文件中移除。
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();
回攻員
Text|null:合併的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
remove From Parent()
從父項移除元素。
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(); }
回攻員
Text|null:已移除的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
replace Text(searchPattern, replacement)
使用規則運算式,將指定文字模式的所有例項替換為指定替代字串。
搜尋模式會以字串形式傳遞,而非 JavaScript 規則運算式物件。因此,您需要逸出模式中的任何反斜線。
這個方法使用 Google 的 RE2 規則運算式程式庫,因此支援的語法有限。
系統會針對目前元素中包含的每個文字區塊,獨立比對所提供的規則運算式模式。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText('^.*Apps ?Script.*$', 'Apps Script');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
search | String | 要搜尋的規則運算式模式 |
replacement | String | 要用來取代的文字 |
回攻員
Element:目前元素
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Attributes(startOffset, endOffsetInclusive, attributes)
將指定屬性套用至指定字元範圍。
指定的屬性參數必須是物件,其中每個屬性名稱都是 Document 列舉中的項目,每個屬性值都是要套用的新值。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Declares style attributes for font size and font family. const style = {}; style[DocumentApp.Attribute.FONT_SIZE] = 20; style[DocumentApp.Attribute.FONT_FAMILY] = 'Impact'; // Sets the style attributes to the first 9 characters in the tab's body. const text = body.setAttributes(0, 8, style);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
attributes | Object | 元素的屬性。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Attributes(attributes)
設定元素的屬性。
指定的屬性參數必須是物件,其中每個屬性名稱都是 Document 列舉中的項目,每個屬性值都是要套用的新值。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Define a custom paragraph style. const style = {}; style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.RIGHT; style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri'; style[DocumentApp.Attribute.FONT_SIZE] = 18; style[DocumentApp.Attribute.BOLD] = true; // Append a plain paragraph. const par = body.appendParagraph('A paragraph with custom style.'); // Apply the custom style. par.setAttributes(style);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
attributes | Object | 元素的屬性。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Background Color(startOffset, endOffsetInclusive, color)
為指定字元範圍設定背景顏色。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the background color of the first 3 characters in the // tab body to hex color #0000FF. const text = body.editAsText().setBackgroundColor(0, 2, '#0000FF');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
color | String | 背景顏色,格式為 CSS 標記 (例如 '#ffffff')。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Background Color(color)
set Bold(bold)
set Bold(startOffset, endOffsetInclusive, bold)
為指定字元範圍設定粗體。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 11 characters in the tab's body to bold. const text = body.editAsText().setBold(0, 10, true);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
bold | Boolean | 粗體設定。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Family(startOffset, endOffsetInclusive, fontFamilyName)
為指定字元範圍設定字型系列。名稱可以是 Google 文件「字型」選單或 Google 字型中的任何字型,且區分大小寫。無法辨識的字型名稱會以 Arial 顯示。方法 get 和 set 現在使用字串名稱做為字型,而非 列舉。雖然這個列舉已淘汰,但仍可與舊版指令碼相容。
Font
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the font of the first 4 characters in the tab's body to Roboto. const text = body.editAsText().setFontFamily(0, 3, 'Roboto');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
font | String | 字型系列名稱,來自 Google 文件或 Google 字型的「字型」選單。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Family(fontFamilyName)
設定字型系列。名稱可以是 Google 文件「字型」選單或 Google 字型中的任何字型,且區分大小寫。無法辨識的字型名稱會以 Arial 顯示。方法 get 和 set 現在使用字串名稱做為字型,而非 列舉。雖然這個列舉已淘汰,但為了與舊版指令碼相容,仍可繼續使用。Font
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
font | String | 字型系列名稱,可從 Google 文件或 Google Fonts 的「字型」選單中取得 |
回攻員
Text:目前元素
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Size(startOffset, endOffsetInclusive, size)
為指定字元範圍設定字型大小。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the size of the first 11 characters in the tab's body to 12. const text = body.editAsText().setFontSize(0, 10, 12);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
size | Number | 字型大小。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Font Size(size)
set Foreground Color(startOffset, endOffsetInclusive, color)
設定指定字元範圍的前景色。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the foreground color of the first 2 characters in the // tab's body to hex color #FF0000. const text = body.editAsText().setForegroundColor(0, 1, '#FF0000'); // Gets the foreground color for the second character in the tab's body. const foregroundColor = text.getForegroundColor(1); // Logs the foreground color to the console. console.log(foregroundColor);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
color | String | 前景顏色,格式為 CSS 標記 (例如 '#ffffff')。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Foreground Color(color)
set Italic(italic)
set Italic(startOffset, endOffsetInclusive, italic)
為指定字元範圍設定斜體。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 11 characters in the tab's body to italic. const text = body.editAsText().setItalic(0, 10, true);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
italic | Boolean | 斜體設定。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Link Url(startOffset, endOffsetInclusive, url)
為指定字元範圍設定連結網址。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Applies a link to the first 11 characters in the body. const text = body.editAsText().setLinkUrl(0, 10, 'https://example.com');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
url | String | 連結網址。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Link Url(url)
set Strikethrough(strikethrough)
set Strikethrough(startOffset, endOffsetInclusive, strikethrough)
為指定字元範圍設定刪除線。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 11 characters in the tab's body to strikethrough. const text = body.editAsText().setStrikethrough(0, 10, true);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
strikethrough | Boolean | 刪除線設定。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text(text)
設定文字內容。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Replaces the contents of the body with the text, 'New body text.' const text = body.editAsText().setText('New body text.');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
text | String | 新文字內容。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text Alignment(startOffset, endOffsetInclusive, textAlignment)
為指定字元範圍設定文字對齊方式。可用的對齊類型包括 Document、Document 和 Document。
// Make the first character in the first paragraph of the active tab be // superscript. const documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab(); const text = documentTab.getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(0, 0, DocumentApp.TextAlignment.SUPERSCRIPT);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 字元範圍的起始位移。 |
end | Integer | 字元範圍的結束偏移 (含)。 |
text | Text | 要套用的文字對齊類型。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text Alignment(textAlignment)
設定文字對齊方式。可用的對齊類型包括 Document、Document 和 Document。
// Make the entire first paragraph in the active tab be superscript. const documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab(); const text = documentTab.getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
text | Text | 要套用的文字對齊類型 |
回攻員
Text:目前元素
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Underline(underline)
set Underline(startOffset, endOffsetInclusive, underline)
為指定字元範圍設定底線。
// Opens the Docs file by its URL. If you created your script from within a // Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the URL with your own. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/DOCUMENT_ID/edit', ); // 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(); // Sets the first 11 characters in the tab's body to underline. const text = body.editAsText().setUnderline(0, 10, true);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
start | Integer | 文字範圍的起始位移。 |
end | Integer | 文字範圍的結束偏移。 |
underline | Boolean | 底線設定。 |
回攻員
Text:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents