styles_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package excelize
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestSetConditionalFormat(t *testing.T) {
  7. cases := []struct {
  8. label string
  9. format string
  10. rules []*xlsxCfRule
  11. }{{
  12. label: "3_color_scale",
  13. format: `[{
  14. "type":"3_color_scale",
  15. "criteria":"=",
  16. "min_type":"num",
  17. "mid_type":"num",
  18. "max_type":"num",
  19. "min_value": "-10",
  20. "mid_value": "0",
  21. "max_value": "10",
  22. "min_color":"ff0000",
  23. "mid_color":"00ff00",
  24. "max_color":"0000ff"
  25. }]`,
  26. rules: []*xlsxCfRule{{
  27. Priority: 1,
  28. Type: "colorScale",
  29. ColorScale: &xlsxColorScale{
  30. Cfvo: []*xlsxCfvo{{
  31. Type: "num",
  32. Val: -10,
  33. }, {
  34. Type: "num",
  35. Val: 0,
  36. }, {
  37. Type: "num",
  38. Val: 10,
  39. }},
  40. Color: []*xlsxColor{{
  41. RGB: "FFFF0000",
  42. }, {
  43. RGB: "FF00FF00",
  44. }, {
  45. RGB: "FF0000FF",
  46. }},
  47. },
  48. }},
  49. }, {
  50. label: "3_color_scale default min/mid/max",
  51. format: `[{
  52. "type":"3_color_scale",
  53. "criteria":"=",
  54. "min_type":"num",
  55. "mid_type":"num",
  56. "max_type":"num",
  57. "min_color":"ff0000",
  58. "mid_color":"00ff00",
  59. "max_color":"0000ff"
  60. }]`,
  61. rules: []*xlsxCfRule{{
  62. Priority: 1,
  63. Type: "colorScale",
  64. ColorScale: &xlsxColorScale{
  65. Cfvo: []*xlsxCfvo{{
  66. Type: "num",
  67. Val: 0,
  68. }, {
  69. Type: "num",
  70. Val: 50,
  71. }, {
  72. Type: "num",
  73. Val: 0,
  74. }},
  75. Color: []*xlsxColor{{
  76. RGB: "FFFF0000",
  77. }, {
  78. RGB: "FF00FF00",
  79. }, {
  80. RGB: "FF0000FF",
  81. }},
  82. },
  83. }},
  84. }, {
  85. label: "2_color_scale default min/max",
  86. format: `[{
  87. "type":"2_color_scale",
  88. "criteria":"=",
  89. "min_type":"num",
  90. "max_type":"num",
  91. "min_color":"ff0000",
  92. "max_color":"0000ff"
  93. }]`,
  94. rules: []*xlsxCfRule{{
  95. Priority: 1,
  96. Type: "colorScale",
  97. ColorScale: &xlsxColorScale{
  98. Cfvo: []*xlsxCfvo{{
  99. Type: "num",
  100. Val: 0,
  101. }, {
  102. Type: "num",
  103. Val: 0,
  104. }},
  105. Color: []*xlsxColor{{
  106. RGB: "FFFF0000",
  107. }, {
  108. RGB: "FF0000FF",
  109. }},
  110. },
  111. }},
  112. }}
  113. for _, testCase := range cases {
  114. xl := NewFile()
  115. const sheet = "Sheet1"
  116. const cellRange = "A1:A1"
  117. err := xl.SetConditionalFormat(sheet, cellRange, testCase.format)
  118. if err != nil {
  119. t.Fatalf("%s", err)
  120. }
  121. xlsx := xl.workSheetReader(sheet)
  122. cf := xlsx.ConditionalFormatting
  123. assert.Len(t, cf, 1, testCase.label)
  124. assert.Len(t, cf[0].CfRule, 1, testCase.label)
  125. assert.Equal(t, cellRange, cf[0].SQRef, testCase.label)
  126. assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label)
  127. }
  128. }