Excel.ConditionalFormat class

封装条件格式的范围、格式、规则和其他属性的对象。 若要了解有关条件格式对象模型的详细信息,请阅读 将条件格式应用于 Excel 范围

扩展

属性

cellValue

如果当前条件格式为类型 CellValue ,则返回单元格值条件格式属性。

cellValueOrNullObject

如果当前条件格式为类型 CellValue ,则返回单元格值条件格式属性。

colorScale

如果当前条件格式为类型 ColorScale ,则返回色阶条件格式属性。

colorScaleOrNullObject

如果当前条件格式为类型 ColorScale ,则返回色阶条件格式属性。

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

custom

如果当前条件格式为自定义类型,则返回自定义条件格式属性。

customOrNullObject

如果当前条件格式为自定义类型,则返回自定义条件格式属性。

dataBar

如果当前条件格式为数据栏,则返回数据栏属性。

dataBarOrNullObject

如果当前条件格式为数据栏,则返回数据栏属性。

iconSet

如果当前条件格式为类型 IconSet ,则返回图标设置条件格式属性。

iconSetOrNullObject

如果当前条件格式为类型 IconSet ,则返回图标设置条件格式属性。

id

条件 ConditionalFormatCollection格式当前 .

preset

返回预设条件条件格式。 有关详细信息,请参阅 Excel.PresetCriteriaConditionalFormat

presetOrNullObject

返回预设条件条件格式。 有关详细信息,请参阅 Excel.PresetCriteriaConditionalFormat

priority

此条件格式当前所在的条件格式集合中的优先级 (或索引) 。 更改此项还会更改其他条件格式的优先级,以允许连续的优先级顺序。 使用负优先级从后面开始。 大于边界的优先级将获取并设置为最大 (,如果为负) 优先级,则设置为最小值。 另请注意,如果更改优先级,如果要对其进行进一步更改,则必须在该新的优先级位置重新获取对象的新副本。

stopIfTrue

如果满足此条件格式的条件,则不会有任何低优先级格式应在此单元格上生效。 值位于 null 数据条、图标集和色阶上,因为没有这些概念 StopIfTrue

textComparison

如果当前条件格式为文本类型,则返回特定的文本条件格式属性。 例如,设置与单词“文本”匹配的单元格的格式。

textComparisonOrNullObject

如果当前条件格式为文本类型,则返回特定的文本条件格式属性。 例如,设置与单词“文本”匹配的单元格的格式。

topBottom

如果当前条件格式为类型 TopBottom ,则返回顶部/底部条件格式属性。 例如,设置前 10% 或后 10 个项目的格式。

topBottomOrNullObject

如果当前条件格式为类型 TopBottom ,则返回顶部/底部条件格式属性。 例如,设置前 10% 或后 10 个项目的格式。

type

一种条件格式。 一次只能设置一个。

方法

changeRuleToCellValue(properties)

将条件格式规则类型更改为单元格值。

changeRuleToColorScale()

将条件格式规则类型更改为色阶。

changeRuleToContainsText(properties)

将条件格式规则类型更改为文本比较。

changeRuleToCustom(formula)

将条件格式规则类型更改为“自定义”。

changeRuleToDataBar()

将条件格式规则类型更改为数据条。

changeRuleToIconSet()

将条件格式规则类型更改为图标集。

changeRuleToPresetCriteria(properties)

将条件格式规则类型更改为预设条件。

changeRuleToTopBottom(properties)

将条件格式规则类型更改为顶部/底部。

delete()

删除此条件格式。

getRange()

返回应用条件格式的区域。 如果条件格式应用于多个范围,则引发错误。

getRangeOrNullObject()

返回应用条件格式的区域。 如果条件格式应用于多个区域,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getRanges()

返回 RangeAreas包含应用条件格式的一个或多个矩形范围的 。

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

setRanges(ranges)

设置应用条件格式规则的范围。

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.ConditionalFormat 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.ConditionalFormatData) ,其中包含从原始对象加载的任何子属性的浅层副本。

属性详细信息

cellValue

如果当前条件格式为类型 CellValue ,则返回单元格值条件格式属性。

readonly cellValue: Excel.CellValueConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B21:E23");
    const conditionalFormat = range.conditionalFormats
        .add(Excel.ConditionalFormatType.cellValue);
    conditionalFormat.cellValue.format.font.color = "red";
    conditionalFormat.cellValue.rule = { formula1: "=0", operator: "LessThan" };

    await context.sync();
});

cellValueOrNullObject

如果当前条件格式为类型 CellValue ,则返回单元格值条件格式属性。

