Excel.ConditionalFormatCollection class

表示与范围重叠的所有条件格式的集合。

Extends

注解

[ API 集:ExcelApi 1.6 ]

属性

context

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

items

获取此集合中已加载的子项。

方法

add(type)

将新的条件格式添加到集合的第一个/最高优先级。

add(typeString)

将新的条件格式添加到集合的第一个/最高优先级。

clearAll()

清除当前指定区域中处于活动状态的所有条件格式。

getCount()

返回工作簿中条件格式的数目。

getItem(id)

返回给定 ID 的条件格式。

getItemAt(index)

返回给定索引处的条件格式。

getItemOrNullObject(id)

返回由其 ID 标识的条件格式。 如果条件格式对象不存在,则此方法返回一个对象,其 isNullObject 属性设置为 true。 有关详细信息,请参阅 *OrNullObject 方法和属性

load(options)

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

load(propertyNames)

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

load(propertyNamesAndPaths)

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

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来,调用toJSON传递给它的 对象的 方法。) 虽然原始Excel.ConditionalFormatCollection对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.ConditionalFormatCollectionData) ,其中包含一个“items”数组,其中包含集合项中任何已加载属性的浅表副本。

属性详细信息

context

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

context: RequestContext;

属性值

items

获取此集合中已加载的子项。

readonly items: Excel.ConditionalFormat[];

属性值

方法详细信息

add(type)

将新的条件格式添加到集合的第一个/最高优先级。

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

参数

type
Excel.ConditionalFormatType

要添加的条件格式的类型。 有关详细信息,请参阅 Excel.ConditionalFormatType

返回

注解

[ 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();
});

add(typeString)

将新的条件格式添加到集合的第一个/最高优先级。

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

参数

typeString

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

要添加的条件格式的类型。 有关详细信息,请参阅 Excel.ConditionalFormatType

返回

注解

[ API 集:ExcelApi 1.6 ]

clearAll()

清除当前指定区域中处于活动状态的所有条件格式。

clearAll(): void;

返回

void

注解

[ 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();
    range.conditionalFormats.clearAll();

    await context.sync();

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

getCount()

返回工作簿中条件格式的数目。

getCount(): OfficeExtension.ClientResult<number>;

返回

注解

[ API 集:ExcelApi 1.6 ]

示例

await Excel.run(async (context) => {
    const sheetName = "Sheet1";
    const rangeAddress = "A1:C3";
    const range = context.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    const conditionalFormat = range.conditionalFormats.add(Excel.ConditionalFormatType.iconSet);
    conditionalFormat.iconSetOrNullObject.style = Excel.IconSet.fourTrafficLights;
    const cfCount = range.conditionalFormats.getCount(); 

    await context.sync()
    console.log("Count: " + cfCount.value);
});

getItem(id)

返回给定 ID 的条件格式。

getItem(id: string): Excel.ConditionalFormat;

参数

id

string

条件格式的 ID。

返回

条件格式对象。

注解

[ API 集:ExcelApi 1.6 ]

示例

await Excel.run(async (context) => {
    const sheetName = "Sheet1";
    const rangeAddress = "A1:C3";
    const range = context.workbook.worksheets.getItem(sheetName).getRange(rangeAddress);
    const conditionalFormats = range.conditionalFormats;
    const conditionalFormat = conditionalFormats.getItemAt(3);
    await context.sync()

    console.log("Conditional Format at Item 3 Loaded");
});

getItemAt(index)

返回给定索引处的条件格式。

getItemAt(index: number): Excel.ConditionalFormat;

参数

index

number

要检索的条件格式的索引。

返回

注解

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

getItemOrNullObject(id)

返回由其 ID 标识的条件格式。 如果条件格式对象不存在,则此方法返回一个对象,其 isNullObject 属性设置为 true。 有关详细信息,请参阅 *OrNullObject 方法和属性

getItemOrNullObject(id: string): Excel.ConditionalFormat;

参数

id

string

条件格式的 ID。

返回

注解

[ API 集:ExcelApi 1.14 ]

load(options)

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

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

参数

返回

load(propertyNames)

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

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

参数

propertyNames

string | string[]

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

返回

load(propertyNamesAndPaths)

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

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

参数

propertyNamesAndPaths
OfficeExtension.LoadOption

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

返回

toJSON()

重写 JavaScript toJSON() 方法,以便在将 API 对象传递给 JSON.stringify()时提供更有用的输出。 JSON.stringify (,反过来,调用toJSON传递给它的 对象的 方法。) 虽然原始Excel.ConditionalFormatCollection对象是 API 对象,toJSON但该方法返回一个纯 JavaScript 对象, (类型为 Excel.Interfaces.ConditionalFormatCollectionData) ,其中包含一个“items”数组,其中包含集合项中任何已加载属性的浅表副本。

toJSON(): Excel.Interfaces.ConditionalFormatCollectionData;

返回