设置条件格式

  1. func (f *File) SetConditionalFormat(sheet, area, formatSet string) error

根据给定的工作表名称、单元格坐标区域和格式参数,为单元格值创建条件格式设置规则。条件格式是 Office Excel 的一项功能,它允许您根据特定条件将格式应用于单元格或一系列单元格。

格式参数 type 选项是必需的参数,它没有默认值。允许的类型值及其相关参数是:

类型参数
cellcriteria
value
minimum
maximum
datecriteria
value
minimum
maximum
time_periodcriteria
textcriteria
value
averagecriteria
duplicate(none)
unique(none)
topcriteria
value
bottomcriteria
value
blanks(none)
no_blanks(none)
errors(none)
no_errors(none)
2_color_scalemin_type
max_type
min_value
max_value
min_color
max_color
3_color_scalemin_type
mid_type
max_type
min_value
mid_value
max_value
min_color
mid_color
max_color
data_barmin_type
max_type
min_value
max_value
bar_color
formulacriteria

criteria 参数用于设置单元格数据的条件格式运算符。它没有默认值,同常与 {"type":"cell"} 一起使用,支持的参数为:

文本描述字符符号表示
between
not between
equal to==
not equal to!=
greater than>
less than<
greater than or equal to>=
less than or equal to<=

可以使用上面表格第一列中的 Office Excel 文本描述字符,或者符号表示方法(betweennot between 没有符号表示法)作为条件格式运算符。下面的相关部分显示了其他条件格式类型的特定标准。

value:该值通常与 criteria 参数一起使用,可以用确定的值作为设置单元格条件格式的条件参数:

  1. f.SetConditionalFormat("Sheet1", "D1:D10", fmt.Sprintf(`[
  2. {
  3. "type": "cell",
  4. "criteria": ">",
  5. "format": %d,
  6. "value": "6"
  7. }]`, format))

value 属性也可以是单元格引用:

  1. f.SetConditionalFormat("Sheet1", "D1:D10", fmt.Sprintf(`[
  2. {
  3. "type": "cell",
  4. "criteria": ">",
  5. "format": %d,
  6. "value": "$C$1"
  7. }]`, format))

类型:format - format 参数用于指定满足条件格式标准时将应用于单元格的格式。该参数可以通过 NewConditionalStyle() 方法来创建:

  1. format, err = f.NewConditionalStyle(`{
  2. "font":
  3. {
  4. "color": "#9A0511"
  5. },
  6. "fill":
  7. {
  8. "type": "pattern",
  9. "color": ["#FEC7CE"],
  10. "pattern": 1
  11. }
  12. }`)
  13. if err != nil {
  14. fmt.Println(err)
  15. }
  16. f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[
  17. {
  18. "type": "cell",
  19. "criteria": ">",
  20. "format": %d,
  21. "value": "6"
  22. }]`, format))

注意:在 Office Excel 中,条件格式叠加在现有单元格格式上,并非所有单元格格式属性都可以修改。无法在条件格式中修改的属性包括:字体名称、字体大小、上标和下标、对角边框、所有对齐属性和所有保护属性。

Office Excel 中内置了一些与条件格式一起使用的默认样式。可以使用以下 excelize 设置实现这些样式效果:

  1. // 浅红填充色深色文本代表较差
  2. format1, err = f.NewConditionalStyle(`{
  3. "font":
  4. {
  5. "color": "#9A0511"
  6. },
  7. "fill":
  8. {
  9. "type": "pattern",
  10. "color": ["#FEC7CE"],
  11. "pattern": 1
  12. }
  13. }`)
  14. // 黄填充色深黄色文本代表一般
  15. format2, err = f.NewConditionalStyle(`{
  16. "font":
  17. {
  18. "color": "#9B5713"
  19. },
  20. "fill":
  21. {
  22. "type": "pattern",
  23. "color": ["#FEEAA0"],
  24. "pattern": 1
  25. }
  26. }`)
  27. // 绿填充色深绿色文本代表较好
  28. format3, err = f.NewConditionalStyle(`{
  29. "font":
  30. {
  31. "color": "#09600B"
  32. },
  33. "fill":
  34. {
  35. "type": "pattern",
  36. "color": ["#C7EECF"],
  37. "pattern": 1
  38. }
  39. }`)

类型:minimum - 当条件格式 criteriabetweennot between 时,minimum 参数用于设置下限值。

  1. // 高亮单元格条件格式规则: between...
  2. f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[
  3. {
  4. "type": "cell",
  5. "criteria": "between",
  6. "format": %d,
  7. "minimum": "6",
  8. "maximum": "8"
  9. }]`, format))

