Class ConditionalFormatRuleBuilder

ConditionalFormatRuleBuilder

條件式格式規則的建構工具。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a number between 1 and 10.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberBetween(1, 10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

方法

方法傳回類型簡短說明
build()ConditionalFormatRule根據套用至建構工具的設定來建立條件式格式規則。
copy()ConditionalFormatRuleBuilder傳回採用這項規則設定的規則製作工具預設選項。
getBooleanCondition()BooleanCondition在這項規則使用時擷取規則的 BooleanCondition 資訊 布林條件條件。
getGradientCondition()GradientCondition如果這項規則,就擷取規則的 GradientCondition 資訊 會參考梯度條件
getRanges()Range[]擷取套用這項條件式格式規則的範圍。
setBackground(color)ConditionalFormatRuleBuilder設定條件式格式規則格式的背景顏色。
setBackgroundObject(color)ConditionalFormatRuleBuilder設定條件式格式規則格式的背景顏色。
setBold(bold)ConditionalFormatRuleBuilder為條件式格式規則的格式設定粗體文字。
setFontColor(color)ConditionalFormatRuleBuilder設定條件式格式規則格式的字型顏色。
setFontColorObject(color)ConditionalFormatRuleBuilder設定條件式格式規則格式的字型顏色。
setGradientMaxpoint(color)ConditionalFormatRuleBuilder清除條件格式規則的漸層最大值,然後改用最大值 值。
setGradientMaxpointObject(color)ConditionalFormatRuleBuilder清除條件格式規則的漸層最大值,然後改用最大值 值。
setGradientMaxpointObjectWithValue(color, type, value)ConditionalFormatRuleBuilder設定條件式格式規則的漸層 maxpoint 欄位。
setGradientMaxpointWithValue(color, type, value)ConditionalFormatRuleBuilder設定條件式格式規則的漸層 maxpoint 欄位。
setGradientMidpointObjectWithValue(color, type, value)ConditionalFormatRuleBuilder設定條件式格式規則的漸層中點欄位。
setGradientMidpointWithValue(color, type, value)ConditionalFormatRuleBuilder設定條件式格式規則的漸層中點欄位。
setGradientMinpoint(color)ConditionalFormatRuleBuilder清除條件格式規則的漸層最小點值,改採用最小值 值。
setGradientMinpointObject(color)ConditionalFormatRuleBuilder清除條件格式規則的漸層最小點值,改採用最小值 值。
setGradientMinpointObjectWithValue(color, type, value)ConditionalFormatRuleBuilder設定條件格式規則的漸層最小點欄位。
setGradientMinpointWithValue(color, type, value)ConditionalFormatRuleBuilder設定條件格式規則的漸層最小點欄位。
setItalic(italic)ConditionalFormatRuleBuilder將條件式格式規則的格式設為斜體。
setRanges(ranges)ConditionalFormatRuleBuilder設定一或多個套用這項條件式格式規則的範圍。
setStrikethrough(strikethrough)ConditionalFormatRuleBuilder為條件式格式規則的格式設定文字刪除線。
setUnderline(underline)ConditionalFormatRuleBuilder為條件式格式規則的格式設定文字底線。
whenCellEmpty()ConditionalFormatRuleBuilder設定儲存格空白時要觸發的條件式格式規則。
whenCellNotEmpty()ConditionalFormatRuleBuilder設定儲存格非空白時要觸發的條件式格式規則。
whenDateAfter(date)ConditionalFormatRuleBuilder設定在日期晚於指定值時觸發的條件格式規則。
whenDateAfter(date)ConditionalFormatRuleBuilder設定在日期晚於指定相對日期時觸發的條件格式規則。
whenDateBefore(date)ConditionalFormatRuleBuilder設定在日期早於指定日期時觸發的條件式格式規則。
whenDateBefore(date)ConditionalFormatRuleBuilder設定在日期早於指定相對日期時觸發的條件格式規則。
whenDateEqualTo(date)ConditionalFormatRuleBuilder設定在日期等於指定日期時觸發的條件格式規則。
whenDateEqualTo(date)ConditionalFormatRuleBuilder設定在日期等於指定相對日期時觸發的條件格式規則。
whenFormulaSatisfied(formula)ConditionalFormatRuleBuilder設定在指定公式計算結果為 true 時要觸發的條件式格式規則。
whenNumberBetween(start, end)ConditionalFormatRuleBuilder設定條件式格式規則,在數字介於數字之間或為二者時觸發 指定的值。
whenNumberEqualTo(number)ConditionalFormatRuleBuilder設定在數字等於指定值時觸發的條件格式規則。
whenNumberGreaterThan(number)ConditionalFormatRuleBuilder設定在數字大於指定值時觸發的條件格式規則。
whenNumberGreaterThanOrEqualTo(number)ConditionalFormatRuleBuilder設定在數字大於或等於指定數值時觸發的條件格式規則 值。
whenNumberLessThan(number)ConditionalFormatRuleBuilder設定在數字小於指定值時觸發的條件式條件式格式規則 值。
whenNumberLessThanOrEqualTo(number)ConditionalFormatRuleBuilder設定在數字小於或等於指定值時觸發的條件格式規則 值。
whenNumberNotBetween(start, end)ConditionalFormatRuleBuilder設定條件式格式規則,讓系統在數值介於數字之間 (且非介於) 之間時觸發 兩個指定值。
whenNumberNotEqualTo(number)ConditionalFormatRuleBuilder設定在數字不等於指定值時觸發的條件格式規則。
whenTextContains(text)ConditionalFormatRuleBuilder設定當輸入內容含有指定值時,要觸發的條件式格式規則。
whenTextDoesNotContain(text)ConditionalFormatRuleBuilder設定在輸入值不包含指定值時觸發的條件式格式規則 值。
whenTextEndsWith(text)ConditionalFormatRuleBuilder設定當輸入內容結尾為指定值時要觸發的條件式格式規則。
whenTextEqualTo(text)ConditionalFormatRuleBuilder設定當輸入值等於指定值時,要觸發的條件式格式規則。
whenTextStartsWith(text)ConditionalFormatRuleBuilder設定當輸入值開頭是指定值時要觸發的條件式格式規則。
withCriteria(criteria, args)ConditionalFormatRuleBuilder將條件式格式規則設為由 BooleanCriteria 值定義的條件。 通常取自下列項目的 criteriaarguments 現有規則。

內容詳盡的說明文件

build()

根據套用至建構工具的設定來建立條件式格式規則。

回攻員

ConditionalFormatRule:條件式格式規則的表示法


copy()

傳回採用這項規則設定的規則製作工具預設選項。

回攻員

ConditionalFormatRuleBuilder:根據這項規則設定建立的建構工具


getBooleanCondition()

在這項規則使用時擷取規則的 BooleanCondition 資訊 布林條件條件。否則會傳回 null

// Log the boolean criteria type of the first conditional format rules of a sheet.
var rule = SpreadsheetApp.getActiveSheet().getConditionalFormatRules()[0];
var booleanCondition = rule.getBooleanCondition();
if (booleanCondition != null) {
  Logger.log(booleanCondition.getCriteriaType());
}

回攻員

BooleanCondition:布林條件物件;如果規則未使用布林值,則為 null 值。


getGradientCondition()

如果這項規則,就擷取規則的 GradientCondition 資訊 會參考梯度條件否則會傳回 null

// Log the gradient minimum color of the first conditional format rule of a sheet.
var rule = SpreadsheetApp.getActiveSheet().getConditionalFormatRules()[0];
var gradientCondition = rule.getGradientCondition();
if (gradientCondition != null) {
  // Assume the color has ColorType.RGB.
  Logger.log(gradientCondition.getMinColorObject().asRgbColor().asHexString());
}

回攻員

GradientCondition:漸層條件物件;如果規則未使用漸層,則為 null 值。


getRanges()

擷取套用這項條件式格式規則的範圍。

// Log each range of the first conditional format rule of a sheet.
var rule = SpreadsheetApp.getActiveSheet().getConditionalFormatRules()[0];
var ranges = rule.getRanges();
for (var i = 0; i < ranges.length; i++) {
  Logger.log(ranges[i].getA1Notation());
}

回攻員

Range[]:這項條件式格式規則的套用範圍。


setBackground(color)

設定條件式格式規則格式的背景顏色。正在傳入 null 會從規則中移除背景顏色格式設定。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color to red if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString所需的顏色,或是 null 要清除的顏色。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setBackgroundObject(color)

設定條件式格式規則格式的背景顏色。正在傳入 null 會從規則中移除背景顏色格式設定。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color to theme background color if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var color = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.BACKGROUND)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setBackground(color)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要清除的顏色物件或 null

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setBold(bold)

