Class Text

文字

代表 RTF 區域的元素。Document 中的所有文字都包含在 Text 元素中。 Text 元素可包含在 EquationEquationFunctionListItemParagraph 中,但本身不能包含任何其他元素。如要進一步瞭解文件結構,請參閱 擴充 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');

方法

方法傳回類型簡短說明
appendText(text)Text將指定文字加到這個文字區域的結尾。
copy()Text傳回目前元素的深層副本,且該副本已分離。
deleteText(startOffset, endOffsetInclusive)Text刪除特定範圍內的文字。
editAsText()Text取得目前元素的 Text 版本,以供編輯。
findText(searchPattern)RangeElement|null使用規則運算式在元素內容中搜尋指定的文字模式。
findText(searchPattern, from)RangeElement|null從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。
getAttributes()Object擷取元素的屬性。
getAttributes(offset)Object擷取指定字元偏移的屬性。
getBackgroundColor()String|null擷取背景顏色設定。
getBackgroundColor(offset)String|null擷取指定字元偏移量的背景顏色。
getFontFamily()String|null擷取字型系列設定。
getFontFamily(offset)String|null擷取指定字元偏移的字型系列。
getFontSize()Number|null擷取字型大小設定。
getFontSize(offset)Number|null擷取指定字元偏移量的字型大小。
getForegroundColor()String|null擷取前景顏色設定。
getForegroundColor(offset)String|null擷取指定字元偏移量的前景色。
getLinkUrl()String|null擷取連結網址。
getLinkUrl(offset)String|null擷取指定字元偏移位置的連結網址。
getNextSibling()Element|null擷取元素的下一個同層級元素。
getParent()ContainerElement|null擷取元素的父項元素。
getPreviousSibling()Element|null擷取元素的前一個同層級元素。
getText()String以文字字串形式擷取元素內容。
getTextAlignment()TextAlignment|null取得文字對齊方式。
getTextAlignment(offset)TextAlignment|null取得單一字元的文字對齊方式。
getTextAttributeIndices()Integer[]擷取對應於不同文字格式設定執行開始位置的文字索引集。
getType()ElementType擷取元素的 ElementType
insertText(offset, text)Text在指定字元位移處插入指定文字。
isAtDocumentEnd()Boolean判斷元素是否位於 Document 的結尾。
isBold()Boolean|null擷取粗體設定。
isBold(offset)Boolean|null擷取指定字元偏移的粗體設定。
isItalic()Boolean|null擷取斜體設定。
isItalic(offset)Boolean|null擷取指定字元偏移的斜體設定。
isStrikethrough()Boolean|null擷取刪除線設定。
isStrikethrough(offset)Boolean|null擷取指定字元偏移的刪除線設定。
isUnderline()Boolean|null擷取底線設定。
isUnderline(offset)Boolean|null擷取指定字元偏移的底線設定。
merge()Text|null將元素與相同類型的前一個同層級元素合併。
removeFromParent()Text|null從父項移除元素。
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)

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

copy()

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

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

回攻員

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

editAsText()

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

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

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

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

// Insert two paragraphs separated by a paragraph containing an
// horizontal rule.
body.insertParagraph(0, 'An editAsText sample.');
body.insertHorizontalRule(0);
body.insertParagraph(0, 'An example.');

// Delete " sample.\n\n An" removing the horizontal rule in the process.
body.editAsText().deleteText(14, 25);

回攻員

Text:目前元素的文字版本


findText(searchPattern)

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

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

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

參數

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

回攻員

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

授權

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

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

findText(searchPattern, from)

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

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

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

參數

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

回攻員

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

授權

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

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

getAttributes()

擷取元素的屬性。

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

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

// Append a styled paragraph.
const par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);

// Retrieve the paragraph's attributes.
const atts = par.getAttributes();

// Log the paragraph attributes.
for (const att in atts) {
  Logger.log(`${att}:${atts[att]}`);
}

回攻員

Object:元素的屬性。

授權

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

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

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

參數

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

回攻員

Object:元素的屬性。

授權

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

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

getBackgroundColor()

擷取背景顏色設定。

回攻員

String|null:背景顏色,以 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/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);

參數

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

回攻員

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

授權

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

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

getFontFamily()

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

回攻員

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

參數

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

回攻員

String|null - 字型系列。

授權

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

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

getFontSize()

擷取字型大小設定。

回攻員

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

參數

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

回攻員

Number|null:字型大小。

授權

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

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

getForegroundColor()

擷取前景顏色設定。

回攻員

String|null:前景色,格式為 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/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);

參數

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

回攻員

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

授權

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

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

getLinkUrl()

擷取連結網址。

回攻員

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

授權

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

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

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

參數

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

回攻員

