Class Text

文字

代表 RTF 格式區域的元素。Document 中的所有文字都包含在 Text 元素中。 Text 元素可以包含在 EquationEquationFunctionListItemParagraph 中,但本身不能包含任何其他元素。如要進一步瞭解文件結構,請參閱擴充 Google 文件的指南

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

// Use editAsText to obtain a single text element containing
// all the characters in the document.
var text = body.editAsText();

// Insert text at the beginning of the document.
text.insertText(0, 'Inserted text.\n');

// Insert text at the end of the document.
text.appendText('\nAppended text.');

// Make the first half of the document blue.
text.setForegroundColor(0, text.getText().length / 2, '#00FFFF');

方法

方法傳回類型簡短說明
appendText(text)Text將指定文字新增至這個文字區域的結尾。
copy()Text傳回目前元素的卸離深層複本。
deleteText(startOffset, endOffsetInclusive)Text刪除特定文字範圍。
editAsText()Text取得目前元素的 Text 版本以進行編輯。
findText(searchPattern)RangeElement使用規則運算式,在元素上搜尋指定文字模式。
findText(searchPattern, from)RangeElement從指定搜尋結果開始,搜尋指定內容模式的元素內容。
getAttributes()Object擷取元素的屬性。
getAttributes(offset)Object擷取指定字元偏移的屬性。
getBackgroundColor()String擷取背景顏色設定。
getBackgroundColor(offset)String擷取指定字元偏移時的背景顏色。
getFontFamily()String擷取字型系列設定。
getFontFamily(offset)String擷取指定字元偏移時的字型系列。
getFontSize()Number擷取字型大小設定。
getFontSize(offset)Number擷取指定字元偏移時的字型大小。
getForegroundColor()String擷取前景色彩設定。
getForegroundColor(offset)String擷取指定字元偏移時的前景色彩。
getLinkUrl()String擷取連結網址。
getLinkUrl(offset)String在指定的字元偏移位置擷取連結網址。
getNextSibling()Element擷取元素的下一個同層級元素。
getParent()ContainerElement擷取元素的父項元素。
getPreviousSibling()Element擷取元素的先前同層級元素。
getText()String以文字字串擷取元素內容。
getTextAlignment()TextAlignment取得文字對齊方式。
getTextAlignment(offset)TextAlignment取得單一字元的文字對齊方式。
getTextAttributeIndices()Integer[]擷取對應不同文字格式執行作業開始的文字索引組合。
getType()ElementType擷取元素的 ElementType
insertText(offset, text)Text以指定的字元位移插入指定文字。
isAtDocumentEnd()Boolean決定元素是否位於 Document 的結尾。
isBold()Boolean擷取粗體設定。
isBold(offset)Boolean擷取指定字元偏移的粗體設定。
isItalic()Boolean擷取斜體設定。
isItalic(offset)Boolean擷取指定字元偏移的斜體設定。
isStrikethrough()Boolean擷取刪除線設定。
isStrikethrough(offset)Boolean擷取指定字元偏移時的刪除線設定。
isUnderline()Boolean擷取底線設定。
isUnderline(offset)Boolean擷取指定字元偏移的底線設定。
merge()Text將元素與先前類型的同層級合併。
removeFromParent()Text從父項中移除元素。
replaceText(searchPattern, replacement)Element使用規則運算式將指定文字模式的所有出現情況替換為特定替換字串。
setAttributes(startOffset, endOffsetInclusive, attributes)Text將指定的屬性套用至指定的字元範圍。
setAttributes(attributes)Text設定元素的屬性。
setBackgroundColor(startOffset, endOffsetInclusive, color)Text設定指定字元範圍的背景顏色。
setBackgroundColor(color)Text設定背景顏色。
setBold(bold)Text設定粗體。
setBold(startOffset, endOffsetInclusive, bold)Text設定指定字元範圍的粗體設定。
setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)Text設定指定字元範圍的字型系列。
setFontFamily(fontFamilyName)Text設定字型系列。
setFontSize(startOffset, endOffsetInclusive, size)Text設定指定字元範圍的字型大小。
setFontSize(size)Text設定字型大小。
setForegroundColor(startOffset, endOffsetInclusive, color)Text設定指定字元範圍的前景顏色。
setForegroundColor(color)Text設定前景顏色。
setItalic(italic)Text設定斜體設定。
setItalic(startOffset, endOffsetInclusive, italic)Text用於設定指定字元範圍的斜體設定。
setLinkUrl(startOffset, endOffsetInclusive, url)Text設定指定字元範圍的連結網址。
setLinkUrl(url)Text設定連結網址。
setStrikethrough(strikethrough)Text設定刪除線。
setStrikethrough(startOffset, endOffsetInclusive, strikethrough)Text指定指定字元範圍的刪除線設定。
setText(text)Text設定文字內容。
setTextAlignment(startOffset, endOffsetInclusive, textAlignment)Text設定指定字元範圍的文字對齊方式。
setTextAlignment(textAlignment)Text設定文字對齊方式。
setUnderline(underline)Text設定底線設定。
setUnderline(startOffset, endOffsetInclusive, underline)Text設定指定字元範圍的底線設定。