為條件式格式規則的格式設定粗體文字。如果 boldtrue, 如果符合條件,規則將以粗體顯示。如為 false,規則會移除任何現有 如果符合條件則以粗體顯示。傳入 null 會將粗體格式設定從 。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn their
// text bold if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setBold(true)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
boldBoolean是否在符合格式條件時,是否要將文字設為粗體。null移除這項設定。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setFontColor(color)

設定條件式格式規則格式的字型顏色。傳入 null 會從中移除 輸入規則的字型顏色格式設定

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their font
// color to red if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setFontColor("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString所需的顏色,或是 null 要清除的顏色。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setFontColorObject(color)

設定條件式格式規則格式的字型顏色。傳入 null 會從中移除 輸入規則的字型顏色格式設定

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their font
// color to theme text color if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var color = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.TEXT)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setFontColor(color)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要清除的顏色物件或 null

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setGradientMaxpoint(color)

清除條件格式規則的漸層最大值,然後改用最大值 值。此外,也會將漸層的最大點顏色設為輸入顏色。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere between white and red, based on their values in comparison to
// the ranges minimum and maximum values.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpoint("#FF0000")
    .setGradientMinpoint("#FFFFFF")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString要設定的最大點顏色。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setGradientMaxpointObject(color)

清除條件格式規則的漸層最大值,然後改用最大值 值。此外,也會將漸層的最大點顏色設為輸入顏色。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere between theme text and background colors, based on their values
// in comparison to the ranges minimum and maximum values.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var textColor = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.TEXT)
    .build();
