Class TextStyle

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 和 900) 之間的 100 倍數。這個範圍對應至 CSS 2.1 規格第 15.6 節所述的數值,且不允許非數值。如果權重大於或等於 700,系統會視為粗體,此時 isBold() 會傳回 true。預設值為 400 (「normal」)。

回攻員

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 和 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.
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)

將指定 SlideLink 設為指定投影片 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