styles_test.go 3.2 KB

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