readonly cellValueOrNullObject: Excel.CellValueConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

colorScale

如果当前条件格式为类型 ColorScale ,则返回色阶条件格式属性。

readonly colorScale: Excel.ColorScaleConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B2:M5");
    const conditionalFormat = range.conditionalFormats
        .add(Excel.ConditionalFormatType.colorScale);
    const criteria = {
        minimum: { formula: null, type: Excel.ConditionalFormatColorCriterionType.lowestValue, color: "blue" },
        midpoint: { formula: "50", type: Excel.ConditionalFormatColorCriterionType.percent, color: "yellow" },
        maximum: { formula: null, type: Excel.ConditionalFormatColorCriterionType.highestValue, color: "red" }
    };
    conditionalFormat.colorScale.criteria = criteria;

    await context.sync();
});

colorScaleOrNullObject

如果当前条件格式为类型 ColorScale ,则返回色阶条件格式属性。

readonly colorScaleOrNullObject: Excel.ColorScaleConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

context

与对象关联的请求上下文。 这将加载项的进程连接到 Office 主机应用程序的进程。

context: RequestContext;

属性值

custom

如果当前条件格式为自定义类型,则返回自定义条件格式属性。

readonly custom: Excel.CustomConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B8:E13");
    const conditionalFormat = range.conditionalFormats.add(Excel.ConditionalFormatType.custom);
    conditionalFormat.custom.rule.formula = '=IF(B8>INDIRECT("RC[-1]",0),TRUE)';
    conditionalFormat.custom.format.font.color = "green";

    await context.sync();
});

customOrNullObject

如果当前条件格式为自定义类型,则返回自定义条件格式属性。

readonly customOrNullObject: Excel.CustomConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

dataBar

如果当前条件格式为数据栏,则返回数据栏属性。

readonly dataBar: Excel.DataBarConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B8:E13");
    const conditionalFormat = range.conditionalFormats
        .add(Excel.ConditionalFormatType.dataBar);
    conditionalFormat.dataBar.barDirection = Excel.ConditionalDataBarDirection.leftToRight;

    await context.sync();
});

dataBarOrNullObject

如果当前条件格式为数据栏,则返回数据栏属性。

readonly dataBarOrNullObject: Excel.DataBarConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

iconSet

如果当前条件格式为类型 IconSet ,则返回图标设置条件格式属性。

readonly iconSet: Excel.IconSetConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B8:E13");
    const conditionalFormat = range.conditionalFormats
        .add(Excel.ConditionalFormatType.iconSet);
    const iconSetCF = conditionalFormat.iconSet;
    iconSetCF.style = Excel.IconSet.threeTriangles;

    /*
        The iconSetCF.criteria array is automatically prepopulated with
        criterion elements whose properties have been given default settings.
        You can't write to each property of a criterion directly. Instead,
        replace the whole criteria object.

        With a "three*" icon set style, such as "threeTriangles", the third
        element in the criteria array (criteria[2]) defines the "top" icon;
        e.g., a green triangle. The second (criteria[1]) defines the "middle"
        icon. The first (criteria[0]) defines the "low" icon, but it
        can often be left empty as the following object shows, because every
        cell that does not match the other two criteria always gets the low
        icon.            
    */
    iconSetCF.criteria = [
        {} as any,
        {
            type: Excel.ConditionalFormatIconRuleType.number,
            operator: Excel.ConditionalIconCriterionOperator.greaterThanOrEqual,
            formula: "=700"
        },
        {
            type: Excel.ConditionalFormatIconRuleType.number,
            operator: Excel.ConditionalIconCriterionOperator.greaterThanOrEqual,
            formula: "=1000",
        }
    ];

    await context.sync();
});

iconSetOrNullObject

如果当前条件格式为类型 IconSet ,则返回图标设置条件格式属性。

readonly iconSetOrNullObject: Excel.IconSetConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

id

条件 ConditionalFormatCollection格式当前 .

readonly id: string;

属性值

string

注解

API 集:ExcelApi 1.6

preset

返回预设条件条件格式。 有关详细信息,请参阅 Excel.PresetCriteriaConditionalFormat

readonly preset: Excel.PresetCriteriaConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B2:M5");
    const conditionalFormat = range.conditionalFormats
        .add(Excel.ConditionalFormatType.presetCriteria);
    conditionalFormat.preset.format.font.color = "white";
    conditionalFormat.preset.rule = { criterion: Excel.ConditionalFormatPresetCriterion.oneStdDevAboveAverage };

    await context.sync();
});