String|null:連結網址。

授權

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

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

getNextSibling()

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

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

回攻員

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

授權

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

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

getParent()

擷取元素的父項元素。

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

回攻員

ContainerElement|null:父項元素。

授權

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

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

getPreviousSibling()

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

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

回攻員

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

授權

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

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

getText()

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

回攻員

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

授權

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

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

getTextAlignment()

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

回攻員

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

授權

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

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

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

參數

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

回攻員

TextAlignment|null:文字對齊方式的類型,如果從未設定文字對齊方式,則為 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/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

getType()

擷取元素的 ElementType

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

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

// Obtain the first element in the active tab's body.

const firstChild = body.getChild(0);

// Use getType() to determine the element's type.
if (firstChild.getType() === DocumentApp.ElementType.PARAGRAPH) {
  Logger.log('The first element is a paragraph.');
} else {
  Logger.log('The first element is not a paragraph.');
}

回攻員

ElementType:元素類型。

授權

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

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

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

isAtDocumentEnd()

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

回攻員

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

授權

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

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

isBold()

擷取粗體設定。

回攻員

Boolean|null:文字是否為粗體,如果元素包含這項屬性的多個值,則為空值

授權

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

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

參數

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

回攻員

Boolean|null:粗體設定。

授權

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

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

isItalic()

擷取斜體設定。

回攻員

Boolean|null:文字是否為斜體,如果元素包含這項屬性的多個值,則為空值

授權

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

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

參數

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

回攻員

Boolean|null:斜體設定。

授權

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

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

isStrikethrough()

擷取刪除線設定。

回攻員

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

參數

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

回攻員

Boolean|null - 刪除線設定。

授權

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

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

isUnderline()

擷取底線設定。

回攻員

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

參數

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

回攻員

Boolean|null - 底線設定。

授權

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

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

merge()

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

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

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

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

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

// Example 2: Merge table cells
// Create a two-dimensional array containing the table's cell contents.
const cells = [
  ['Row 1, Cell 1', 'Row 1, Cell 2'],
  ['Row 2, Cell 1', 'Row 2, Cell 2'],
];
// Build a table from the array.
const table = body.appendTable(cells);
// Get the first row in the table.
const row = table.getRow(0);
// Get the two cells in this row.
const cell1 = row.getCell(0);
const cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
const merged = cell2.merge();

回攻員

Text|null:合併的元素。

授權

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

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

removeFromParent()

從父項移除元素。

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

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

回攻員

Text|null:已移除的元素。

授權

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

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

replaceText(searchPattern, replacement)

使用規則運算式,將指定文字模式的所有例項替換為指定替代字串。

搜尋模式會以字串形式傳遞,而非 JavaScript 規則運算式物件。因此,您需要逸出模式中的任何反斜線。

這個方法使用 Google 的 RE2 規則運算式程式庫,因此支援的語法有限。

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

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

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

參數

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

回攻員

Element:目前元素

授權

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

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

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

setAttributes(attributes)

設定元素的屬性。

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

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

// Define a custom paragraph style.
const style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
    DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;

// Append a plain paragraph.
const par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

參數

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

回攻員

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

setBackgroundColor(color)

設定背景顏色。

參數

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

回攻員

Text:目前元素

授權

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

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

setBold(bold)

設定粗體。

參數

名稱類型說明
boldBoolean粗體設定

回攻員

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

參數

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

回攻員

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

參數

名稱類型說明
startOffsetInteger文字範圍的起始位移。
endOffsetInclusiveInteger文字範圍的結束偏移。
fontFamilyNameString字型系列名稱,來自 Google 文件或 Google 字型的「字型」選單。

回攻員

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 的「字型」選單中取得

回攻員

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

setFontSize(size)

設定字型大小。

參數

名稱類型說明
sizeNumber字型大小

回攻員

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

setForegroundColor(color)

設定前景色。

參數

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

回攻員

Text:目前元素

授權

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

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

setItalic(italic)

設定斜體。

參數

名稱類型說明
italicBoolean斜體設定

回攻員

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

參數

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

回攻員

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

參數

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

回攻員

Text:目前的元素。

授權

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

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

setLinkUrl(url)

設定連結網址。

參數

名稱類型說明
urlString連結網址

回攻員

Text:目前元素

授權

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

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

setStrikethrough(strikethrough)

設定刪除線。

參數

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

回攻員

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

參數

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

回攻員

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

參數

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

回攻員

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

參數

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

回攻員

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

參數

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

回攻員

Text:目前元素

授權

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

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

setUnderline(underline)

設定底線。

參數

名稱類型說明
underlineBoolean底線設定

回攻員

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

參數

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

回攻員

Text:目前的元素。

授權

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

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