cell_test.go 9.9 KB

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