styles_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package excelize
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestStyleFill(t *testing.T) {
  9. cases := []struct {
  10. label string
  11. format string
  12. expectFill bool
  13. }{{
  14. label: "no_fill",
  15. format: `{"alignment":{"wrap_text":true}}`,
  16. expectFill: false,
  17. }, {
  18. label: "fill",
  19. format: `{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`,
  20. expectFill: true,
  21. }}
  22. for _, testCase := range cases {
  23. xl := NewFile()
  24. styleID, err := xl.NewStyle(testCase.format)
  25. assert.NoError(t, err)
  26. styles := xl.stylesReader()
  27. style := styles.CellXfs.Xf[styleID]
  28. if testCase.expectFill {
  29. assert.NotEqual(t, *style.FillID, 0, testCase.label)
  30. } else {
  31. assert.Equal(t, *style.FillID, 0, testCase.label)
  32. }
  33. }
  34. f := NewFile()
  35. styleID1, err := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`)
  36. assert.NoError(t, err)
  37. styleID2, err := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`)
  38. assert.NoError(t, err)
  39. assert.Equal(t, styleID1, styleID2)
  40. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestStyleFill.xlsx")))
  41. }
  42. func TestSetConditionalFormat(t *testing.T) {
  43. cases := []struct {
  44. label string
  45. format string
  46. rules []*xlsxCfRule
  47. }{{
  48. label: "3_color_scale",
  49. format: `[{
  50. "type":"3_color_scale",
  51. "criteria":"=",
  52. "min_type":"num",
  53. "mid_type":"num",
  54. "max_type":"num",
  55. "min_value": "-10",
  56. "mid_value": "0",
  57. "max_value": "10",
  58. "min_color":"ff0000",
  59. "mid_color":"00ff00",
  60. "max_color":"0000ff"
  61. }]`,
  62. rules: []*xlsxCfRule{{
  63. Priority: 1,
  64. Type: "colorScale",
  65. ColorScale: &xlsxColorScale{
  66. Cfvo: []*xlsxCfvo{{
  67. Type: "num",
  68. Val: "-10",
  69. }, {
  70. Type: "num",
  71. Val: "0",
  72. }, {
  73. Type: "num",
  74. Val: "10",
  75. }},
  76. Color: []*xlsxColor{{
  77. RGB: "FFFF0000",
  78. }, {
  79. RGB: "FF00FF00",
  80. }, {
  81. RGB: "FF0000FF",
  82. }},
  83. },
  84. }},
  85. }, {
  86. label: "3_color_scale default min/mid/max",
  87. format: `[{
  88. "type":"3_color_scale",
  89. "criteria":"=",
  90. "min_type":"num",
  91. "mid_type":"num",
  92. "max_type":"num",
  93. "min_color":"ff0000",
  94. "mid_color":"00ff00",
  95. "max_color":"0000ff"
  96. }]`,
  97. rules: []*xlsxCfRule{{
  98. Priority: 1,
  99. Type: "colorScale",
  100. ColorScale: &xlsxColorScale{
  101. Cfvo: []*xlsxCfvo{{
  102. Type: "num",
  103. Val: "0",
  104. }, {
  105. Type: "num",
  106. Val: "50",
  107. }, {
  108. Type: "num",
  109. Val: "0",
  110. }},
  111. Color: []*xlsxColor{{
  112. RGB: "FFFF0000",
  113. }, {
  114. RGB: "FF00FF00",
  115. }, {
  116. RGB: "FF0000FF",
  117. }},
  118. },
  119. }},
  120. }, {
  121. label: "2_color_scale default min/max",
  122. format: `[{
  123. "type":"2_color_scale",
  124. "criteria":"=",
  125. "min_type":"num",
  126. "max_type":"num",
  127. "min_color":"ff0000",
  128. "max_color":"0000ff"
  129. }]`,
  130. rules: []*xlsxCfRule{{
  131. Priority: 1,
  132. Type: "colorScale",
  133. ColorScale: &xlsxColorScale{
  134. Cfvo: []*xlsxCfvo{{
  135. Type: "num",
  136. Val: "0",
  137. }, {
  138. Type: "num",
  139. Val: "0",
  140. }},
  141. Color: []*xlsxColor{{
  142. RGB: "FFFF0000",
  143. }, {
  144. RGB: "FF0000FF",
  145. }},
  146. },
  147. }},
  148. }}
  149. for _, testCase := range cases {
  150. xl := NewFile()
  151. const sheet = "Sheet1"
  152. const cellRange = "A1:A1"
  153. err := xl.SetConditionalFormat(sheet, cellRange, testCase.format)
  154. if err != nil {
  155. t.Fatalf("%s", err)
  156. }
  157. xlsx, err := xl.workSheetReader(sheet)
  158. assert.NoError(t, err)
  159. cf := xlsx.ConditionalFormatting
  160. assert.Len(t, cf, 1, testCase.label)
  161. assert.Len(t, cf[0].CfRule, 1, testCase.label)
  162. assert.Equal(t, cellRange, cf[0].SQRef, testCase.label)
  163. assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label)
  164. }
  165. }
  166. func TestUnsetConditionalFormat(t *testing.T) {
  167. f := NewFile()
  168. assert.NoError(t, f.SetCellValue("Sheet1", "A1", 7))
  169. assert.NoError(t, f.UnsetConditionalFormat("Sheet1", "A1:A10"))
  170. format, err := f.NewConditionalStyle(`{"font":{"color":"#9A0511"},"fill":{"type":"pattern","color":["#FEC7CE"],"pattern":1}}`)
  171. assert.NoError(t, err)
  172. assert.NoError(t, f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"6"}]`, format)))
  173. assert.NoError(t, f.UnsetConditionalFormat("Sheet1", "A1:A10"))
  174. // Test unset conditional format on not exists worksheet.
  175. assert.EqualError(t, f.UnsetConditionalFormat("SheetN", "A1:A10"), "sheet SheetN is not exist")
  176. // Save xlsx file by the given path.
  177. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestUnsetConditionalFormat.xlsx")))
  178. }
  179. func TestNewStyle(t *testing.T) {
  180. f := NewFile()
  181. styleID, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777"}}`)
  182. assert.NoError(t, err)
  183. styles := f.stylesReader()
  184. fontID := styles.CellXfs.Xf[styleID].FontID
  185. font := styles.Fonts.Font[*fontID]
  186. assert.Contains(t, *font.Name.Val, "Times New Roman", "Stored font should contain font name")
  187. assert.Equal(t, 2, styles.CellXfs.Count, "Should have 2 styles")
  188. _, err = f.NewStyle(&Style{})
  189. assert.NoError(t, err)
  190. _, err = f.NewStyle(Style{})
  191. assert.EqualError(t, err, "invalid parameter type")
  192. }
  193. func TestGetDefaultFont(t *testing.T) {
  194. f := NewFile()
  195. s := f.GetDefaultFont()
  196. assert.Equal(t, s, "Calibri", "Default font should be Calibri")
  197. }
  198. func TestSetDefaultFont(t *testing.T) {
  199. f := NewFile()
  200. f.SetDefaultFont("Ariel")
  201. styles := f.stylesReader()
  202. s := f.GetDefaultFont()
  203. assert.Equal(t, s, "Ariel", "Default font should change to Ariel")
  204. assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true)
  205. }
  206. func TestStylesReader(t *testing.T) {
  207. f := NewFile()
  208. // Test read styles with unsupport charset.
  209. f.Styles = nil
  210. f.XLSX["xl/styles.xml"] = MacintoshCyrillicCharset
  211. assert.EqualValues(t, new(xlsxStyleSheet), f.stylesReader())
  212. }
  213. func TestThemeReader(t *testing.T) {
  214. f := NewFile()
  215. // Test read theme with unsupport charset.
  216. f.XLSX["xl/theme/theme1.xml"] = MacintoshCyrillicCharset
  217. assert.EqualValues(t, new(xlsxTheme), f.themeReader())
  218. }
  219. func TestSetCellStyle(t *testing.T) {
  220. f := NewFile()
  221. // Test set cell style on not exists worksheet.
  222. assert.EqualError(t, f.SetCellStyle("SheetN", "A1", "A2", 1), "sheet SheetN is not exist")
  223. }
  224. func TestGetStyleID(t *testing.T) {
  225. assert.Equal(t, -1, NewFile().getStyleID(&xlsxStyleSheet{}, nil))
  226. }
  227. func TestGetFillID(t *testing.T) {
  228. assert.Equal(t, -1, getFillID(NewFile().stylesReader(), &Style{Fill: Fill{Type: "unknown"}}))
  229. }