Skip to content
本页内容

条件格式(FormatConditions)

代表一个区域内所有条件格式的集合

方法列表

方法说明
添加条件格式
编辑条件格式

Add()

添加条件格式

语法

表达式.Range.FormatConditions.Add({ Type, Operator, Formula1, Formula2 })

表达式:文档类型应用对象

参数

属性数据类型默认值必填说明
Type
Enum
指定条件格式是基于单元格值还是基于表达式,详细可见 Enum.XlFormatConditionType
Operator
Number
条件格式运算符,可以是 XlFormatConditionOperator 常量之一,详细可见 Enum.XlFormatConditionOperator
Formula1
Number
与条件格式关联的值或表达式。可为常量值、字符串值、单元格引用或公式
Formula2
Number
运算符为 xlBetween 或 xlNotBetween 时,与条件格式第二部分相关联的值或表达式(否则, 将忽略此参数)。可以是常量值、字符串值、单元格引用或公式

示例

js
//@file=base.xlsx
async function example() {
  await instance.ready()

  const app = instance.Application

  // 区域对象
  const range = await app.Range('A4:D5')

  // 选择区域
  await range.Select()

  // 设置公式
  range.Formula = 'WebOffice'

  // 条件格式对象
  const formatConditions = await range.FormatConditions

  // 添加条件格式
  await formatConditions.Add(
    app.Enum.XlFormatConditionType.xlExpression,
    undefined,
    '=D1=1'
  )
}

With()

编辑条件格式

语法

表达式.Range.FormatConditions.With({ Interior, Font, Borders })

表达式:文档类型应用对象

参数

属性数据类型默认值必填说明
Interior
Object
-
内部属性对象
Font
Object
-
字体对象
Borders
Object
-
边框对象

示例

js
//@file=base.xlsx
async function example() {
  await instance.ready()

  const app = instance.Application

  // 区域对象
  const range = await app.Range('A4:D5')

  // 选择区域
  await range.Select()

  // 设置公式
  range.Formula = 'WebOffice'

  // 条件格式对象
  const formatConditions = await range.FormatConditions

  // 添加条件格式
  const formatConditionsAdd = await formatConditions.Add(
    app.Enum.XlFormatConditionType.xlExpression,
    undefined,
    '=D1=1'
  )

  // 编辑条件格式
  await formatConditionsAdd.With({
    Interior: { Color: '#000000' },
    Font: {
      Bold: true,
      Color: '#FF0000',
      Underline: 2,
      Italic: true,
      Strikethrough: true
    },
    Border: { LineStyle: -4119, Color: '#FF0000' }
  })
}