cell_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type StyleSuite struct {}
  6. var _ = Suite(&StyleSuite{})
  7. func (s *StyleSuite) TestNewStyle(c *C){
  8. style := NewStyle()
  9. c.Assert(style, NotNil)
  10. }
  11. type FontSuite struct {}
  12. var _ = Suite(&FontSuite{})
  13. func (s *FontSuite) TestNewFont(c *C) {
  14. font := NewFont(12, "Verdana")
  15. c.Assert(font, NotNil)
  16. c.Assert(font.Name, Equals, "Verdana")
  17. c.Assert(font.Size, Equals, 12)
  18. }
  19. type CellSuite struct {}
  20. var _ = Suite(&CellSuite{})
  21. // Test that we can set and get a Value from a Cell
  22. func (s *CellSuite) TestValueSet(c *C) {
  23. // Note, this test is fairly pointless, it serves mostly to
  24. // reinforce that this functionality is important, and should
  25. // the mechanics of this all change at some point, to remind
  26. // us not to lose this.
  27. cell := Cell{}
  28. cell.Value = "A string"
  29. c.Assert(cell.Value, Equals, "A string")
  30. }
  31. // Test that GetStyle correctly converts the xlsxStyle.Fonts.
  32. func (s *CellSuite) TestGetStyleWithFonts(c *C) {
  33. var cell *Cell
  34. var style Style
  35. var xStyles *xlsxStyles
  36. var fonts []xlsxFont
  37. var cellXfs []xlsxXf
  38. fonts = make([]xlsxFont, 1)
  39. fonts[0] = xlsxFont{
  40. Sz: xlsxVal{Val: "10"},
  41. Name: xlsxVal{Val: "Calibra"}}
  42. cellXfs = make([]xlsxXf, 1)
  43. cellXfs[0] = xlsxXf{ApplyFont: true, FontId: 0}
  44. xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
  45. cell = &Cell{Value: "123", styleIndex: 0, styles: xStyles}
  46. style = cell.GetStyle()
  47. c.Assert(style, NotNil)
  48. c.Assert(style.Font.Size, Equals, 10)
  49. c.Assert(style.Font.Name, Equals, "Calibra")
  50. }
  51. // Test that SetStyle correctly updates the xlsxStyle.Fonts.
  52. func (s *CellSuite) TestSetStyleWithFonts(c *C) {
  53. file := NewFile()
  54. sheet := file.AddSheet("Test")
  55. row := sheet.AddRow()
  56. cell := row.AddCell()
  57. font := NewFont(12, "Calibra")
  58. style := NewStyle()
  59. style.Font = *font
  60. cell.SetStyle(style)
  61. c.Assert(cell.styleIndex, Equals, 0)
  62. c.Assert(cell.styles.Fonts, HasLen, 1)
  63. xFont := cell.styles.Fonts[0]
  64. c.Assert(xFont.Sz.Val, Equals, "12")
  65. c.Assert(xFont.Name.Val, Equals, "Calibra")
  66. }
  67. // Test that GetStyle correctly converts the xlsxStyle.Fills.
  68. func (s *CellSuite) TestGetStyleWithFills(c *C) {
  69. var cell *Cell
  70. var style Style
  71. var xStyles *xlsxStyles
  72. var fills []xlsxFill
  73. var cellXfs []xlsxXf
  74. fills = make([]xlsxFill, 1)
  75. fills[0] = xlsxFill{
  76. PatternFill: xlsxPatternFill{
  77. PatternType: "solid",
  78. FgColor: xlsxColor{RGB: "FF000000"},
  79. BgColor: xlsxColor{RGB: "00FF0000"}}}
  80. cellXfs = make([]xlsxXf, 1)
  81. cellXfs[0] = xlsxXf{ApplyFill: true, FillId: 0}
  82. xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
  83. cell = &Cell{Value: "123", styleIndex: 0, styles: xStyles}
  84. style = cell.GetStyle()
  85. fill := style.Fill
  86. c.Assert(fill.PatternType, Equals, "solid")
  87. c.Assert(fill.BgColor, Equals, "00FF0000")
  88. c.Assert(fill.FgColor, Equals, "FF000000")
  89. }
  90. // Test that SetStyle correctly updates xlsxStyle.Fills.
  91. func (s *CellSuite) TestSetStyleWithFills(c *C) {
  92. file := NewFile()
  93. sheet := file.AddSheet("Test")
  94. row := sheet.AddRow()
  95. cell := row.AddCell()
  96. fill := NewFill("solid", "00FF0000", "FF000000")
  97. style := NewStyle()
  98. style.Fill = *fill
  99. cell.SetStyle(style)
  100. c.Assert(cell.styleIndex, Equals, 0)
  101. c.Assert(cell.styles.Fills, HasLen, 1)
  102. xFill := cell.styles.Fills[0]
  103. xPatternFill := xFill.PatternFill
  104. c.Assert(xPatternFill.PatternType, Equals, "solid")
  105. c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
  106. c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
  107. }
  108. // Test that GetStyle correctly converts the xlsxStyle.Borders.
  109. func (s *CellSuite) TestGetStyleWithBorders(c *C) {
  110. var cell *Cell
  111. var style Style
  112. var xStyles *xlsxStyles
  113. var borders []xlsxBorder
  114. var cellXfs []xlsxXf
  115. borders = make([]xlsxBorder, 1)
  116. borders[0] = xlsxBorder{
  117. Left: xlsxLine{Style: "thin"},
  118. Right: xlsxLine{Style: "thin"},
  119. Top: xlsxLine{Style: "thin"},
  120. Bottom: xlsxLine{Style: "thin"}}
  121. cellXfs = make([]xlsxXf, 1)
  122. cellXfs[0] = xlsxXf{ApplyBorder: true, BorderId: 0}
  123. xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
  124. cell = &Cell{Value: "123", styleIndex: 0, styles: xStyles}
  125. style = cell.GetStyle()
  126. border := style.Border
  127. c.Assert(border.Left, Equals, "thin")
  128. c.Assert(border.Right, Equals, "thin")
  129. c.Assert(border.Top, Equals, "thin")
  130. c.Assert(border.Bottom, Equals, "thin")
  131. }
  132. func (s *CellSuite) TestGetNumberFormat(c *C) {
  133. var cell *Cell
  134. var cellXfs []xlsxXf
  135. var numFmt xlsxNumFmt
  136. var numFmts []xlsxNumFmt
  137. var xStyles *xlsxStyles
  138. var numFmtRefTable map[int]xlsxNumFmt
  139. cellXfs = make([]xlsxXf, 1)
  140. cellXfs[0] = xlsxXf{NumFmtId: 0}
  141. numFmts = make([]xlsxNumFmt, 1)
  142. numFmtRefTable = make(map[int]xlsxNumFmt)
  143. xStyles = &xlsxStyles{NumFmts: numFmts, CellXfs: cellXfs}
  144. cell = &Cell{Value: "123.123", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
  145. numFmt = xlsxNumFmt{NumFmtId: 0, FormatCode: "dd/mm/yy"}
  146. numFmts[0] = numFmt
  147. numFmtRefTable[0] = numFmt
  148. c.Assert(cell.GetNumberFormat(), Equals, "dd/mm/yy")
  149. }
  150. // We can return a string representation of the formatted data
  151. func (l *CellSuite) TestFormattedValue(c *C) {
  152. var cell, earlyCell, negativeCell, smallCell *Cell
  153. var cellXfs []xlsxXf
  154. var numFmt xlsxNumFmt
  155. var numFmts []xlsxNumFmt
  156. var xStyles *xlsxStyles
  157. var numFmtRefTable map[int]xlsxNumFmt
  158. cellXfs = make([]xlsxXf, 1)
  159. cellXfs[0] = xlsxXf{NumFmtId: 1}
  160. numFmts = make([]xlsxNumFmt, 1)
  161. numFmtRefTable = make(map[int]xlsxNumFmt)
  162. xStyles = &xlsxStyles{NumFmts: numFmts, CellXfs: cellXfs}
  163. cell = &Cell{Value: "37947.7500001", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
  164. negativeCell = &Cell{Value: "-37947.7500001", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
  165. smallCell = &Cell{Value: "0.007", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
  166. earlyCell = &Cell{Value: "2.1", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
  167. setCode := func(code string) {
  168. numFmt = xlsxNumFmt{NumFmtId: 1, FormatCode: code}
  169. numFmts[0] = numFmt
  170. numFmtRefTable[1] = numFmt
  171. }
  172. setCode("general")
  173. c.Assert(cell.FormattedValue(), Equals, "37947.7500001")
  174. c.Assert(negativeCell.FormattedValue(), Equals, "-37947.7500001")
  175. setCode("0")
  176. c.Assert(cell.FormattedValue(), Equals, "37947")
  177. setCode("#,##0") // For the time being we're not doing this
  178. // comma formatting, so it'll fall back to
  179. // the related non-comma form.
  180. c.Assert(cell.FormattedValue(), Equals, "37947")
  181. setCode("0.00")
  182. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  183. setCode("#,##0.00") // For the time being we're not doing this
  184. // comma formatting, so it'll fall back to
  185. // the related non-comma form.
  186. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  187. setCode("#,##0 ;(#,##0)")
  188. c.Assert(cell.FormattedValue(), Equals, "37947")
  189. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  190. setCode("#,##0 ;[red](#,##0)")
  191. c.Assert(cell.FormattedValue(), Equals, "37947")
  192. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  193. setCode("0%")
  194. c.Assert(cell.FormattedValue(), Equals, "3794775%")
  195. setCode("0.00%")
  196. c.Assert(cell.FormattedValue(), Equals, "3794775.00%")
  197. setCode("0.00e+00")
  198. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  199. setCode("##0.0e+0") // This is wrong, but we'll use it for now.
  200. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  201. setCode("mm-dd-yy")
  202. c.Assert(cell.FormattedValue(), Equals, "11-22-03")
  203. setCode("d-mmm-yy")
  204. c.Assert(cell.FormattedValue(), Equals, "22-Nov-03")
  205. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-00")
  206. setCode("d-mmm")
  207. c.Assert(cell.FormattedValue(), Equals, "22-Nov")
  208. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan")
  209. setCode("mmm-yy")
  210. c.Assert(cell.FormattedValue(), Equals, "Nov-03")
  211. setCode("h:mm am/pm")
  212. c.Assert(cell.FormattedValue(), Equals, "6:00 pm")
  213. c.Assert(smallCell.FormattedValue(), Equals, "12:14 am")
  214. setCode("h:mm:ss am/pm")
  215. c.Assert(cell.FormattedValue(), Equals, "6:00:00 pm")
  216. c.Assert(smallCell.FormattedValue(), Equals, "12:14:47 am")
  217. setCode("h:mm")
  218. c.Assert(cell.FormattedValue(), Equals, "18:00")
  219. c.Assert(smallCell.FormattedValue(), Equals, "00:14")
  220. setCode("h:mm:ss")
  221. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  222. // This is wrong, but there's no eary way aroud it in Go right now, AFAICT.
  223. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  224. setCode("m/d/yy h:mm")
  225. c.Assert(cell.FormattedValue(), Equals, "11/22/03 18:00")
  226. c.Assert(smallCell.FormattedValue(), Equals, "12/30/99 00:14") // Note, that's 1899
  227. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00 02:24") // and 1900
  228. setCode("mm:ss")
  229. c.Assert(cell.FormattedValue(), Equals, "00:00")
  230. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  231. setCode("[h]:mm:ss")
  232. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  233. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  234. setCode("mmss.0") // I'm not sure about these.
  235. c.Assert(cell.FormattedValue(), Equals, "00.8640")
  236. c.Assert(smallCell.FormattedValue(), Equals, "1447.999997")
  237. setCode("yyyy\\-mm\\-dd")
  238. c.Assert(cell.FormattedValue(), Equals, "2003\\-11\\-22")
  239. setCode("dd/mm/yy")
  240. c.Assert(cell.FormattedValue(), Equals, "22/11/03")
  241. c.Assert(earlyCell.FormattedValue(), Equals, "01/01/00")
  242. setCode("hh:mm:ss")
  243. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  244. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  245. setCode("dd/mm/yy\\ hh:mm")
  246. c.Assert(cell.FormattedValue(), Equals, "22/11/03\\ 18:00")
  247. setCode("yy-mm-dd")
  248. c.Assert(cell.FormattedValue(), Equals, "03-11-22")
  249. setCode("d-mmm-yyyy")
  250. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  251. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-1900")
  252. setCode("m/d/yy")
  253. c.Assert(cell.FormattedValue(), Equals, "11/22/03")
  254. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00")
  255. setCode("m/d/yyyy")
  256. c.Assert(cell.FormattedValue(), Equals, "11/22/2003")
  257. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/1900")
  258. setCode("dd-mmm-yyyy")
  259. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  260. setCode("dd/mm/yyyy")
  261. c.Assert(cell.FormattedValue(), Equals, "22/11/2003")
  262. setCode("mm/dd/yy hh:mm am/pm")
  263. c.Assert(cell.FormattedValue(), Equals, "11/22/03 06:00 pm")
  264. setCode("mm/dd/yyyy hh:mm:ss")
  265. c.Assert(cell.FormattedValue(), Equals, "11/22/2003 18:00:00")
  266. c.Assert(smallCell.FormattedValue(), Equals, "12/30/1899 00:14:47")
  267. setCode("yyyy-mm-dd hh:mm:ss")
  268. c.Assert(cell.FormattedValue(), Equals, "2003-11-22 18:00:00")
  269. c.Assert(smallCell.FormattedValue(), Equals, "1899-12-30 00:14:47")
  270. }