Class Text

文本

表示富文本区域的元素。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要附加的文本。

返回

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/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要删除的最后一个字符的字符偏移量。

返回

Text - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

editAsText()

获取 Text 版本的当前元素,用于修改。

使用 editAsText 将元素内容处理为富文本。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);

返回

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 枚举中的一项。

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

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

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

// Log the paragraph attributes.
for (var att in atts) {
  Logger.log(att + ":" + atts[att]);
}

返回

Object - 元素的属性。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

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字符偏移量。

返回

Object - 元素的属性。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getBackgroundColor()

检索背景颜色设置。

返回

String - 背景颜色,采用 CSS 表示法(例如 '#ffffff')格式;如果元素包含此属性的多个值,则为 null

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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字符偏移量。

返回

String - 背景颜色,采用 CSS 表示法格式(例如 '#ffffff')。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getFontFamily()

检索字体系列设置。该名称可以是 Google 文档或 Google Fonts 中的“字体”菜单中的任意字体,并且区分大小写。getFontFamily()setFontFamily(fontFamilyName) 方法现在使用字体字符串名称而不是 FontFamily 枚举。虽然此枚举已废弃,但仍可用于与旧版脚本兼容。

返回

String - 字体系列;如果元素包含此属性的多个值,则为 null

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getFontFamily(offset)

用于检索在指定字符偏移量处的字体系列。该名称可以是文档或 Google Fonts 中的“字体”菜单中的任意字体,并且区分大小写。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字符偏移量。

返回

String - 字体系列。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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/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字符偏移量。

返回

Number - 字体大小。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getForegroundColor()

检索前景颜色设置。

返回

String - 前景色,采用 CSS 表示法的格式(例如 '#ffffff'),如果元素包含此属性的多个值,则为 null

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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字符偏移量。

返回

String - 前景色,采用 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/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字符偏移量。

返回

String - 链接网址。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getNextSibling()

检索该元素的下一个同级元素。

下一个兄弟姐妹具有相同的父元素,并遵循当前元素。

返回

Element - 下一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getParent()

检索元素的父元素。

父元素包含当前元素。

返回

ContainerElement - 父元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

getPreviousSibling()

检索该元素的之前同级元素。

前面的同级元素具有相同的父元素,并且位于当前元素之前。

返回

Element - 上一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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字符的偏移量。

返回

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

返回

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

返回

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要插入的文本。

返回

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/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字符偏移量。

返回

Boolean - 粗体设置。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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/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字符偏移量。

返回

Boolean - 斜体设置。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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/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字符偏移量。

返回

Boolean - 删除线设置。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • 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/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字符偏移量。

返回

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

返回

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

返回

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要用作替换文字的文字

返回

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

返回

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

返回

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

返回

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/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粗体设置。

返回

Text - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setFontFamily(startOffset, endOffsetInclusive, fontFamilyName)

设置指定字符范围的字体系列。该名称可以是 Google 文档或 Google Fonts 中的“字体”菜单中的任意字体,并且区分大小写。无法识别的字体名称将呈现为 Mainline。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 的“字体”菜单。

返回

Text - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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

setFontFamily(fontFamilyName)

设置字体系列。该名称可以是 Google 文档或 Google Fonts 中的“字体”菜单中的任意字体,并且区分大小写。无法识别的字体名称将呈现为 Mainline。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/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字体大小。

返回

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

返回

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/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斜体设置。

返回

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链接网址。

返回

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/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删除线设置。

返回

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新的文本内容。

返回

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要应用的文本对齐方式类型。

返回

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要应用的文字对齐方式类型

返回

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/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下划线设置。

返回

Text - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

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