Excel.ConditionalFormat class

An object encapsulating a conditional format's range, format, rule, and other properties. To learn more about the conditional formatting object model, read Apply conditional formatting to Excel ranges.

Extends

Remarks

[ API set: ExcelApi 1.6 ]

Properties

cellValue

Returns the cell value conditional format properties if the current conditional format is a CellValue type.

cellValueOrNullObject

Returns the cell value conditional format properties if the current conditional format is a CellValue type.

colorScale

Returns the color scale conditional format properties if the current conditional format is a ColorScale type.

colorScaleOrNullObject

Returns the color scale conditional format properties if the current conditional format is a ColorScale type.

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

custom

Returns the custom conditional format properties if the current conditional format is a custom type.

customOrNullObject

Returns the custom conditional format properties if the current conditional format is a custom type.

dataBar

Returns the data bar properties if the current conditional format is a data bar.

dataBarOrNullObject

Returns the data bar properties if the current conditional format is a data bar.

iconSet

Returns the icon set conditional format properties if the current conditional format is an IconSet type.

iconSetOrNullObject

Returns the icon set conditional format properties if the current conditional format is an IconSet type.

id

The priority of the conditional format in the current ConditionalFormatCollection.

preset

Returns the preset criteria conditional format. See Excel.PresetCriteriaConditionalFormat for more details.

presetOrNullObject

Returns the preset criteria conditional format. See Excel.PresetCriteriaConditionalFormat for more details.

priority

The priority (or index) within the conditional format collection that this conditional format currently exists in. Changing this also changes other conditional formats' priorities, to allow for a contiguous priority order. Use a negative priority to begin from the back. Priorities greater than the bounds will get and set to the maximum (or minimum if negative) priority. Also note that if you change the priority, you have to re-fetch a new copy of the object at that new priority location if you want to make further changes to it.

stopIfTrue

If the conditions of this conditional format are met, no lower-priority formats shall take effect on that cell. Value is null on data bars, icon sets, and color scales as there's no concept of StopIfTrue for these.

textComparison

Returns the specific text conditional format properties if the current conditional format is a text type. For example, to format cells matching the word "Text".

textComparisonOrNullObject

Returns the specific text conditional format properties if the current conditional format is a text type. For example, to format cells matching the word "Text".

topBottom

Returns the top/bottom conditional format properties if the current conditional format is a TopBottom type. For example, to format the top 10% or bottom 10 items.

topBottomOrNullObject

Returns the top/bottom conditional format properties if the current conditional format is a TopBottom type. For example, to format the top 10% or bottom 10 items.

type

A type of conditional format. Only one can be set at a time.

Methods

delete()

Deletes this conditional format.

getRange()

Returns the range the conditonal format is applied to. Throws an error if the conditional format is applied to multiple ranges.

getRangeOrNullObject()

Returns the range to which the conditonal format is applied. If the conditional format is applied to multiple ranges, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getRanges()

Returns the RangeAreas, comprising one or more rectangular ranges, to which the conditonal format is applied.

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.ConditionalFormat object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.ConditionalFormatData) that contains shallow copies of any loaded child properties from the original object.

Property Details

cellValue

Returns the cell value conditional format properties if the current conditional format is a CellValue type.

readonly cellValue: Excel.CellValueConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the cell value conditional format properties if the current conditional format is a CellValue type.

readonly cellValueOrNullObject: Excel.CellValueConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

colorScale

Returns the color scale conditional format properties if the current conditional format is a ColorScale type.

readonly colorScale: Excel.ColorScaleConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the color scale conditional format properties if the current conditional format is a ColorScale type.

readonly colorScaleOrNullObject: Excel.ColorScaleConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

context

The request context associated with the object. This connects the add-in's process to the Office host application's process.

context: RequestContext;

Property Value

custom

Returns the custom conditional format properties if the current conditional format is a custom type.

readonly custom: Excel.CustomConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the custom conditional format properties if the current conditional format is a custom type.

readonly customOrNullObject: Excel.CustomConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

dataBar

Returns the data bar properties if the current conditional format is a data bar.

readonly dataBar: Excel.DataBarConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the data bar properties if the current conditional format is a data bar.

readonly dataBarOrNullObject: Excel.DataBarConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

iconSet

Returns the icon set conditional format properties if the current conditional format is an IconSet type.

readonly iconSet: Excel.IconSetConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the icon set conditional format properties if the current conditional format is an IconSet type.

readonly iconSetOrNullObject: Excel.IconSetConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

id

