Class TextStyle

TextStyle

文本的样式。

如果相应的 TextRange 跨越多个文本运行,并且这些运行对于被调用的读取方法具有不同的值,则此类中的读取方法会返回 null。为避免出现这种情况,请使用 TextRange.getRuns() 方法返回的 TextRange 查询文本样式。

如果您使用方法修改文本在形状中的调整方式,则应用于文本样式的所有自动调整设置会被停用。

方法

方法返回类型简介
getBackgroundColor()Color返回文本的背景颜色;如果文本有多种样式,则返回 null
getBaselineOffset()TextBaselineOffset返回文本相对于其正常位置的垂直偏移量;如果文本有多种样式,则返回 null
getFontFamily()String返回文本的字体系列,如果文本有多种样式,则返回 null
getFontSize()Number返回文本的字体大小(以点为单位);如果文本有多种样式,则返回 null
getFontWeight()Integer返回文本的字体粗细,如果文本有多种样式,则返回 null
getForegroundColor()Color返回文本的前景颜色;如果文本有多种样式,则返回 null
getLink()Link在文本上返回 Link;如果没有链接、链接位于文本的一部分或有多个链接,则返回 null
hasLink()Boolean如果文本上有链接,则返回 true;如果文本中没有链接,则返回 false;如果链接位于文本的一部分或有多个链接,则返回 null
isBackgroundTransparent()Boolean如果文本背景是透明的,则返回 true;如果文本背景不透明,则返回 false;如果文本有多种样式,则返回 null
isBold()Boolean如果文本呈现为粗体,则返回 true;如果文本呈现为粗体,则返回 false;如果文本有多种样式,则返回 null
isItalic()Boolean如果文本设为斜体,则返回 true;如果文本设为斜体,则返回 false;如果文本有多种样式,则返回 null
isSmallCaps()Boolean如果文本使用的是大写字母,则返回 true;如果文本采用小写样式,则返回 false;如果文本有多种样式,则返回 null
isStrikethrough()Boolean如果文本带有删除线,则返回 true;如果文本没有删除线,则返回 false;如果文本有多种样式,则返回 null
isUnderline()Boolean如果文本带有下划线,则返回 true;如果文本没有下划线,则返回 false;如果文本有多种样式,则返回 null
removeLink()TextStyle移除 Link
setBackgroundColor(color)TextStyle设置文本的背景颜色。
setBackgroundColor(red, green, blue)TextStyle将文本的背景颜色设为指定的 RGB 值(0 到 255)。
setBackgroundColor(hexColor)TextStyle将文本的背景颜色设为指定的十六进制颜色字符串。
setBackgroundColor(color)TextStyle将文本的背景颜色设为指定的 ThemeColorType
setBackgroundColorTransparent()TextStyle将文本的背景颜色设为透明。
setBaselineOffset(offset)TextStyle设置文本相对于其正常位置的垂直偏移量。
setBold(bold)TextStyle设置是否应将文本渲染为粗体。
setFontFamily(fontFamily)TextStyle设置文本的字体系列。
setFontFamilyAndWeight(fontFamily, fontWeight)TextStyle设置文本的字体系列和粗细。
setFontSize(fontSize)TextStyle设置文本的字体大小(以点为单位)。
setForegroundColor(foregroundColor)TextStyle设置文本的前景颜色。
setForegroundColor(red, green, blue)TextStyle将文本的前景颜色设为指定的 RGB 值(0 到 255)。
setForegroundColor(hexColor)TextStyle将文本的前景颜色设为指定的十六进制颜色字符串。
setForegroundColor(color)TextStyle将文本的前景颜色设为指定的 ThemeColorType
setItalic(italic)TextStyle设置文本是否设为斜体。
setLinkSlide(slideIndex)TextStyle使用幻灯片索引(从零开始)将 Link 设为指定的 Slide
setLinkSlide(slide)TextStyleLink 设为指定的 Slide,链接由指定的幻灯片 ID 设置。
setLinkSlide(slidePosition)TextStyle使用幻灯片的相对位置将 Link 设为指定的 Slide
setLinkUrl(url)TextStyleLink 设置为指定的非空网址字符串。
setSmallCaps(smallCaps)TextStyle设置文本是否以较小的大写字母呈现。
setStrikethrough(strikethrough)TextStyle设置是否删除文本。
setUnderline(underline)TextStyle设置文本是否加下划线。

详细文档

getBackgroundColor()

返回文本的背景颜色;如果文本有多种样式,则返回 null

弃踢回攻

Color

授权

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

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

getBaselineOffset()

返回文本相对于其正常位置的垂直偏移量;如果文本有多种样式,则返回 null

