styles_test.go 6.6 KB

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