xmlStyle_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type XMLStyleSuite struct{}
  6. var _ = Suite(&XMLStyleSuite{})
  7. // Test we produce valid output for an empty style file.
  8. func (x *XMLStyleSuite) TestMarshalEmptyXlsxStyleSheet(c *C) {
  9. styles := newXlsxStyleSheet(nil)
  10. result, err := styles.Marshal()
  11. c.Assert(err, IsNil)
  12. c.Assert(string(result), Equals, `<?xml version="1.0" encoding="UTF-8"?>
  13. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"></styleSheet>`)
  14. }
  15. // Test we produce valid output for a style file with one font definition.
  16. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithAFont(c *C) {
  17. styles := newXlsxStyleSheet(nil)
  18. styles.Fonts = xlsxFonts{}
  19. styles.Fonts.Count = 1
  20. styles.Fonts.Font = make([]xlsxFont, 1)
  21. font := xlsxFont{}
  22. font.Sz.Val = "10"
  23. font.Name.Val = "Andale Mono"
  24. font.B = &xlsxVal{}
  25. font.I = &xlsxVal{}
  26. font.U = &xlsxVal{}
  27. styles.Fonts.Font[0] = font
  28. expected := `<?xml version="1.0" encoding="UTF-8"?>
  29. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="10"/><name val="Andale Mono"/><b/><i/><u/></font></fonts></styleSheet>`
  30. result, err := styles.Marshal()
  31. c.Assert(err, IsNil)
  32. c.Assert(string(result), Equals, expected)
  33. }
  34. // Test we produce valid output for a style file with one fill definition.
  35. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithAFill(c *C) {
  36. styles := newXlsxStyleSheet(nil)
  37. styles.Fills = xlsxFills{}
  38. styles.Fills.Count = 1
  39. styles.Fills.Fill = make([]xlsxFill, 1)
  40. fill := xlsxFill{}
  41. patternFill := xlsxPatternFill{
  42. PatternType: "solid",
  43. FgColor: xlsxColor{RGB: "#FFFFFF"},
  44. BgColor: xlsxColor{RGB: "#000000"}}
  45. fill.PatternFill = patternFill
  46. styles.Fills.Fill[0] = fill
  47. expected := `<?xml version="1.0" encoding="UTF-8"?>
  48. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fills count="1"><fill><patternFill patternType="solid"><fgColor rgb="#FFFFFF"/><bgColor rgb="#000000"/></patternFill></fill></fills></styleSheet>`
  49. result, err := styles.Marshal()
  50. c.Assert(err, IsNil)
  51. c.Assert(string(result), Equals, expected)
  52. }
  53. // Test we produce valid output for a style file with one border definition.
  54. // Empty elements are required to accommodate for Excel quirks.
  55. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithABorder(c *C) {
  56. styles := newXlsxStyleSheet(nil)
  57. styles.Borders = xlsxBorders{}
  58. styles.Borders.Count = 1
  59. styles.Borders.Border = make([]xlsxBorder, 1)
  60. border := xlsxBorder{}
  61. border.Left.Style = "solid"
  62. border.Top.Style = ""
  63. styles.Borders.Border[0] = border
  64. expected := `<?xml version="1.0" encoding="UTF-8"?>
  65. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><borders count="1"><border><left style="solid"></left><right style=""></right><top style=""></top><bottom style=""></bottom></border></borders></styleSheet>`
  66. result, err := styles.Marshal()
  67. c.Assert(err, IsNil)
  68. c.Assert(string(result), Equals, expected)
  69. }
  70. // Test we produce valid output for a style file with one cellStyleXf definition.
  71. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyleXf(c *C) {
  72. styles := newXlsxStyleSheet(nil)
  73. styles.CellStyleXfs = &xlsxCellStyleXfs{}
  74. styles.CellStyleXfs.Count = 1
  75. styles.CellStyleXfs.Xf = make([]xlsxXf, 1)
  76. xf := xlsxXf{}
  77. xf.ApplyAlignment = true
  78. xf.ApplyBorder = true
  79. xf.ApplyFont = true
  80. xf.ApplyFill = true
  81. xf.ApplyProtection = true
  82. xf.BorderId = 0
  83. xf.FillId = 0
  84. xf.FontId = 0
  85. xf.NumFmtId = 0
  86. xf.Alignment = xlsxAlignment{
  87. Horizontal: "left",
  88. Indent: 1,
  89. ShrinkToFit: true,
  90. TextRotation: 0,
  91. Vertical: "middle",
  92. WrapText: false}
  93. styles.CellStyleXfs.Xf[0] = xf
  94. expected := `<?xml version="1.0" encoding="UTF-8"?>
  95. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellStyleXfs count="1"><xf applyAlignment="1" applyBorder="1" applyFont="1" applyFill="1" applyNumberFormat="0" applyProtection="1" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="1" shrinkToFit="1" textRotation="0" vertical="middle" wrapText="0"/></xf></cellStyleXfs></styleSheet>`
  96. result, err := styles.Marshal()
  97. c.Assert(err, IsNil)
  98. c.Assert(string(result), Equals, expected)
  99. }
  100. // Test we produce valid output for a style file with one cellStyle definition.
  101. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellStyle(c *C) {
  102. var builtInId int
  103. styles := newXlsxStyleSheet(nil)
  104. styles.CellStyles = &xlsxCellStyles{Count: 1}
  105. styles.CellStyles.CellStyle = make([]xlsxCellStyle, 1)
  106. builtInId = 31
  107. styles.CellStyles.CellStyle[0] = xlsxCellStyle{
  108. Name: "Bob",
  109. BuiltInId: &builtInId, // XXX Todo - work out built-ins!
  110. XfId: 0,
  111. }
  112. expected := `<?xml version="1.0" encoding="UTF-8"?>
  113. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellStyles count="1"><cellStyle builtInId="31" name="Bob" xfId="0"></cellStyle></cellStyles></styleSheet>`
  114. result, err := styles.Marshal()
  115. c.Assert(err, IsNil)
  116. c.Assert(string(result), Equals, expected)
  117. }
  118. // Test we produce valid output for a style file with one cellXf
  119. // definition.
  120. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithACellXf(c *C) {
  121. styles := newXlsxStyleSheet(nil)
  122. styles.CellXfs = xlsxCellXfs{}
  123. styles.CellXfs.Count = 1
  124. styles.CellXfs.Xf = make([]xlsxXf, 1)
  125. xf := xlsxXf{}
  126. xf.ApplyAlignment = true
  127. xf.ApplyBorder = true
  128. xf.ApplyFont = true
  129. xf.ApplyFill = true
  130. xf.ApplyNumberFormat = true
  131. xf.ApplyProtection = true
  132. xf.BorderId = 0
  133. xf.FillId = 0
  134. xf.FontId = 0
  135. xf.NumFmtId = 0
  136. xf.Alignment = xlsxAlignment{
  137. Horizontal: "left",
  138. Indent: 1,
  139. ShrinkToFit: true,
  140. TextRotation: 0,
  141. Vertical: "middle",
  142. WrapText: false}
  143. styles.CellXfs.Xf[0] = xf
  144. expected := `<?xml version="1.0" encoding="UTF-8"?>
  145. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><cellXfs count="1"><xf applyAlignment="1" applyBorder="1" applyFont="1" applyFill="1" applyNumberFormat="1" applyProtection="1" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="1" shrinkToFit="1" textRotation="0" vertical="middle" wrapText="0"/></xf></cellXfs></styleSheet>`
  146. result, err := styles.Marshal()
  147. c.Assert(err, IsNil)
  148. c.Assert(string(result), Equals, expected)
  149. }
  150. // Test we produce valid output for a style file with one NumFmt
  151. // definition.
  152. func (x *XMLStyleSuite) TestMarshalXlsxStyleSheetWithANumFmt(c *C) {
  153. styles := &xlsxStyleSheet{}
  154. styles.NumFmts = xlsxNumFmts{}
  155. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  156. numFmt := xlsxNumFmt{NumFmtId: 164, FormatCode: "GENERAL"}
  157. styles.addNumFmt(numFmt)
  158. expected := `<?xml version="1.0" encoding="UTF-8"?>
  159. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><numFmts count="1"><numFmt numFmtId="164" formatCode="GENERAL"/></numFmts></styleSheet>`
  160. result, err := styles.Marshal()
  161. c.Assert(err, IsNil)
  162. c.Assert(string(result), Equals, expected)
  163. }
  164. func (x *XMLStyleSuite) TestFontEquals(c *C) {
  165. fontA := xlsxFont{Sz: xlsxVal{Val: "11"},
  166. Color: xlsxColor{RGB: "FFFF0000"},
  167. Name: xlsxVal{Val: "Calibri"},
  168. Family: xlsxVal{Val: "2"},
  169. B: &xlsxVal{},
  170. I: &xlsxVal{},
  171. U: &xlsxVal{}}
  172. fontB := xlsxFont{Sz: xlsxVal{Val: "11"},
  173. Color: xlsxColor{RGB: "FFFF0000"},
  174. Name: xlsxVal{Val: "Calibri"},
  175. Family: xlsxVal{Val: "2"},
  176. B: &xlsxVal{},
  177. I: &xlsxVal{},
  178. U: &xlsxVal{}}
  179. c.Assert(fontA.Equals(fontB), Equals, true)
  180. fontB.Sz.Val = "12"
  181. c.Assert(fontA.Equals(fontB), Equals, false)
  182. fontB.Sz.Val = "11"
  183. fontB.Color.RGB = "12345678"
  184. c.Assert(fontA.Equals(fontB), Equals, false)
  185. fontB.Color.RGB = "FFFF0000"
  186. fontB.Name.Val = "Arial"
  187. c.Assert(fontA.Equals(fontB), Equals, false)
  188. fontB.Name.Val = "Calibri"
  189. fontB.Family.Val = "1"
  190. c.Assert(fontA.Equals(fontB), Equals, false)
  191. fontB.Family.Val = "2"
  192. fontB.B = nil
  193. c.Assert(fontA.Equals(fontB), Equals, false)
  194. fontB.B = &xlsxVal{}
  195. fontB.I = nil
  196. c.Assert(fontA.Equals(fontB), Equals, false)
  197. fontB.I = &xlsxVal{}
  198. fontB.U = nil
  199. c.Assert(fontA.Equals(fontB), Equals, false)
  200. fontB.U = &xlsxVal{}
  201. // For sanity
  202. c.Assert(fontA.Equals(fontB), Equals, true)
  203. }
  204. func (x *XMLStyleSuite) TestFillEquals(c *C) {
  205. fillA := xlsxFill{PatternFill: xlsxPatternFill{
  206. PatternType: "solid",
  207. FgColor: xlsxColor{RGB: "FFFF0000"},
  208. BgColor: xlsxColor{RGB: "0000FFFF"}}}
  209. fillB := xlsxFill{PatternFill: xlsxPatternFill{
  210. PatternType: "solid",
  211. FgColor: xlsxColor{RGB: "FFFF0000"},
  212. BgColor: xlsxColor{RGB: "0000FFFF"}}}
  213. c.Assert(fillA.Equals(fillB), Equals, true)
  214. fillB.PatternFill.PatternType = "gray125"
  215. c.Assert(fillA.Equals(fillB), Equals, false)
  216. fillB.PatternFill.PatternType = "solid"
  217. fillB.PatternFill.FgColor.RGB = "00FF00FF"
  218. c.Assert(fillA.Equals(fillB), Equals, false)
  219. fillB.PatternFill.FgColor.RGB = "FFFF0000"
  220. fillB.PatternFill.BgColor.RGB = "12456789"
  221. c.Assert(fillA.Equals(fillB), Equals, false)
  222. fillB.PatternFill.BgColor.RGB = "0000FFFF"
  223. // For sanity
  224. c.Assert(fillA.Equals(fillB), Equals, true)
  225. }
  226. func (x *XMLStyleSuite) TestBorderEquals(c *C) {
  227. borderA := xlsxBorder{Left: xlsxLine{Style: "none"},
  228. Right: xlsxLine{Style: "none"},
  229. Top: xlsxLine{Style: "none"},
  230. Bottom: xlsxLine{Style: "none"}}
  231. borderB := xlsxBorder{Left: xlsxLine{Style: "none"},
  232. Right: xlsxLine{Style: "none"},
  233. Top: xlsxLine{Style: "none"},
  234. Bottom: xlsxLine{Style: "none"}}
  235. c.Assert(borderA.Equals(borderB), Equals, true)
  236. borderB.Left.Style = "thin"
  237. c.Assert(borderA.Equals(borderB), Equals, false)
  238. borderB.Left.Style = "none"
  239. borderB.Right.Style = "thin"
  240. c.Assert(borderA.Equals(borderB), Equals, false)
  241. borderB.Right.Style = "none"
  242. borderB.Top.Style = "thin"
  243. c.Assert(borderA.Equals(borderB), Equals, false)
  244. borderB.Top.Style = "none"
  245. borderB.Bottom.Style = "thin"
  246. c.Assert(borderA.Equals(borderB), Equals, false)
  247. borderB.Bottom.Style = "none"
  248. // for sanity
  249. c.Assert(borderA.Equals(borderB), Equals, true)
  250. }
  251. func (x *XMLStyleSuite) TestXfEquals(c *C) {
  252. xfA := xlsxXf{
  253. ApplyAlignment: true,
  254. ApplyBorder: true,
  255. ApplyFont: true,
  256. ApplyFill: true,
  257. ApplyProtection: true,
  258. BorderId: 0,
  259. FillId: 0,
  260. FontId: 0,
  261. NumFmtId: 0}
  262. xfB := xlsxXf{
  263. ApplyAlignment: true,
  264. ApplyBorder: true,
  265. ApplyFont: true,
  266. ApplyFill: true,
  267. ApplyProtection: true,
  268. BorderId: 0,
  269. FillId: 0,
  270. FontId: 0,
  271. NumFmtId: 0}
  272. c.Assert(xfA.Equals(xfB), Equals, true)
  273. xfB.ApplyAlignment = false
  274. c.Assert(xfA.Equals(xfB), Equals, false)
  275. xfB.ApplyAlignment = true
  276. xfB.ApplyBorder = false
  277. c.Assert(xfA.Equals(xfB), Equals, false)
  278. xfB.ApplyBorder = true
  279. xfB.ApplyFont = false
  280. c.Assert(xfA.Equals(xfB), Equals, false)
  281. xfB.ApplyFont = true
  282. xfB.ApplyFill = false
  283. c.Assert(xfA.Equals(xfB), Equals, false)
  284. xfB.ApplyFill = true
  285. xfB.ApplyProtection = false
  286. c.Assert(xfA.Equals(xfB), Equals, false)
  287. xfB.ApplyProtection = true
  288. xfB.BorderId = 1
  289. c.Assert(xfA.Equals(xfB), Equals, false)
  290. xfB.BorderId = 0
  291. xfB.FillId = 1
  292. c.Assert(xfA.Equals(xfB), Equals, false)
  293. xfB.FillId = 0
  294. xfB.FontId = 1
  295. c.Assert(xfA.Equals(xfB), Equals, false)
  296. xfB.FontId = 0
  297. xfB.NumFmtId = 1
  298. c.Assert(xfA.Equals(xfB), Equals, false)
  299. xfB.NumFmtId = 0
  300. // for sanity
  301. c.Assert(xfA.Equals(xfB), Equals, true)
  302. var i1 int = 1
  303. xfA.XfId = &i1
  304. c.Assert(xfA.Equals(xfB), Equals, false)
  305. xfB.XfId = &i1
  306. c.Assert(xfA.Equals(xfB), Equals, true)
  307. var i2 int = 1
  308. xfB.XfId = &i2
  309. c.Assert(xfA.Equals(xfB), Equals, true)
  310. i2 = 2
  311. c.Assert(xfA.Equals(xfB), Equals, false)
  312. }
  313. func (s *CellSuite) TestNewNumFmt(c *C) {
  314. styles := newXlsxStyleSheet(nil)
  315. styles.NumFmts = xlsxNumFmts{}
  316. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  317. c.Assert(styles.newNumFmt("0"), DeepEquals, xlsxNumFmt{1, "0"})
  318. c.Assert(styles.newNumFmt("0.00e+00"), DeepEquals, xlsxNumFmt{11, "0.00e+00"})
  319. c.Assert(styles.newNumFmt("mm-dd-yy"), DeepEquals, xlsxNumFmt{14, "mm-dd-yy"})
  320. c.Assert(styles.newNumFmt("hh:mm:ss"), DeepEquals, xlsxNumFmt{164, "hh:mm:ss"})
  321. c.Assert(len(styles.NumFmts.NumFmt), Equals, 1)
  322. }
  323. func (s *CellSuite) TestAddNumFmt(c *C) {
  324. styles := &xlsxStyleSheet{}
  325. styles.NumFmts = xlsxNumFmts{}
  326. styles.NumFmts.NumFmt = make([]xlsxNumFmt, 0)
  327. styles.addNumFmt(xlsxNumFmt{1, "0"})
  328. c.Assert(styles.NumFmts.Count, Equals, 0)
  329. styles.addNumFmt(xlsxNumFmt{14, "mm-dd-yy"})
  330. c.Assert(styles.NumFmts.Count, Equals, 0)
  331. styles.addNumFmt(xlsxNumFmt{164, "hh:mm:ss"})
  332. c.Assert(styles.NumFmts.Count, Equals, 1)
  333. styles.addNumFmt(xlsxNumFmt{165, "yyyy/mm/dd"})
  334. c.Assert(styles.NumFmts.Count, Equals, 2)
  335. styles.addNumFmt(xlsxNumFmt{165, "yyyy/mm/dd"})
  336. c.Assert(styles.NumFmts.Count, Equals, 2)
  337. }