內容詳盡的說明文件

appendText(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Adds the text, 'Sample body text,' to the end of the document body.
const text = body.editAsText().appendText('Sample body text');

參數

名稱類型說明
textString要附加的文字。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

copy()

傳回目前元素的卸離深層複本。

系統也會複製元素中的所有子元素。新元素沒有父項。

Return 鍵

Text — 新的副本。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

deleteText(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Deletes the first 10 characters in the body.
 const text = body.editAsText().deleteText(0, 9);

參數

名稱類型說明
startOffsetInteger第一個要刪除的字元的偏移值。
endOffsetInclusiveInteger要刪除的最後一個字元的字元偏移。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

editAsText()

取得目前元素的 Text 版本以進行編輯。

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

已刪除的文字範圍中完全包含的子元素會從元素中移除。

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

Return 鍵

Text:目前元素的文字版本


findText(searchPattern)

使用規則運算式,在元素上搜尋指定文字模式。

無法完整支援一部分的 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

提供的規則運算式模式會與目前元素中包含的每個文字區塊進行獨立比對。

參數

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

Return 鍵

RangeElement:代表搜尋文字所在位置的搜尋結果;如果沒有比對結果,則傳回 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

findText(searchPattern, from)

從指定搜尋結果開始,搜尋指定內容模式的元素內容。

無法完整支援一部分的 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

提供的規則運算式模式會與目前元素中包含的每個文字區塊進行獨立比對。

參數

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

Return 鍵

RangeElement:代表搜尋文字的下一個位置;如果沒有相符項目,則傳回空值

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

Return 鍵

Object — 元素的屬性。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getAttributes(offset)

擷取指定字元偏移的屬性。

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

// 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/abc123456/edit');

// Gets the document body.
const body = doc.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 document 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);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

Object — 元素的屬性。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getBackgroundColor()

擷取背景顏色設定。

Return 鍵

String:以 CSS 標記法表示的背景顏色 (如 '#ffffff');如果元素包含多個屬性,則會傳回空值

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getBackgroundColor(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/abc123456');

// Gets the document body.
const body = doc.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);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

String:以 CSS 標記法表示的背景顏色 (例如 '#ffffff')。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getFontFamily()

擷取字型系列設定。名稱可以是 Google 文件字型選單中的任何字型,或是 Google 字型,並且區分大小寫。getFontFamily()setFontFamily(fontFamilyName) 方法現在使用字串的字串做為字型,而非 FontFamily 列舉。雖然這個列舉已經淘汰,但仍可用於與舊版指令碼相容。

Return 鍵

String:字型系列,如果元素包含多個屬性的值,則傳回 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getFontFamily(offset)

擷取指定字元偏移時的字型系列。名稱可以是 Google 文件中的字型選單的任何字型,或是 Google 字型,而且區分大小寫。getFontFamily()setFontFamily(fontFamilyName) 方法現在會使用字串名稱做為字型,而非 FontFamily 列舉。雖然這個列舉已經淘汰,但仍可用於與舊版指令碼的相容性。

// 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/abc123456/edit');

// Gets the document body.
const body = doc.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 document body.
const fontFamily = text.getFontFamily(15);

// Logs the font family to the console.
console.log(fontFamily);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

String:字型系列。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getFontSize()

擷取字型大小設定。

Return 鍵

Number:字型大小,如果元素包含多個屬性,則傳回 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getFontSize(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/abc123456/edit');

// Gets the document body.
const body = doc.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);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

Number:字型大小。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getForegroundColor()

擷取前景色彩設定。

Return 鍵

String:以 CSS 標記法表示的前景顏色 (例如 '#ffffff'),如果元素包含多個屬性,則會傳回空值

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getForegroundColor(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the foreground color of the first 3 characters in the document body.
const text = body.editAsText().setForegroundColor(0, 2, '#0000FF');

// Gets the foreground color of the first character in the document body.
const foregroundColor = text.getForegroundColor(0);

// Logs the foreground color to the console.
console.log(foregroundcolor);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

String:前景顏色,格式為 CSS 標記 (例如 '#ffffff')。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getLinkUrl()

擷取連結網址。

Return 鍵

String:連結網址,如果元素包含多個屬性,則傳回 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getLinkUrl(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/abc123456/edit');

// Gets the document body.
const body = doc.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);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

String:連結網址。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getNextSibling()

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

下一個同層級的父項相同,並跟隨目前元素。

Return 鍵

Element — 下一個同層級元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getParent()

擷取元素的父項元素。

父項元素包含目前元素。

Return 鍵

ContainerElement:父項元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getPreviousSibling()

擷取元素的先前同層級元素。

上一個同層級的父項相同,且在目前元素之前。

Return 鍵

Element — 上一個同層級元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getText()

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

Return 鍵

String:以文字字串表示元素內容

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getTextAlignment()

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

Return 鍵

TextAlignment:文字對齊方式;如果文字具有多種文字對齊方式,或是從未設定文字對齊方式,則為 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getTextAlignment(offset)

取得單一字元的文字對齊方式。可用的對齊類型為 DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT

// 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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the text alignment of the document 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());

參數

名稱類型說明
offsetInteger字元的位移。

Return 鍵

TextAlignment — 文字對齊方式;如果未設定文字對齊方式,則為 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

getTextAttributeIndices()

擷取對應不同文字格式執行作業開始的文字索引組合。

// 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/abc123456/edit');

// Gets the document body.
const body = doc.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());

Return 鍵

Integer[] — 文字格式變更的文字索引組合。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

Return 鍵

ElementType — 元素類型。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

insertText(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Inserts the text, 'Sample inserted text', at the start of the body content.
const text = body.editAsText().insertText(0, 'Sample inserted text');

參數

名稱類型說明
offsetInteger插入文字的位移值。
textString要插入的文字。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isAtDocumentEnd()

決定元素是否位於 Document 的結尾。

Return 鍵

Boolean — 元素是否位於文件結尾。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isBold()

擷取粗體設定。

Return 鍵

Boolean:文字為粗體,如果元素包含多個屬性,則設為空值

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isBold(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Bolds the first 4 characters in the document 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);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

Boolean:粗體設定。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isItalic()

擷取斜體設定。

Return 鍵

Boolean:文字為斜體,如果元素包含多個屬性,則會傳回空值

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isItalic(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 13 characters of the document body to italic.
const text = body.editAsText().setItalic(0, 12, true);

// Gets whether the fifth character in the document body is set to
// italic and logs it to the console.
const italic = text.isItalic(4);
console.log(italic);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

Boolean — 斜體設定。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isStrikethrough()

擷取刪除線設定。

Return 鍵

Boolean — 文字是否加上刪除線。如果元素包含多個屬性,則傳回 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isStrikethrough(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 17 characters of the document body to strikethrough.
const text = body.editAsText().setStrikethrough(0, 16, true);

// Gets whether the first character in the document body is set to
// strikethrough and logs it to the console.
const strikethrough = text.isStrikethrough(0);
console.log(strikethrough);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

Boolean — 刪除線設定。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isUnderline()

擷取底線設定。

Return 鍵

Boolean:文字是否加上底線;如果元素包含多個屬性,則傳回 null

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

isUnderline(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 13 characters of the document body to underline.
const text = body.editAsText().setUnderline(0, 12, false);

// Gets whether the first character in the document body is set to
// underline and logs it to the console
const underline = text.editAsText().isUnderline(0);
console.log(underline);

參數

名稱類型說明
offsetInteger字元偏移。

Return 鍵

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

Return 鍵

Text — 合併的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

removeFromParent()

從父項中移除元素。

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

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

Return 鍵

Text — 移除的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

參數

名稱類型說明
searchPatternString要搜尋的規則運算式模式
replacementString要當做替換品的文字

Return 鍵

Element:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setAttributes(startOffset, endOffsetInclusive, attributes)

將指定的屬性套用至指定的字元範圍。

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

// 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/abc123456/edit');

// Gets the document body and edits as text.
const body = doc.getBody().editAsText();

// 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 document body.
const text = body.setAttributes(0, 8, style);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
attributesObject元素的屬性。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

參數

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

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setBackgroundColor(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the background color of the first 3 characters in the
// document body to hex color #0000FF.
const text = body.editAsText().setBackgroundColor(0, 2, '#0000FF');

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
colorString以 CSS 標記法表示的背景顏色 (例如 '#ffffff')。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setBackgroundColor(color)

設定背景顏色。

參數

名稱類型說明
colorString以 CSS 標記法表示的背景顏色 (例如 '#ffffff')

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setBold(bold)

設定粗體。

參數

名稱類型說明
boldBoolean粗體設定

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setBold(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 11 characters in the document body to bold.
const text = body.editAsText().setBold(0, 10, true);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
boldBoolean粗體設定。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)

設定指定字元範圍的字型系列。名稱可以是 Google 文件中的字型選單的任何字型,或是 Google 字型,而且區分大小寫。 無法辨識的字型名稱會顯示為 Arial。getFontFamily(offset)setFontFamily(fontFamilyName) 方法現在會使用字串名稱做為字型,而非 FontFamily 列舉。雖然這個列舉已經淘汰,但仍可用於與舊版指令碼的相容性。

// 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/abc123456/edit');

// Gets body of the document.
const body = doc.getBody();

// Sets the font of the first 4 characters in the document body to Roboto.
const text = body.editAsText().setFontFamily(0, 3, 'Roboto');

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
fontFamilyNameString字型系列名稱 (擷取自 Google 文件或 Google Fonts 的字型選單)。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setFontFamily(fontFamilyName)

設定字型系列。名稱可以是 Google 文件字型選單中的任何字型,或是 Google 字型,並且區分大小寫。無法辨識的字型名稱會顯示為 Arial。getFontFamily()setFontFamily(fontFamilyName) 方法現在會使用字串名稱做為字型,而非 FontFamily 列舉。雖然這個列舉已經淘汰,但仍可用於與舊版指令碼相容。

參數

名稱類型說明
fontFamilyNameString字型系列名稱 (擷取自 Google 文件或 Google Fonts 的字型選單)

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setFontSize(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the size of the first 11 characters in the document body to 12.
const text = body.editAsText().setFontSize(0, 10, 12);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
sizeNumber字型大小。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setFontSize(size)

設定字型大小。

參數

名稱類型說明
sizeNumber字型大小

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setForegroundColor(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the foreground color of the first 2 characters in the
// document body to hex color #FF0000.
const text = body.editAsText().setForegroundColor(0, 1, '#FF0000');

// Gets the foreground color for the second character in the document body.
const foregroundColor = text.getForegroundColor(1);

//  Logs the foreground color to the console.
console.log(foregroundColor);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
colorString前景顏色,格式為 CSS 標記格式 (例如 '#ffffff')。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setForegroundColor(color)

設定前景顏色。

參數

名稱類型說明
colorString前景顏色,格式為 CSS 標記格式 (例如 '#ffffff')

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setItalic(italic)

設定斜體設定。

參數

名稱類型說明
italicBoolean斜體設定

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setItalic(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 11 characters in the document body to italic.
const text = body.editAsText().setItalic(0, 10, true);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
italicBoolean斜體設定。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setLinkUrl(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Applies a link to the first 11 characters in the body.
const text = body.editAsText().setLinkUrl(0, 10, 'https://example.com');

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
urlString連結網址。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setLinkUrl(url)

設定連結網址。

參數

名稱類型說明
urlString連結網址

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setStrikethrough(strikethrough)

設定刪除線。

參數

名稱類型說明
strikethroughBoolean刪除線設定

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setStrikethrough(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 11 characters in the document body to strikethrough.
const text = body.editAsText().setStrikethrough(0, 10, true);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
strikethroughBoolean刪除線設定。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setText(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Replaces the contents of the body with the text, 'New body text.'
const text = body.editAsText().setText('New body text.');

參數

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

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setTextAlignment(startOffset, endOffsetInclusive, textAlignment)

設定指定字元範圍的文字對齊方式。可用的對齊類型為 DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT

// Make the first character in the first paragraph be superscript.
var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(0, 0, DocumentApp.TextAlignment.SUPERSCRIPT);

參數

名稱類型說明
startOffsetInteger字元範圍的起始位移。
endOffsetInclusiveInteger字元範圍的結束位移 (含)。
textAlignmentTextAlignment要套用的文字對齊類型。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

  • 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 be superscript.
var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

參數

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

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setUnderline(underline)

設定底線設定。

參數

名稱類型說明
underlineBoolean底線設定

Return 鍵

Text:目前的元素

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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

setUnderline(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/abc123456/edit');

// Gets the document body.
const body = doc.getBody();

// Sets the first 11 characters in the document body to underline.
const text = body.editAsText().setUnderline(0, 10, true);

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束位移。
underlineBoolean底線設定。

Return 鍵

Text — 目前的元素。

授權

採用這個方法的指令碼須使用下列一或多個範圍進行授權:

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