The priority of the conditional format in the current ConditionalFormatCollection.

readonly id: string;

Property Value

string

Remarks

[ API set: ExcelApi 1.6 ]

preset

Returns the preset criteria conditional format. See Excel.PresetCriteriaConditionalFormat for more details.

readonly preset: Excel.PresetCriteriaConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the preset criteria conditional format. See Excel.PresetCriteriaConditionalFormat for more details.

readonly presetOrNullObject: Excel.PresetCriteriaConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

priority

The priority (or index) within the conditional format collection that this conditional format currently exists in. Changing this also changes other conditional formats' priorities, to allow for a contiguous priority order. Use a negative priority to begin from the back. Priorities greater than the bounds will get and set to the maximum (or minimum if negative) priority. Also note that if you change the priority, you have to re-fetch a new copy of the object at that new priority location if you want to make further changes to it.

priority: number;

Property Value

number

Remarks

[ API set: ExcelApi 1.6 ]

stopIfTrue

If the conditions of this conditional format are met, no lower-priority formats shall take effect on that cell. Value is null on data bars, icon sets, and color scales as there's no concept of StopIfTrue for these.

stopIfTrue: boolean;

Property Value

boolean

Remarks

[ API set: ExcelApi 1.6 ]

textComparison

Returns the specific text conditional format properties if the current conditional format is a text type. For example, to format cells matching the word "Text".

readonly textComparison: Excel.TextConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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

Returns the specific text conditional format properties if the current conditional format is a text type. For example, to format cells matching the word "Text".

readonly textComparisonOrNullObject: Excel.TextConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

topBottom

Returns the top/bottom conditional format properties if the current conditional format is a TopBottom type. For example, to format the top 10% or bottom 10 items.

readonly topBottom: Excel.TopBottomConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

topBottomOrNullObject

Returns the top/bottom conditional format properties if the current conditional format is a TopBottom type. For example, to format the top 10% or bottom 10 items.

readonly topBottomOrNullObject: Excel.TopBottomConditionalFormat;

Property Value

Remarks

[ API set: ExcelApi 1.6 ]

type

A type of conditional format. Only one can be set at a time.

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

Property Value

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

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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.");
    }
});

Method Details

delete()

Deletes this conditional format.

delete(): void;

Returns

void

Remarks

[ API set: ExcelApi 1.6 ]

getRange()

Returns the range the conditonal format is applied to. Throws an error if the conditional format is applied to multiple ranges.

getRange(): Excel.Range;

Returns

Remarks

[ API set: ExcelApi 1.6 ]

Examples

// 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()

Returns the range to which the conditonal format is applied. If the conditional format is applied to multiple ranges, then this method returns an object with its isNullObject property set to true. For further information, see *OrNullObject methods and properties.

getRangeOrNullObject(): Excel.Range;

Returns

Remarks

[ API set: ExcelApi 1.6 ]

getRanges()

Returns the RangeAreas, comprising one or more rectangular ranges, to which the conditonal format is applied.

getRanges(): Excel.RangeAreas;

Returns

Remarks

[ API set: ExcelApi 1.9 ]

load(options)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

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

Parameters

options
Excel.Interfaces.ConditionalFormatLoadOptions

Provides options for which properties of the object to load.

Returns

load(propertyNames)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

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

Parameters

propertyNames

string | string[]

A comma-delimited string or an array of strings that specify the properties to load.

Returns

load(propertyNamesAndPaths)

Queues up a command to load the specified properties of the object. You must call context.sync() before reading the properties.

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

Parameters

propertyNamesAndPaths

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

propertyNamesAndPaths.select is a comma-delimited string that specifies the properties to load, and propertyNamesAndPaths.expand is a comma-delimited string that specifies the navigation properties to load.

Returns

set(properties, options)

Sets multiple properties of an object at the same time. You can pass either a plain object with the appropriate properties, or another API object of the same type.

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

Parameters

properties
Excel.Interfaces.ConditionalFormatUpdateData

A JavaScript object with properties that are structured isomorphically to the properties of the object on which the method is called.

options
OfficeExtension.UpdateOptions

Provides an option to suppress errors if the properties object tries to set any read-only properties.

Returns

void

set(properties)

Sets multiple properties on the object at the same time, based on an existing loaded object.

set(properties: Excel.ConditionalFormat): void;

Parameters

Returns

void

toJSON()

Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.ConditionalFormat object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.ConditionalFormatData) that contains shallow copies of any loaded child properties from the original object.

toJSON(): Excel.Interfaces.ConditionalFormatData;

Returns