presetOrNullObject

返回预设条件条件格式。 有关详细信息,请参阅 Excel.PresetCriteriaConditionalFormat

readonly presetOrNullObject: Excel.PresetCriteriaConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

priority

此条件格式当前所在的条件格式集合中的优先级 (或索引) 。 更改此项还会更改其他条件格式的优先级,以允许连续的优先级顺序。 使用负优先级从后面开始。 大于边界的优先级将获取并设置为最大 (,如果为负) 优先级,则设置为最小值。 另请注意,如果更改优先级,如果要对其进行进一步更改,则必须在该新的优先级位置重新获取对象的新副本。

priority: number;

属性值

number

注解

API 集:ExcelApi 1.6

stopIfTrue

如果满足此条件格式的条件,则不会有任何低优先级格式应在此单元格上生效。 值位于 null 数据条、图标集和色阶上,因为没有这些概念 StopIfTrue

stopIfTrue: boolean;

属性值

boolean

注解

API 集:ExcelApi 1.6

textComparison

如果当前条件格式为文本类型,则返回特定的文本条件格式属性。 例如,设置与单词“文本”匹配的单元格的格式。

readonly textComparison: Excel.TextConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange("B16:D18");
    const conditionalFormat = range.conditionalFormats
        .add(Excel.ConditionalFormatType.containsText);
    conditionalFormat.textComparison.format.font.color = "red";
    conditionalFormat.textComparison.rule = { operator: Excel.ConditionalTextOperator.contains, text: "Delayed" };

    await context.sync();
});

textComparisonOrNullObject

如果当前条件格式为文本类型,则返回特定的文本条件格式属性。 例如,设置与单词“文本”匹配的单元格的格式。

readonly textComparisonOrNullObject: Excel.TextConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

topBottom

如果当前条件格式为类型 TopBottom ,则返回顶部/底部条件格式属性。 例如,设置前 10% 或后 10 个项目的格式。

readonly topBottom: Excel.TopBottomConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

topBottomOrNullObject

如果当前条件格式为类型 TopBottom ,则返回顶部/底部条件格式属性。 例如,设置前 10% 或后 10 个项目的格式。

readonly topBottomOrNullObject: Excel.TopBottomConditionalFormat;

属性值

注解

API 集:ExcelApi 1.6

type

一种条件格式。 一次只能设置一个。

readonly type: Excel.ConditionalFormatType | "Custom" | "DataBar" | "ColorScale" | "IconSet" | "TopBottom" | "PresetCriteria" | "ContainsText" | "CellValue";

属性值

Excel.ConditionalFormatType | "Custom" | "DataBar" | "ColorScale" | "IconSet" | "TopBottom" | "PresetCriteria" | "ContainsText" | "CellValue"

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const worksheetRange = sheet.getRange();
    worksheetRange.conditionalFormats.load("type");

    await context.sync();

    let cfRangePairs: { cf: Excel.ConditionalFormat, range: Excel.Range }[] = [];
    worksheetRange.conditionalFormats.items.forEach(item => {
        cfRangePairs.push({
            cf: item,
            range: item.getRange().load("address")
        });
    });

    await context.sync();

    if (cfRangePairs.length > 0) {
        cfRangePairs.forEach(item => {
            console.log(item.cf.type);
        });
    } else {
        console.log("No conditional formats applied.");
    }
});

方法详细信息

changeRuleToCellValue(properties)

将条件格式规则类型更改为单元格值。

changeRuleToCellValue(properties: Excel.ConditionalCellValueRule): void;

参数

properties
Excel.ConditionalCellValueRule

要为单元格值条件格式规则设置的属性。

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToColorScale()

将条件格式规则类型更改为色阶。

changeRuleToColorScale(): void;

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToContainsText(properties)

将条件格式规则类型更改为文本比较。

changeRuleToContainsText(properties: Excel.ConditionalTextComparisonRule): void;

参数

properties
Excel.ConditionalTextComparisonRule

要为文本比较条件格式规则设置的属性。

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToCustom(formula)

将条件格式规则类型更改为“自定义”。

changeRuleToCustom(formula: string): void;

参数

formula

string

要为自定义条件格式规则设置的公式。

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToDataBar()

将条件格式规则类型更改为数据条。

changeRuleToDataBar(): void;

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToIconSet()

将条件格式规则类型更改为图标集。

changeRuleToIconSet(): void;

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToPresetCriteria(properties)

将条件格式规则类型更改为预设条件。

