styles_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package excelize
  2. import (
  3. "fmt"
  4. "math"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestStyleFill(t *testing.T) {
  11. cases := []struct {
  12. label string
  13. format string
  14. expectFill bool
  15. }{{
  16. label: "no_fill",
  17. format: `{"alignment":{"wrap_text":true}}`,
  18. expectFill: false,
  19. }, {
  20. label: "fill",
  21. format: `{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`,
  22. expectFill: true,
  23. }}
  24. for _, testCase := range cases {
  25. xl := NewFile()
  26. styleID, err := xl.NewStyle(testCase.format)
  27. assert.NoError(t, err)
  28. styles := xl.stylesReader()
  29. style := styles.CellXfs.Xf[styleID]
  30. if testCase.expectFill {
  31. assert.NotEqual(t, *style.FillID, 0, testCase.label)
  32. } else {
  33. assert.Equal(t, *style.FillID, 0, testCase.label)
  34. }
  35. }
  36. f := NewFile()
  37. styleID1, err := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`)
  38. assert.NoError(t, err)
  39. styleID2, err := f.NewStyle(`{"fill":{"type":"pattern","pattern":1,"color":["#000000"]}}`)
  40. assert.NoError(t, err)
  41. assert.Equal(t, styleID1, styleID2)
  42. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestStyleFill.xlsx")))
  43. }
  44. func TestSetConditionalFormat(t *testing.T) {
  45. cases := []struct {
  46. label string
  47. format string
  48. rules []*xlsxCfRule
  49. }{{
  50. label: "3_color_scale",
  51. format: `[{
  52. "type":"3_color_scale",
  53. "criteria":"=",
  54. "min_type":"num",
  55. "mid_type":"num",
  56. "max_type":"num",
  57. "min_value": "-10",
  58. "mid_value": "0",
  59. "max_value": "10",
  60. "min_color":"ff0000",
  61. "mid_color":"00ff00",
  62. "max_color":"0000ff"
  63. }]`,
  64. rules: []*xlsxCfRule{{
  65. Priority: 1,
  66. Type: "colorScale",
  67. ColorScale: &xlsxColorScale{
  68. Cfvo: []*xlsxCfvo{{
  69. Type: "num",
  70. Val: "-10",
  71. }, {
  72. Type: "num",
  73. Val: "0",
  74. }, {
  75. Type: "num",
  76. Val: "10",
  77. }},
  78. Color: []*xlsxColor{{
  79. RGB: "FFFF0000",
  80. }, {
  81. RGB: "FF00FF00",
  82. }, {
  83. RGB: "FF0000FF",
  84. }},
  85. },
  86. }},
  87. }, {
  88. label: "3_color_scale default min/mid/max",
  89. format: `[{
  90. "type":"3_color_scale",
  91. "criteria":"=",
  92. "min_type":"num",
  93. "mid_type":"num",
  94. "max_type":"num",
  95. "min_color":"ff0000",
  96. "mid_color":"00ff00",
  97. "max_color":"0000ff"
  98. }]`,
  99. rules: []*xlsxCfRule{{
  100. Priority: 1,
  101. Type: "colorScale",
  102. ColorScale: &xlsxColorScale{
  103. Cfvo: []*xlsxCfvo{{
  104. Type: "num",
  105. Val: "0",
  106. }, {
  107. Type: "num",
  108. Val: "50",
  109. }, {
  110. Type: "num",
  111. Val: "0",
  112. }},
  113. Color: []*xlsxColor{{
  114. RGB: "FFFF0000",
  115. }, {
  116. RGB: "FF00FF00",
  117. }, {
  118. RGB: "FF0000FF",
  119. }},
  120. },
  121. }},
  122. }, {
  123. label: "2_color_scale default min/max",
  124. format: `[{
  125. "type":"2_color_scale",
  126. "criteria":"=",
  127. "min_type":"num",
  128. "max_type":"num",
  129. "min_color":"ff0000",
  130. "max_color":"0000ff"
  131. }]`,
  132. rules: []*xlsxCfRule{{
  133. Priority: 1,
  134. Type: "colorScale",
  135. ColorScale: &xlsxColorScale{
  136. Cfvo: []*xlsxCfvo{{
  137. Type: "num",
  138. Val: "0",
  139. }, {
  140. Type: "num",
  141. Val: "0",
  142. }},
  143. Color: []*xlsxColor{{
  144. RGB: "FFFF0000",
  145. }, {
  146. RGB: "FF0000FF",
  147. }},
  148. },
  149. }},
  150. }}
  151. for _, testCase := range cases {
  152. f := NewFile()
  153. const sheet = "Sheet1"
  154. const cellRange = "A1:A1"
  155. err := f.SetConditionalFormat(sheet, cellRange, testCase.format)
  156. if err != nil {
  157. t.Fatalf("%s", err)
  158. }
  159. ws, err := f.workSheetReader(sheet)
  160. assert.NoError(t, err)
  161. cf := ws.ConditionalFormatting
  162. assert.Len(t, cf, 1, testCase.label)
  163. assert.Len(t, cf[0].CfRule, 1, testCase.label)
  164. assert.Equal(t, cellRange, cf[0].SQRef, testCase.label)
  165. assert.EqualValues(t, testCase.rules, cf[0].CfRule, testCase.label)
  166. }
  167. }
  168. func TestUnsetConditionalFormat(t *testing.T) {
  169. f := NewFile()
  170. assert.NoError(t, f.SetCellValue("Sheet1", "A1", 7))
  171. assert.NoError(t, f.UnsetConditionalFormat("Sheet1", "A1:A10"))
  172. format, err := f.NewConditionalStyle(`{"font":{"color":"#9A0511"},"fill":{"type":"pattern","color":["#FEC7CE"],"pattern":1}}`)
  173. assert.NoError(t, err)
  174. assert.NoError(t, f.SetConditionalFormat("Sheet1", "A1:A10", fmt.Sprintf(`[{"type":"cell","criteria":">","format":%d,"value":"6"}]`, format)))
  175. assert.NoError(t, f.UnsetConditionalFormat("Sheet1", "A1:A10"))
  176. // Test unset conditional format on not exists worksheet.
  177. assert.EqualError(t, f.UnsetConditionalFormat("SheetN", "A1:A10"), "sheet SheetN is not exist")
  178. // Save spreadsheet by the given path.
  179. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestUnsetConditionalFormat.xlsx")))
  180. }
  181. func TestNewStyle(t *testing.T) {
  182. f := NewFile()
  183. styleID, err := f.NewStyle(`{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777"}}`)
  184. assert.NoError(t, err)
  185. styles := f.stylesReader()
  186. fontID := styles.CellXfs.Xf[styleID].FontID
  187. font := styles.Fonts.Font[*fontID]
  188. assert.Contains(t, *font.Name.Val, "Times New Roman", "Stored font should contain font name")
  189. assert.Equal(t, 2, styles.CellXfs.Count, "Should have 2 styles")
  190. _, err = f.NewStyle(&Style{})
  191. assert.NoError(t, err)
  192. _, err = f.NewStyle(Style{})
  193. assert.EqualError(t, err, ErrParameterInvalid.Error())
  194. _, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
  195. assert.EqualError(t, err, "the length of the font family name must be smaller than or equal to 31")
  196. _, err = f.NewStyle(&Style{Font: &Font{Size: MaxFontSize + 1}})
  197. assert.EqualError(t, err, "font size must be between 1 and 409 points")
  198. // new numeric custom style
  199. fmt := "####;####"
  200. f.Styles.NumFmts = nil
  201. styleID, err = f.NewStyle(&Style{
  202. CustomNumFmt: &fmt,
  203. })
  204. assert.NoError(t, err)
  205. assert.Equal(t, 2, styleID)
  206. assert.NotNil(t, f.Styles)
  207. assert.NotNil(t, f.Styles.CellXfs)
  208. assert.NotNil(t, f.Styles.CellXfs.Xf)
  209. nf := f.Styles.CellXfs.Xf[styleID]
  210. assert.Equal(t, 164, *nf.NumFmtID)
  211. // new currency custom style
  212. f.Styles.NumFmts = nil
  213. styleID, err = f.NewStyle(&Style{
  214. Lang: "ko-kr",
  215. NumFmt: 32, // must not be in currencyNumFmt
  216. })
  217. assert.NoError(t, err)
  218. assert.Equal(t, 3, styleID)
  219. assert.NotNil(t, f.Styles)
  220. assert.NotNil(t, f.Styles.CellXfs)
  221. assert.NotNil(t, f.Styles.CellXfs.Xf)
  222. nf = f.Styles.CellXfs.Xf[styleID]
  223. assert.Equal(t, 32, *nf.NumFmtID)
  224. }
  225. func TestGetDefaultFont(t *testing.T) {
  226. f := NewFile()
  227. s := f.GetDefaultFont()
  228. assert.Equal(t, s, "Calibri", "Default font should be Calibri")
  229. }
  230. func TestSetDefaultFont(t *testing.T) {
  231. f := NewFile()
  232. f.SetDefaultFont("Ariel")
  233. styles := f.stylesReader()
  234. s := f.GetDefaultFont()
  235. assert.Equal(t, s, "Ariel", "Default font should change to Ariel")
  236. assert.Equal(t, *styles.CellStyles.CellStyle[0].CustomBuiltIn, true)
  237. }
  238. func TestStylesReader(t *testing.T) {
  239. f := NewFile()
  240. // Test read styles with unsupported charset.
  241. f.Styles = nil
  242. f.Pkg.Store("xl/styles.xml", MacintoshCyrillicCharset)
  243. assert.EqualValues(t, new(xlsxStyleSheet), f.stylesReader())
  244. }
  245. func TestThemeReader(t *testing.T) {
  246. f := NewFile()
  247. // Test read theme with unsupported charset.
  248. f.Pkg.Store("xl/theme/theme1.xml", MacintoshCyrillicCharset)
  249. assert.EqualValues(t, new(xlsxTheme), f.themeReader())
  250. }
  251. func TestSetCellStyle(t *testing.T) {
  252. f := NewFile()
  253. // Test set cell style on not exists worksheet.
  254. assert.EqualError(t, f.SetCellStyle("SheetN", "A1", "A2", 1), "sheet SheetN is not exist")
  255. }
  256. func TestGetStyleID(t *testing.T) {
  257. assert.Equal(t, -1, NewFile().getStyleID(&xlsxStyleSheet{}, nil))
  258. }
  259. func TestGetFillID(t *testing.T) {
  260. assert.Equal(t, -1, getFillID(NewFile().stylesReader(), &Style{Fill: Fill{Type: "unknown"}}))
  261. }
  262. func TestParseTime(t *testing.T) {
  263. assert.Equal(t, "2019", parseTime("43528", "YYYY"))
  264. assert.Equal(t, "43528", parseTime("43528", ""))
  265. assert.Equal(t, "2019-03-04 05:05:42", parseTime("43528.2123", "YYYY-MM-DD hh:mm:ss"))
  266. assert.Equal(t, "2019-03-04 05:05:42", parseTime("43528.2123", "YYYY-MM-DD hh:mm:ss;YYYY-MM-DD hh:mm:ss"))
  267. assert.Equal(t, "3/4/2019 5:5:42", parseTime("43528.2123", "M/D/YYYY h:m:s"))
  268. assert.Equal(t, "3/4/2019 0:5:42", parseTime("43528.003958333335", "m/d/yyyy h:m:s"))
  269. assert.Equal(t, "3/4/2019 0:05:42", parseTime("43528.003958333335", "M/D/YYYY h:mm:s"))
  270. assert.Equal(t, "0:05", parseTime("43528.003958333335", "h:mm"))
  271. assert.Equal(t, "0:0", parseTime("6.9444444444444444E-5", "h:m"))
  272. assert.Equal(t, "0:00", parseTime("6.9444444444444444E-5", "h:mm"))
  273. assert.Equal(t, "0:0", parseTime("6.9444444444444444E-5", "h:m"))
  274. assert.Equal(t, "12:1", parseTime("0.50070601851851848", "h:m"))
  275. assert.Equal(t, "23:30", parseTime("0.97952546296296295", "h:m"))
  276. assert.Equal(t, "March", parseTime("43528", "mmmm"))
  277. assert.Equal(t, "Monday", parseTime("43528", "dddd"))
  278. }
  279. func TestThemeColor(t *testing.T) {
  280. for _, clr := range [][]string{
  281. {"FF000000", ThemeColor("000000", -0.1)},
  282. {"FF000000", ThemeColor("000000", 0)},
  283. {"FF33FF33", ThemeColor("00FF00", 0.2)},
  284. {"FFFFFFFF", ThemeColor("000000", 1)},
  285. {"FFFFFFFF", ThemeColor(strings.Repeat(string(rune(math.MaxUint8+1)), 6), 1)},
  286. {"FFFFFFFF", ThemeColor(strings.Repeat(string(rune(-1)), 6), 1)},
  287. } {
  288. assert.Equal(t, clr[0], clr[1])
  289. }
  290. }