Excel.ConditionalFormatCollection class

表示与区域重叠的所有条件格式的集合。

扩展

属性

context

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

items

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

方法

add(type)

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

add(type)

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

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”数组,其中包含从集合的 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(type)

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

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

参数

type

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

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”数组,其中包含从集合的 items 中加载的任何属性的浅层副本。

toJSON(): Excel.Interfaces.ConditionalFormatCollectionData;

返回