Class TextStyle

文本样式

文本的样式。

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

如果您使用的方法会修改文字在形状中的排布方式,则应用于文字样式的任何自动调整设置都会停用。

方法

方法返回类型简介
getBackgroundColor()Color|null返回文本的背景颜色,如果文本具有多种样式,则返回 null
getBaselineOffset()TextBaselineOffset|null返回文本相对于其正常位置的垂直偏移量;如果文本具有多种样式,则返回 null
getFontFamily()String|null返回文本的字体系列;如果文本具有多种样式,则返回 null
getFontSize()Number|null返回文本的字体大小(以磅为单位),如果文本具有多种样式,则返回 null
getFontWeight()Integer|null返回文本的字体粗细,如果文本具有多种样式,则返回 null
getForegroundColor()Color|null返回文本的前景色,如果文本具有多种样式,则返回 null
getLink()Link|null返回文本中的 Link,如果没有链接、链接位于部分文本中或有多个链接,则返回 null
hasLink()Boolean|null如果文本中存在链接,则返回 true;如果不存在,则返回 false;如果链接位于部分文本中或存在多个链接,则返回 null
isBackgroundTransparent()Boolean|null如果文本的背景是透明的,则返回 true;如果不是,则返回 false;如果文本具有多种样式,则返回 null
isBold()Boolean|null如果文本以粗体呈现,则返回 true;如果不是,则返回 false;如果文本具有多种样式,则返回 null
isItalic()Boolean|null如果文本为斜体,则返回 true;如果不是,则返回 false;如果文本具有多种样式,则返回 null
isSmallCaps()Boolean|null如果文本采用小型大写字母,则返回 true;否则返回 false;如果文本采用多种样式,则返回 null
isStrikethrough()Boolean|null如果文本带有删除线,则返回 true;如果文本不带删除线,则返回 false;如果文本具有多种样式,则返回 null
isUnderline()Boolean|null如果文本带有下划线,则返回 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|null

授权

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

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

getBaselineOffset()

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

返回

TextBaselineOffset|null

授权

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

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

getFontFamily()

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

返回

String|null

授权

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

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

getFontSize()

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

返回

Number|null

授权

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

  • 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(“正常”)。

返回

Integer|null

授权

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

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

getForegroundColor()

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

返回

Color|null

授权

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

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

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

const shape = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0];
const textLink = shape.getText().getTextStyle().getLink();
if (textLink != null) {
  Logger.log(`Shape text has a link of type: ${textLink.getLinkType()}`);
}

返回

Link|null

授权

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

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

如果文本中存在链接,则返回 true;如果不存在,则返回 false;如果链接位于部分文本中或存在多个链接,则返回 null

无法在换行符上设置链接。因此,如果 TextRange 包含换行符,此方法始终返回 nullfalse

返回

Boolean|null

授权

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

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

isBackgroundTransparent()

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

返回

Boolean|null

授权

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

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

isBold()

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

返回

Boolean|null

授权

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

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

isItalic()

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

返回

Boolean|null

授权

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

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

isSmallCaps()

如果文本采用小型大写字母,则返回 true;否则返回 false;如果文本采用多种样式,则返回 null

返回

Boolean|null

授权

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

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

isStrikethrough()

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

返回

Boolean|null

授权

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

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

isUnderline()

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

返回

Boolean|null

授权

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

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

移除 Link

移除链接会移除文本中的超链接前景色和下划线样式。如果可能,系统会应用这些样式,使链接与前面的文字保持一致。

const textRange = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0].getText();
textRange.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(“正常”)。

参数

名称类型说明
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.
const presentation = SlidesApp.getActivePresentation();
const slide = presentation.getSlides()[0];
const textRange = slide.getShapes()[0].getText();
textRange.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.
const presentation = SlidesApp.getActivePresentation();
const slide = presentation.getSlides()[0];
const textRange = slide.getShapes()[0].getText();
textRange.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.
const textRange = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0].getText();
textRange.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.
const textRange = SlidesApp.getActivePresentation().getSlides()[0].getShapes()[0].getText();
textRange.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