changeRuleToPresetCriteria(properties: Excel.ConditionalPresetCriteriaRule): void;

参数

properties
Excel.ConditionalPresetCriteriaRule

要为预设条件条件格式规则设置的属性。

返回

void

注解

API 集:ExcelApi 1.17

changeRuleToTopBottom(properties)

将条件格式规则类型更改为顶部/底部。

changeRuleToTopBottom(properties: Excel.ConditionalTopBottomRule): void;

参数

properties
Excel.ConditionalTopBottomRule

要为顶部/底部条件格式规则设置的属性。

返回

void

注解

API 集:ExcelApi 1.17

delete()

删除此条件格式。

delete(): void;

返回

void

注解

API 集:ExcelApi 1.6

getRange()

返回应用条件格式的区域。 如果条件格式应用于多个范围,则引发错误。

getRange(): Excel.Range;

返回

注解

API 集:ExcelApi 1.6

示例

// Link to full sample: https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/14-conditional-formatting/conditional-formatting-basic.yaml

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const worksheetRange = sheet.getRange();
    worksheetRange.conditionalFormats.load("type");

    await context.sync();

    let cfRangePairs: { cf: Excel.ConditionalFormat, range: Excel.Range }[] = [];
    worksheetRange.conditionalFormats.items.forEach(item => {
        cfRangePairs.push({
            cf: item,
            range: item.getRange().load("address")
        });
    });

    await context.sync();

    if (cfRangePairs.length > 0) {
        cfRangePairs.forEach(item => {
            console.log(item.cf.type);
        });
    } else {
        console.log("No conditional formats applied.");
    }
});

getRangeOrNullObject()

返回应用条件格式的区域。 如果条件格式应用于多个区域,则此方法将返回其 isNullObject 属性设置为 true的对象。 有关详细信息,请参阅 *OrNullObject 方法和属性

getRangeOrNullObject(): Excel.Range;

返回

注解

API 集:ExcelApi 1.6

getRanges()

返回 RangeAreas包含应用条件格式的一个或多个矩形范围的 。

getRanges(): Excel.RangeAreas;

返回

注解

API 集:ExcelApi 1.9

load(options)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(options?: Excel.Interfaces.ConditionalFormatLoadOptions): Excel.ConditionalFormat;

参数

options
Excel.Interfaces.ConditionalFormatLoadOptions

为要加载的对象属性提供选项。

返回

load(propertyNames)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNames?: string | string[]): Excel.ConditionalFormat;

参数

propertyNames

string | string[]

指定要加载的属性的逗号分隔的字符串或字符串数组。

返回

load(propertyNamesAndPaths)

将命令加入队列以加载对象的指定属性。 阅读属性前必须先调用 context.sync()

load(propertyNamesAndPaths?: {
            select?: string;
            expand?: string;
        }): Excel.ConditionalFormat;

参数

propertyNamesAndPaths

{ select?: string; expand?: string; }

propertyNamesAndPaths.select 是指定要加载的属性的逗号分隔字符串,以及 propertyNamesAndPaths.expand 指定要加载的导航属性的逗号分隔字符串。

返回

set(properties, options)

同时设置对象的多个属性。 可以传递具有相应属性的纯文本对象,也可以传递另一个相同类型的 API 对象。

set(properties: Interfaces.ConditionalFormatUpdateData, options?: OfficeExtension.UpdateOptions): void;

参数

properties
Excel.Interfaces.ConditionalFormatUpdateData

一个 JavaScript 对象,其属性与调用该方法的对象的属性同构结构。

options
OfficeExtension.UpdateOptions

提供一个选项,用于在 properties 对象尝试设置任何只读属性时禁止错误。

返回

void

set(properties)

基于现有加载的对象,同时在对象上设置多个属性。

set(properties: Excel.ConditionalFormat): void;

参数

返回

void

setRanges(ranges)

设置应用条件格式规则的范围。

setRanges(ranges: Range | RangeAreas | string): void;

参数

ranges

Excel.Range | Excel.RangeAreas | string

要应用此规则的一个或多个范围的集合。

返回

void

注解

API 集:ExcelApi 1.17

toJSON()

覆盖 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 (JSON.stringify反过来调用 toJSON 传递给它的对象的方法。) 虽然原始 Excel.ConditionalFormat 对象是 API 对象, toJSON 但该方法返回一个纯 JavaScript 对象, (类型化为 Excel.Interfaces.ConditionalFormatData) ,其中包含从原始对象加载的任何子属性的浅层副本。

toJSON(): Excel.Interfaces.ConditionalFormatData;

返回