弃踢回攻

TextBaselineOffset

授权

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

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

getFontFamily()

返回文本的字体系列,如果文本有多种样式,则返回 null

弃踢回攻

String

授权

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

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

getFontSize()

返回文本的字体大小(以点为单位);如果文本有多种样式,则返回 null

弃踢回攻

Number

授权

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

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

getFontWeight()

返回文本的字体粗细,如果文本有多种样式,则返回 null

权重是 100 到 900(含)之间的 100 的倍数。此范围对应于 CSS 2.1 规范第 15.6 节中描述的数值,不允许使用非数值。权重大于或等于 700 会被视为粗体,在这种情况下,isBold() 会返回 true。默认值为 400(“normal”)。

弃踢回攻

Integer

授权

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

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

getForegroundColor()

返回文本的前景颜色;如果文本有多种样式,则返回 null

弃踢回攻

Color

授权

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

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

在文本上返回 Link;如果没有链接、链接位于文本的一部分或有多个链接,则返回 null。调用 hasLink() 以确定文本是否没有链接。

var textLink = shape.getText().getTextStyle().getLink();
if (textLink != null) {
  Logger.log('Shape text has a link of type: ' + textLink.getLinkType());
}

弃踢回攻

Link

授权

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

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

如果文本中有链接,则返回 true;如果文本中没有链接,则返回 false;如果链接位于文本的一部分或有多个链接,则返回 null

链接不能用换行符设置。因此,如果 TextRange 包含换行符,则此方法始终返回 nullfalse

弃踢回攻

Boolean

授权

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

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

isBackgroundTransparent()

如果文本背景是透明的,则返回 true;如果文本背景不透明,则返回 false;如果文本有多种样式,则返回 null

弃踢回攻

Boolean

授权

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

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

isBold()

如果文本呈现为粗体,则返回 true;如果文本呈现为粗体,则返回 false;如果文本有多种样式,则返回 null

弃踢回攻

Boolean

授权

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

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

isItalic()

如果文本设为斜体,则返回 true;如果文本设为斜体,则返回 false;如果文本有多种样式,则返回 null

弃踢回攻

Boolean

授权

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

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

isSmallCaps()

如果文本使用的是大写字母,则返回 true;如果文本采用小写样式,则返回 false;如果文本有多种样式,则返回 null

弃踢回攻

Boolean

授权

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

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

isStrikethrough()

如果文本带有删除线,则返回 true;如果文本没有删除线,则返回 false;如果文本有多种样式,则返回 null

弃踢回攻

Boolean

授权

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

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

isUnderline()

如果文本带有下划线,则返回 true;如果文本没有下划线,则返回 false;如果文本有多种样式,则返回 null

弃踢回攻

Boolean

授权

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

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

移除 Link

删除链接会删除文本的超链接前景颜色和下划线样式。如果可能,系统会应用这些样式以匹配链接前面的文本。

text.getTextStyle().removeLink();

弃踢回攻

TextStyle


setBackgroundColor(color)

设置文本的背景颜色。

参数

名称类型说明
colorColor

弃踢回攻

TextStyle

授权

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

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

setBackgroundColor(red, green, blue)

将文本的背景颜色设为指定的 RGB 值(0 到 255)。

参数

名称类型说明
redInteger
greenInteger
blueInteger

弃踢回攻

TextStyle

授权

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

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

setBackgroundColor(hexColor)

将文本的背景颜色设为指定的十六进制颜色字符串。

十六进制字符串必须采用“#RRGGBB”格式。例如,粉色将表示为“#FFC0CB”。

参数

名称类型说明
hexColorString

弃踢回攻

TextStyle

授权

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

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

setBackgroundColor(color)

将文本的背景颜色设为指定的 ThemeColorType

参数

名称类型说明
colorThemeColorType

弃踢回攻

TextStyle

授权

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

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

setBackgroundColorTransparent()

将文本的背景颜色设为透明。

弃踢回攻

TextStyle

授权

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

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

setBaselineOffset(offset)

设置文本相对于其正常位置的垂直偏移量。

参数

名称类型说明
offsetTextBaselineOffset

弃踢回攻

TextStyle

授权

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

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

setBold(bold)

设置是否应将文本呈现为粗体。

更改文字的粗细程度会更新用于渲染文字的字体粗细。

参数

名称类型说明
boldBoolean

弃踢回攻

TextStyle

授权

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

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

setFontFamily(fontFamily)

设置文本的字体系列。

参数

名称类型说明
fontFamilyString

弃踢回攻

TextStyle

授权

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

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

setFontFamilyAndWeight(fontFamily, fontWeight)

设置文本的字体系列和粗细。

