styles_test.go 5.7 KB

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