类型:maximum - 当条件格式 criteriabetweennot between 时,maximum 参数用于设置上限值,参考上面的例子。

类型:average - 平均类型用于指定 Office Excel “最前最后规则”中“经典”样式的“仅高于或低于平均值的数值设置格式”条件格式:

  1. // 最前最后规则:高于平均值...
  2. f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[
  3. {
  4. "type": "average",
  5. "criteria": "=",
  6. "format": %d,
  7. "above_average": true
  8. }]`, format1))
  9. // 最前最后规则:低于平均值...
  10. f.SetConditionalFormat("Sheet1", "B1:B10", fmt.Sprintf(`[
  11. {
  12. "type": "average",
  13. "criteria": "=",
  14. "format": %d,
  15. "above_average": false
  16. }]`, format2))

类型:duplicate - 用于设置“突出显示单元格规则”中的“重复值 …”:

  1. // 突出显示单元格规则: 重复值...
  2. f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[
  3. {
  4. "type": "duplicate",
  5. "criteria": "=",
  6. "format": %d
  7. }]`, format))

类型:unique - 用于设置“突出显示单元格规则”中“只为以下内容的单元格设置格式”的“特定文本”:

  1. // 突出显示单元格规则,只为以下内容的单元格设置格式: 特定文本 不等于...
  2. f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[
  3. {
  4. "type": "unique",
  5. "criteria": "=",
  6. "format": %d
  7. }]`, format))

类型:top - 用于设置“最前最后规则”中的“前 10 项…”或“前 10% …”:

  1. // 最前最后规则: 前 10 项...
  2. f.SetConditionalFormat("Sheet1", "H1:H10", fmt.Sprintf(`[
  3. {
  4. "type": "top",
  5. "criteria": "=",
  6. "format": %d,
  7. "value": "6"
  8. }]`, format))

设置带有百分比条件的条件格式:

  1. f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[
  2. {
  3. "type": "top",
  4. "criteria": "=",
  5. "format": %d,
  6. "value": "6",
  7. "percent": true
  8. }]`, format))

类型:2_color_scale - 用于设置带有“双色刻度”的“色阶样式”条件格式:

  1. // 色阶:双色刻度
  2. f.SetConditionalFormat("Sheet1", "A1:A10", `[
  3. {
  4. "type": "2_color_scale",
  5. "criteria": "=",
  6. "min_type": "min",
  7. "max_type": "max",
  8. "min_color": "#F8696B",
  9. "max_color": "#63BE7B"
  10. }]`)

双色刻度色阶条件格式可选参数:min_typemax_typemin_valuemax_valuemin_colormax_color

类型:3_color_scale - 用于设置带有“三色刻度”的“色阶样式”条件格式:

  1. // 色阶:三色刻度
  2. f.SetConditionalFormat("Sheet1", "A1:A10", `[
  3. {
  4. "type": "3_color_scale",
  5. "criteria": "=",
  6. "min_type": "min",
  7. "mid_type": "percentile",
  8. "max_type": "max",
  9. "min_color": "#F8696B",
  10. "mid_color": "#FFEB84",
  11. "max_color": "#63BE7B"
  12. }]`)

三色刻度色阶条件格式可选参数: min_typemid_typemax_typemin_valuemid_valuemax_valuemin_colormid_colormax_color

类型:data_bar - 用于设置“数据条”类型的条件格式。

