Excel.ConditionalFormatCollection class

代表与区域重叠的所有条件格式的集合。Represents a collection of all the conditional formats that are overlap the range.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

Extends

属性

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

方法

add(type)

将新的条件格式添加到集合中的第一个/最高优先级处。Adds a new conditional format to the collection at the first/top priority.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

add(typeString)

将新的条件格式添加到集合中的第一个/最高优先级处。Adds a new conditional format to the collection at the first/top priority.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

clearAll()

清除当前指定区域中处于活动状态的所有条件格式。Clears all conditional formats active on the current specified range.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

getCount()

返回工作簿中的条件格式数。Returns the number of conditional formats in the workbook. 只读。Read-only.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

getItem(id)

返回给定 ID 的条件格式。Returns a conditional format for the given ID.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

getItemAt(index)

返回给定索引处的条件格式。Returns a conditional format at the given index.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

load(option)
toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始Excel.ConditionalFormatCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.ConditionalFormatCollectionData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.ConditionalFormatCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.ConditionalFormatCollectionData), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

属性详细信息

context

与该对象关联的请求上下文。The request context associated with the object. 这会将加载项的进程连接到 Office 主机应用程序的进程。This connects the add-in's process to the Office host application's process.

context: RequestContext;

属性值

RequestContext

items

获取此集合中已加载的子项。Gets the loaded child items in this collection.

readonly items: Excel.ConditionalFormat[];

属性值

Excel.ConditionalFormat[]

方法详细信息

add(type)

将新的条件格式添加到集合中的第一个/最高优先级处。Adds a new conditional format to the collection at the first/top priority.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

add(type: Excel.ConditionalFormatType): Excel.ConditionalFormat;

参数

type
Excel.ConditionalFormatType

要添加的条件格式的类型。The type of conditional format being added. 有关详细信息, 请参阅 ConditionalFormatType。See Excel.ConditionalFormatType for details.

返回

示例Examples

Excel.run(function (ctx) {
    var sheetName = "Sheet1";
    var rangeAddress = "A1:C3";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    var conditionalFormat = range.conditionalFormats.add(Excel.ConditionalFormatType.iconSet);
    conditionalFormat.iconOrNull.style = "YellowThreeArrows";
    return ctx.sync().then(function () {
        console.log("Added new yellow three arrow icon set.");
    });
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
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();
});

add(typeString)

将新的条件格式添加到集合中的第一个/最高优先级处。Adds a new conditional format to the collection at the first/top priority.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

add(typeString: "Custom" | "DataBar" | "ColorScale" | "IconSet" | "TopBottom" | "PresetCriteria" | "ContainsText" | "CellValue"): Excel.ConditionalFormat;

参数

typeString
"Custom" | "DataBar" | "ColorScale" | "IconSet" | "TopBottom" | "PresetCriteria" | "ContainsText" | "CellValue"

要添加的条件格式的类型。The type of conditional format being added. 有关详细信息, 请参阅 ConditionalFormatType。See Excel.ConditionalFormatType for details.

返回

clearAll()

清除当前指定区域中处于活动状态的所有条件格式。Clears all conditional formats active on the current specified range.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

clearAll(): void;

返回

void

示例Examples

Excel.run(function (ctx) {
    var sheetName = "Sheet1";
    var rangeAddress = "A1:C3";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    var conditionalFormats = range.conditionalFormats;
    var conditionalFormat = conditionalFormats.clearAll();
    return ctx.sync().then(function () {
        console.log("Cleared all conditional formats from this range.");
    });
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Sample");
    const range = sheet.getRange();
    range.conditionalFormats.clearAll();

    await context.sync();

    $(".conditional-formats").hide();
});

getCount()

返回工作簿中的条件格式数。Returns the number of conditional formats in the workbook. 只读。Read-only.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

getCount(): OfficeExtension.ClientResult<number>;

返回

OfficeExtension.ClientResult<number>

示例Examples