权重是 100 到 900(含)之间的 100 的倍数。此范围对应于 CSS 2.1 规范第 15.6 节中描述的数值,不允许使用非数值。权重大于或等于 700 会被视为粗体,在这种情况下,isBold() 会返回 true。默认值为 400(“normal”)。

参数

名称类型说明
fontFamilyString
fontWeightInteger

弃踢回攻

TextStyle

授权

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

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

setFontSize(fontSize)

设置文本的字体大小(以点为单位)。

参数

名称类型说明
fontSizeNumber

弃踢回攻

TextStyle

授权

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

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

setForegroundColor(foregroundColor)

设置文本的前景颜色。

参数

名称类型说明
foregroundColorColor

弃踢回攻

TextStyle

授权

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

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

setForegroundColor(red, green, blue)

将文本的前景颜色设为指定的 RGB 值(0 到 255)。

参数

名称类型说明
redInteger
greenInteger
blueInteger

弃踢回攻

TextStyle

授权

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

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

setForegroundColor(hexColor)

将文本的前景颜色设为指定的十六进制颜色字符串。

十六进制字符串必须采用“#RRGGBB”格式。例如,粉色将表示为“#FFC0CB”。

参数

名称类型说明
hexColorString

弃踢回攻

TextStyle

授权

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

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

setForegroundColor(color)

将文本的前景颜色设为指定的 ThemeColorType

参数

名称类型说明
colorThemeColorType

弃踢回攻

TextStyle

授权

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

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

setItalic(italic)

设置文本是否设为斜体。

参数

名称类型说明
italicBoolean

弃踢回攻

TextStyle

授权

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

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

setLinkSlide(slideIndex)

使用幻灯片的索引(从零开始)将 Link 设为指定的 Slide

设置链接会更改要添加下划线的文本样式,并将前景颜色设置为 ThemeColorType.HYPERLINK。这可以通过 setForegroundColor(hexColor)setUnderline(underline) 进行更改。

由于无法针对换行符设置链接,因此 TextRange 中的换行符会被忽略。

// Set a link to the first slide of the presentation.
text.getTextStyle().setLinkSlide(0);

参数

名称类型说明
slideIndexInteger幻灯片的索引(从零开始)。

弃踢回攻

TextStyle

授权

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

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

setLinkSlide(slide)

Link 设为指定的 Slide,链接由指定的幻灯片 ID 设置。

设置链接会更改要添加下划线的文本样式,并将前景颜色设置为 ThemeColorType.HYPERLINK。这可以通过 setForegroundColor(hexColor)setUnderline(underline) 进行更改。

由于无法针对换行符设置链接,因此 TextRange 中的换行符会被忽略。

// Set a link to the first slide of the presentation.
var slide = presentation.getSlides()[0];
text.getTextStyle().setLinkSlide(slide);

参数

名称类型说明
slideSlide要关联的 Slide

弃踢回攻

TextStyle

授权

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

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

setLinkSlide(slidePosition)

使用幻灯片的相对位置将 Link 设为指定的 Slide

设置链接会更改要添加下划线的文本样式,并将前景颜色设置为 ThemeColorType.HYPERLINK。这可以通过 setForegroundColor(hexColor)setUnderline(underline) 进行更改。

由于无法针对换行符设置链接,因此 TextRange 中的换行符会被忽略。

// Set a link to the first slide of the presentation.
text.getTextStyle().setLinkSlide(SlidesApp.SlidePosition.FIRST_SLIDE);

参数

名称类型说明
slidePositionSlidePosition相对 SlidePosition

弃踢回攻

TextStyle

授权

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

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

setLinkUrl(url)

Link 设置为指定的非空网址字符串。

设置链接会更改要添加下划线的文本样式,并将前景颜色设置为 ThemeColorType.HYPERLINK。这可以通过 setForegroundColor(hexColor)setUnderline(underline) 进行更改。

由于无法针对换行符设置链接,因此 TextRange 中的换行符会被忽略。

// Set a link to the URL.
text.getTextStyle().setLinkUrl("https://slides.google.com");

参数

名称类型说明
urlString网址字符串。

弃踢回攻

TextStyle

授权

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

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

setSmallCaps(smallCaps)

设置文本是否以较小的大写字母呈现。

参数

名称类型说明
smallCapsBoolean

弃踢回攻

TextStyle

授权

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

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

setStrikethrough(strikethrough)

设置是否删除文本。

参数

名称类型说明
strikethroughBoolean

弃踢回攻

TextStyle

授权

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

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

setUnderline(underline)

设置文本是否加下划线。

参数

名称类型说明
underlineBoolean

弃踢回攻

TextStyle

授权

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

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