min_type - 参数 min_type 在条件格式类型为 2_color_scale3_color_scaledata_bar 时可用。参数 mid_type 在条件格式类型为 3_color_scale 时可用。例如:

  1. // 数据条:渐变填充
  2. f.SetConditionalFormat("Sheet1", "K1:K10", `[
  3. {
  4. "type": "data_bar",
  5. "criteria": "=",
  6. "min_type": "min",
  7. "max_type": "max",
  8. "bar_color": "#638EC6"
  9. }]`)

参数 min/mid/max_types 可选值列表:

参数类型
min最低值(仅用于 min_type
num数字
percent百分比
percentile百分点值
formula公式
max最高值(仅用于 max_type

mid_type - 当条件格式类型为 3_color_scale 时使用,与 min_type 用法相同,参考上面的表格。

max_type - 与 min_type 用法相同,参考上面的表格。

min_value - 参数 min_valuemax_value 在条件格式类型为 2_color_scale3_color_scaledata_bar 时可用。参数 mid_value 在条件格式类型为 3_color_scale 时可用。

mid_value - 在条件格式类型为 3_color_scale 时可用,与 min_value 的用法相同,参考上述文档。

max_value - 与 min_value 的用法相同,参考上述文档。

min_color - 参数 min_colormax_color 在条件格式类型为 2_color_scale3_color_scaledata_bar 时可用。参数 mid_color 在条件格式类型为 3_color_scale 时可用。例如:

  1. // 色阶:三色刻度
  2. f.SetConditionalFormat("Sheet1", "B1:B10", `[
  3. {
  4. "type": "3_color_scale",
  5. "criteria": "=",
  6. "min_type": "min",
  7. "mid_type": "percentile",
  8. "max_type": "max",
  9. "min_color": "#F8696B",
  10. "mid_color": "#FFEB84",
  11. "max_color": "#63BE7B"
  12. }]`)

mid_color - 当条件格式类型为 3_color_scale 时使用。与 min_color 用法相同,参考上述文档。

max_color - 与 min_color 用法相同,参考上述文档。

bar_color - 当条件格式类型为 data_bar 时使用。与 min_color 用法相同,参考上述文档。

例如,为名为 Sheet1 的工作表中,通过设置条件格式高亮 A1:D4 区域单元格中的最大值与最小值:

通过设置条件格式高亮区域单元格中的最大值与最小值

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "github.com/xuri/excelize/v2"
  6. )
  7. func main() {
  8. f := excelize.NewFile()
  9. for r := 1; r <= 4; r++ {
  10. row := []int{rand.Intn(100), rand.Intn(100), rand.Intn(100), rand.Intn(100)}
  11. if err := f.SetSheetRow("Sheet1", fmt.Sprintf("A%d", r), &row); err != nil {
  12. fmt.Println(err)
  13. }
  14. }
  15. red, err := f.NewConditionalStyle(`{
  16. "font":
  17. {
  18. "color": "#9A0511"
  19. },
  20. "fill":
  21. {
  22. "type": "pattern",
  23. "color": ["#FEC7CE"],
  24. "pattern": 1
  25. }
  26. }`)
  27. if err != nil {
  28. fmt.Println(err)
  29. }
  30. if err := f.SetConditionalFormat("Sheet1", "A1:D4", fmt.Sprintf(`[
  31. {
  32. "type": "bottom",
  33. "criteria": "=",
  34. "value": "1",
  35. "format": %d
  36. }]`, red)); err != nil {
  37. fmt.Println(err)
  38. }
  39. green, err := f.NewConditionalStyle(`{
  40. "font":
  41. {
  42. "color": "#09600B"
  43. },
  44. "fill":
  45. {
  46. "type": "pattern",
  47. "color": ["#C7EECF"],
  48. "pattern": 1
  49. }
  50. }`)
  51. if err != nil {
  52. fmt.Println(err)
  53. }
  54. if err := f.SetConditionalFormat("Sheet1", "A1:D4", fmt.Sprintf(`[
  55. {
  56. "type": "top",
  57. "criteria":"=",
  58. "value":"1",
  59. "format": %d
  60. }]`, green)); err != nil {
  61. fmt.Println(err)
  62. }
  63. if err := f.SaveAs("Book1.xlsx"); err != nil {
  64. fmt.Println(err)
  65. }
  66. }