Excel.run(function (ctx) {
    var sheetName = "Sheet1";
    var rangeAddress = "A1:C3";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    var conditionalFormat = range.conditionalFormats.add(Excel.ConditionalFormatType.iconSet);
    conditionalFormat.iconOrNull.style = Excel.IconSet.fourTrafficLights;
    var cfCount = range.conditionalFormats.getCount(); 

    return ctx.sync().then(function () {
        console.log("Count: " + cfCount.value);
    });
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getItem(id)

返回给定 ID 的条件格式。Returns a conditional format for the given ID.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

getItem(id: string): Excel.ConditionalFormat;

参数

id
string

条件格式的 id。The id of the conditional format.

返回

条件格式对象。Conditional Format object.

示例Examples

Excel.run(function (ctx) {
    var sheetName = "Sheet1";
    var rangeAddress = "A1:C3";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    var conditionalFormats = range.conditionalFormats;
    var conditionalFormat = conditionalFormats.getItemAt(3);
    return ctx.sync().then(function () {
        console.log("Conditional Format at Item 3 Loaded");
    });
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

getItemAt(index)

返回给定索引处的条件格式。Returns a conditional format at the given index.

[API 集: ExcelApi 1.6][ API set: ExcelApi 1.6 ]

getItemAt(index: number): Excel.ConditionalFormat;

参数

index
number

要检索的条件格式的索引。Index of the conditional formats to be retrieved.

返回

示例Examples

Excel.run(function (ctx) {
    var sheetName = "Sheet1";
    var rangeAddress = "A1:C3";
    var range = ctx.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    var conditionalFormats = range.conditionalFormats;
    var conditionalFormat = conditionalFormats.getItemAt(3);
    return ctx.sync().then(function () {
        console.log("Conditional Format at Item 3 Loaded");
    });
}).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
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.");
    }
});

load(option)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性之前,你必须调用“context.sync()”。You must call "context.sync()" before reading the properties.

load(option?: Excel.Interfaces.ConditionalFormatCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions): Excel.ConditionalFormatCollection;

参数

option
Excel.Interfaces.ConditionalFormatCollectionLoadOptions & Excel.Interfaces.CollectionLoadOptions

返回

注解

除了此签名之外, 此方法还具有以下签名:In addition to this signature, this method has the following signatures:

load(option?: string | string[]): Excel.ConditionalFormatCollection-Where 选项是一个逗号分隔的字符串或一个字符串数组, 用于指定要加载的属性。load(option?: string | string[]): Excel.ConditionalFormatCollection - Where option is a comma-delimited string or an array of strings that specify the properties to load.

load(option?: { select?: string; expand?: string; }): Excel.ConditionalFormatCollection-Where 选项。选择是一个用逗号分隔的字符串, 用于指定要加载的属性和选项。 expand 是一个用逗号分隔的字符串, 用于指定要加载的导航属性。load(option?: { select?: string; expand?: string; }): Excel.ConditionalFormatCollection - Where option.select is a comma-delimited string that specifies the properties to load, and options.expand is a comma-delimited string that specifies the navigation properties to load.

load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.ConditionalFormatCollection-仅适用于集合类型。load(option?: { select?: string; expand?: string; top?: number; skip?: number }): Excel.ConditionalFormatCollection - Only available on collection types. 它类似于前面的签名。It is similar to the preceding signature. top 指定结果中可包含的集合项的最大数量。Option.top specifies the maximum number of collection items that can be included in the result. 选项。 skip 指定要跳过且不包含在结果中的项目数。Option.skip specifies the number of items that are to be skipped and not included in the result. 如果指定了 top 选项, 则在跳过指定数目的项目后将启动结果集。If option.top is specified, the result set will start after skipping the specified number of items.

load(propertyNames)

将命令加入队列以加载对象的指定属性。Queues up a command to load the specified properties of the object. 在读取属性context.sync()之前, 必须先调用。You must call context.sync() before reading the properties.

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

参数

propertyNames
string | string[]

一个以逗号分隔的字符串或字符串数组, 用于指定要加载的属性。A comma-delimited string or an array of strings that specify the properties to load.

返回

load(option)

load(option?: OfficeExtension.LoadOption): Excel.ConditionalFormatCollection;

参数

返回

toJSON()

重写 JavaScript toJSON()方法, 以便在将 API 对象传递到时提供更有用的输出JSON.stringify()Overrides the JavaScript toJSON() method in order to provide more useful output when an API object is passed to JSON.stringify(). (JSON.stringify反过来, 也会调用传递toJSON给它的对象的方法。尽管原始Excel.ConditionalFormatCollection对象是 API 对象, 但toJSON方法返回一个无格式的 JavaScript 对象 (类型为Excel.Interfaces.ConditionalFormatCollectionData, in turn, calls the toJSON method of the object that is passed to it.) Whereas the original Excel.ConditionalFormatCollection object is an API object, the toJSON method returns a plain JavaScript object (typed as Excel.Interfaces.ConditionalFormatCollectionData), 其中包含集合的项中所有加载属性的浅表副本的 "items" 数组。) that contains an "items" array with shallow copies of any loaded properties from the collection's items.

toJSON(): Excel.Interfaces.ConditionalFormatCollectionData;

返回