var backgroundColor = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.BACKGROUND)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpoint(textColor)
    .setGradientMinpoint(backgroundColor)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要設定的最大點顏色物件。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setGradientMaxpointObjectWithValue(color, type, value)

設定條件式格式規則的漸層 maxpoint 欄位。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere from theme accent 1, accent 2 to accent 3 colors, based on their
// values in comparison to the values 0, 50, and 100.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var color1 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
var color2 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2)
    .build();
var color3 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpointWithValue(color1, SpreadsheetApp.InterpolationType.NUMBER, "100")
    .setGradientMidpointWithValue(color2, SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue(color3, SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要設定的最大點顏色。
typeInterpolationType要設定的最大點內插類型。
valueString要設定的最大值點值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setGradientMaxpointWithValue(color, type, value)

設定條件式格式規則的漸層 maxpoint 欄位。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere from red green to blue, based on their values in comparison to
// the values 0, 50, and 100.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpointWithValue("#0000FF", SpreadsheetApp.InterpolationType.NUMBER, "100")
    .setGradientMidpointWithValue("#00FF00", SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue("#FF0000", SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString要設定的最大點顏色。
typeInterpolationType要設定的最大點內插類型。
valueString要設定的最大值點值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setGradientMidpointObjectWithValue(color, type, value)

設定條件式格式規則的漸層中點欄位。清除所有中點欄位 如果傳遞的內插類型為 null

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere from theme accent 1 to accent 2 to accent 3 colors, based on
// their values in comparison to the values 0, 50, and 100.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var color1 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
var color2 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2)
    .build();
var color3 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpointWithValue(color1, SpreadsheetApp.InterpolationType.NUMBER, "100")
    .setGradientMidpointWithValue(color2, SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue(color3, SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要設定的中點顏色。
typeInterpolationType要設定的中點內插類型,或將 null 設為清除。
valueString要設定的中點值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setGradientMidpointWithValue(color, type, value)

設定條件式格式規則的漸層中點欄位。清除所有中點欄位 如果傳遞的內插類型為 null

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere from red green to blue, based on their values in comparison to
// the values 0, 50, and 100.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpointWithValue("#0000FF", SpreadsheetApp.InterpolationType.NUMBER, "100")
    .setGradientMidpointWithValue("#00FF00", SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue("#FF0000", SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString要設定的中點顏色。
typeInterpolationType要設定的中點內插類型,或將 null 設為清除。
valueString要設定的中點值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setGradientMinpoint(color)

清除條件格式規則的漸層最小點值,改採用最小值 值。此外,也會將漸層的最小點顏色設定為輸入顏色。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere between white and red, based on their values in comparison to
// the ranges minimum and maximum values.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpoint("#FF0000")
    .setGradientMinpoint("#FFFFFF")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString要設定的最小點顏色。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setGradientMinpointObject(color)

清除條件格式規則的漸層最小點值,改採用最小值 值。此外,也會將漸層的最小點顏色設定為輸入顏色。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere between theme text and background colors, based on their values
// in comparison to the ranges minimum and maximum values.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var textColor = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.TEXT)
    .build();
var backgroundColor = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.BACKGROUND)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpoint(textColor)
    .setGradientMinpoint(backgroundColor)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要設定的最小點顏色物件。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setGradientMinpointObjectWithValue(color, type, value)

設定條件格式規則的漸層最小點欄位。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere from theme accent 1 to accent 2 to accent 3 colors, based on
// their values in comparison to the values 0, 50, and 100.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var color1 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT1)
    .build();
var color2 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT2)
    .build();
var color3 = SpreadsheetApp.newColor()
    .setThemeColor(SpreadsheetApp.ThemeColorType.ACCENT3)
    .build();
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpointWithValue(color1, SpreadsheetApp.InterpolationType.NUMBER, "100")
    .setGradientMidpointWithValue(color2, SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue(color3, SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorColor要設定的最小點顏色。
typeInterpolationType要設定的最小點內插類型。
valueString要設定的最小點值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具。


setGradientMinpointWithValue(color, type, value)

設定條件格式規則的漸層最小點欄位。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to set their
// background color somewhere from red to green to blue, based on their values in comparison to
// the values 0, 50, and 100.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .setGradientMaxpointWithValue("#0000FF", SpreadsheetApp.InterpolationType.NUMBER, "100")
    .setGradientMidpointWithValue("#00FF00", SpreadsheetApp.InterpolationType.NUMBER, "50")
    .setGradientMinpointWithValue("#FF0000", SpreadsheetApp.InterpolationType.NUMBER, "0")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
colorString要設定的最小點顏色。
typeInterpolationType要設定的最小點內插類型。
valueString要設定的最小點值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setItalic(italic)

將條件式格式規則的格式設為斜體。如果 italictrue, 規則會在符合條件時將文字設為斜體。如果 false,規則會將任何 現有的斜體文字傳入 null 會移除斜體 格式設定。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn their
// text italic if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setItalic(true)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
italicBoolean在符合格式條件時,是否要將文字設為斜體; null移除這項設定。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setRanges(ranges)

設定一或多個套用這項條件式格式規則的範圍。這項作業 會取代任何現有範圍設定空白陣列會清除任何現有範圍。規則必須 至少須有一個範圍

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 and range D4:F6
// to turn red if they contain a number between 1 and 10.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeOne = sheet.getRange("A1:B3");
var rangeTwo = sheet.getRange("D4:F6");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberBetween(1, 10)
    .setBackground("#FF0000")
    .setRanges([rangeOne, rangeTwo])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
rangesRange[]這項條件式格式規則的套用範圍。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setStrikethrough(strikethrough)

為條件式格式規則的格式設定文字刪除線。如果 strikethroughtrue,在條件符合的情況下,規則會加上刪除線文字;如果 false,規則 在條件符合時,移除所有現有的刪除線格式設定。正在傳入 null 就會移除規則的刪除線格式設定

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to strikethrough
// their text if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setStrikethrough(true)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
strikethroughBoolean如果格式條件為 met;null移除這項設定。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


setUnderline(underline)

為條件式格式規則的格式設定文字底線。如果 underlinetrue,在條件符合時,規則會為文字加上底線。如果 false,規則會將任何 會在條件符合時加上底線傳入 null 會移除底線 格式設定。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to underline
// their text if the cell has text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setUnderline(true)
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
underlineBoolean是否在符合格式條件時替文字加上底線; null移除這項設定。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenCellEmpty()

設定儲存格空白時要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they are empty.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenCellEmpty()
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenCellNotEmpty()

設定儲存格非空白時要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they are not empty.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenCellNotEmpty()
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenDateAfter(date)

設定在日期晚於指定值時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a date after 11/4/1993.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenDateAfter(new Date("11/4/1993"))
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
dateDate最新日期。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenDateAfter(date)

設定在日期晚於指定相對日期時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a date after today.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenDateAfter(SpreadsheetApp.RelativeDate.TODAY)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
dateRelativeDate相對於所選日期類型的最新日期。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenDateBefore(date)

設定在日期早於指定日期時觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a date before 11/4/1993.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenDateBefore(new Date("11/4/1993"))
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
dateDate無法接受的最早日期。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenDateBefore(date)

設定在日期早於指定相對日期時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a date before today.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenDateBefore(SpreadsheetApp.RelativeDate.TODAY)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
dateRelativeDate相對於所選日期類型的最新日期。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenDateEqualTo(date)

設定在日期等於指定日期時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain the date 11/4/1993.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenDateEqualTo(new Date("11/4/1993"))
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
dateDate唯一可接受的日期。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenDateEqualTo(date)

設定在日期等於指定相對日期時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain todays date.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenDateEqualTo(SpreadsheetApp.RelativeDate.TODAY)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
dateRelativeDate相對於所選日期類型的最新日期。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenFormulaSatisfied(formula)

設定在指定公式計算結果為 true 時要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they satisfy the condition "=EQ(B4, C3)".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenFormulaSatisfied("=EQ(B4, C3)")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
formulaString如果輸入有效,系統評估為 true 的自訂公式。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberBetween(start, end)

設定條件式格式規則,在數字介於數字之間或為二者時觸發 指定的值。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a number between 1 and 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberBetween(1, 10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
startNumber可接受的最低值。
endNumber可接受的最高值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberEqualTo(number)

設定在數字等於指定值時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain the number 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberEqualTo(10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
numberNumber唯一可接受的值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberGreaterThan(number)

設定在數字大於指定值時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red
// if they contain a number greater than 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberGreaterThan(10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
numberNumber允許的最高值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberGreaterThanOrEqualTo(number)

設定在數字大於或等於指定數值時觸發的條件格式規則 值。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a number greater than or equal to 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberGreaterThanOrEqualTo(10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
numberNumber可接受的最低值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberLessThan(number)

設定在數字小於指定值時觸發的條件式條件式格式規則 值。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a number less than 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberLessThan(10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
numberNumber無法接受的最低值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberLessThanOrEqualTo(number)

設定在數字小於或等於指定值時觸發的條件格式規則 值。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a number less than or equal to 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberLessThanOrEqualTo(10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
numberNumber可接受的最高值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberNotBetween(start, end)

設定條件式格式規則,讓系統在數值介於數字之間 (且非介於) 之間時觸發 兩個指定值。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain a number not between 1 and 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberNotBetween(1, 10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
startNumber無法接受的最低值。
endNumber允許的最高值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenNumberNotEqualTo(number)

設定在數字不等於指定值時觸發的條件格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they don't contain the number 10.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenNumberNotEqualTo(10)
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
numberNumber唯一不接受的值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenTextContains(text)

設定當輸入內容含有指定值時,要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they contain the text "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextContains("hello")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
textString輸入內容必須包含的值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenTextDoesNotContain(text)

設定在輸入值不包含指定值時觸發的條件式格式規則 值。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they don't contain the text "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextDoesNotContain("hello")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
textString輸入內容不得包含的值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenTextEndsWith(text)

設定當輸入內容結尾為指定值時要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they end with the text "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEndsWith("hello")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
textString要與字串結尾比較的文字。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenTextEqualTo(text)

設定當輸入值等於指定值時,要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they have text equal to "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextEqualTo("hello")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
textString唯一可接受的值。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


whenTextStartsWith(text)

設定當輸入值開頭是指定值時要觸發的條件式格式規則。

// Adds a conditional format rule to a sheet that causes cells in range A1:B3 to turn red if
// they start with the text "hello".

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:B3");
var rule = SpreadsheetApp.newConditionalFormatRule()
    .whenTextStartsWith("hello")
    .setBackground("#FF0000")
    .setRanges([range])
    .build();
var rules = sheet.getConditionalFormatRules();
rules.push(rule);
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
textString要與字串開頭比較的文字。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具


withCriteria(criteria, args)

將條件式格式規則設為由 BooleanCriteria 值定義的條件。 通常取自下列項目的 criteriaarguments 現有規則。

// Adds a new conditional format rule that is a copy of the first active
// conditional format rule, except it instead sets its cells to have a black
// background color.

var sheet = SpreadsheetApp.getActiveSheet();
var rules = sheet.getConditionalFormatRules();
var booleanCondition = rules[0].getBooleanCondition();
if (booleanCondition != null) {
  var rule = SpreadsheetApp.newConditionalFormatRule()
      .withCriteria(booleanCondition.getCriteriaType(),
         booleanCondition.getCriteriaValues())
      .setBackground("#000000")
      .setRanges(rule.getRanges())
      .build();
  rules.push(rule);
}
sheet.setConditionalFormatRules(rules);

參數

名稱類型說明
criteriaBooleanCriteria條件式格式條件的類型。
argsObject[]適合該條件類型的引數陣列;可能會引發 其類型符合上述對應的 when...() 方法。

回攻員

ConditionalFormatRuleBuilder:用於鏈結的建構工具