cell_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. font := NewFont(10, "Calibra")
  20. style := NewStyle()
  21. style.Font = *font
  22. cell := &Cell{Value: "123", style: style}
  23. style = cell.GetStyle()
  24. c.Assert(style, NotNil)
  25. c.Assert(style.Font.Size, Equals, 10)
  26. c.Assert(style.Font.Name, Equals, "Calibra")
  27. }
  28. // Test that SetStyle correctly translates into a xlsxFont element
  29. func (s *CellSuite) TestSetStyleWithFonts(c *C) {
  30. file := NewFile()
  31. sheet := file.AddSheet("Test")
  32. row := sheet.AddRow()
  33. cell := row.AddCell()
  34. font := NewFont(12, "Calibra")
  35. style := NewStyle()
  36. style.Font = *font
  37. cell.SetStyle(style)
  38. style = cell.GetStyle()
  39. xFont, _, _, _, _ := style.makeXLSXStyleElements()
  40. c.Assert(xFont.Sz.Val, Equals, "12")
  41. c.Assert(xFont.Name.Val, Equals, "Calibra")
  42. }
  43. // Test that GetStyle correctly converts the xlsxStyle.Fills.
  44. func (s *CellSuite) TestGetStyleWithFills(c *C) {
  45. fill := *NewFill("solid", "FF000000", "00FF0000")
  46. style := NewStyle()
  47. style.Fill = fill
  48. cell := &Cell{Value: "123", style: style}
  49. style = cell.GetStyle()
  50. _, xFill, _, _, _ := style.makeXLSXStyleElements()
  51. c.Assert(xFill.PatternFill.PatternType, Equals, "solid")
  52. c.Assert(xFill.PatternFill.BgColor.RGB, Equals, "00FF0000")
  53. c.Assert(xFill.PatternFill.FgColor.RGB, Equals, "FF000000")
  54. }
  55. // Test that SetStyle correctly updates xlsxStyle.Fills.
  56. func (s *CellSuite) TestSetStyleWithFills(c *C) {
  57. file := NewFile()
  58. sheet := file.AddSheet("Test")
  59. row := sheet.AddRow()
  60. cell := row.AddCell()
  61. fill := NewFill("solid", "00FF0000", "FF000000")
  62. style := NewStyle()
  63. style.Fill = *fill
  64. cell.SetStyle(style)
  65. style = cell.GetStyle()
  66. _, xFill, _, _, _ := style.makeXLSXStyleElements()
  67. xPatternFill := xFill.PatternFill
  68. c.Assert(xPatternFill.PatternType, Equals, "solid")
  69. c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
  70. c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
  71. }
  72. // Test that GetStyle correctly converts the xlsxStyle.Borders.
  73. func (s *CellSuite) TestGetStyleWithBorders(c *C) {
  74. border := *NewBorder("thin", "thin", "thin", "thin")
  75. style := NewStyle()
  76. style.Border = border
  77. cell := Cell{Value: "123", style: style}
  78. style = cell.GetStyle()
  79. _, _, xBorder, _, _ := style.makeXLSXStyleElements()
  80. c.Assert(xBorder.Left.Style, Equals, "thin")
  81. c.Assert(xBorder.Right.Style, Equals, "thin")
  82. c.Assert(xBorder.Top.Style, Equals, "thin")
  83. c.Assert(xBorder.Bottom.Style, Equals, "thin")
  84. }
  85. // We can return a string representation of the formatted data
  86. func (l *CellSuite) TestFormattedValue(c *C) {
  87. cell := Cell{Value: "37947.7500001"}
  88. negativeCell := Cell{Value: "-37947.7500001"}
  89. smallCell := Cell{Value: "0.007"}
  90. earlyCell := Cell{Value: "2.1"}
  91. cell.numFmt = "general"
  92. c.Assert(cell.FormattedValue(), Equals, "37947.7500001")
  93. negativeCell.numFmt = "general"
  94. c.Assert(negativeCell.FormattedValue(), Equals, "-37947.7500001")
  95. // TODO: This test is currently broken. For a string type cell, I
  96. // don't think FormattedValue() should be doing a numeric conversion on the value
  97. // before returning the string.
  98. cell.numFmt = "0"
  99. c.Assert(cell.FormattedValue(), Equals, "37947")
  100. cell.numFmt = "#,##0" // For the time being we're not doing
  101. // this comma formatting, so it'll fall back to the related
  102. // non-comma form.
  103. c.Assert(cell.FormattedValue(), Equals, "37947")
  104. cell.numFmt = "0.00"
  105. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  106. cell.numFmt = "#,##0.00" // For the time being we're not doing
  107. // this comma formatting, so it'll fall back to the related
  108. // non-comma form.
  109. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  110. cell.numFmt = "#,##0 ;(#,##0)"
  111. c.Assert(cell.FormattedValue(), Equals, "37947")
  112. negativeCell.numFmt = "#,##0 ;(#,##0)"
  113. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  114. cell.numFmt = "#,##0 ;[red](#,##0)"
  115. c.Assert(cell.FormattedValue(), Equals, "37947")
  116. negativeCell.numFmt = "#,##0 ;[red](#,##0)"
  117. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  118. cell.numFmt = "0%"
  119. c.Assert(cell.FormattedValue(), Equals, "3794775%")
  120. cell.numFmt = "0.00%"
  121. c.Assert(cell.FormattedValue(), Equals, "3794775.00%")
  122. cell.numFmt = "0.00e+00"
  123. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  124. cell.numFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  125. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  126. cell.numFmt = "mm-dd-yy"
  127. c.Assert(cell.FormattedValue(), Equals, "11-22-03")
  128. cell.numFmt = "d-mmm-yy"
  129. c.Assert(cell.FormattedValue(), Equals, "22-Nov-03")
  130. earlyCell.numFmt = "d-mmm-yy"
  131. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-00")
  132. cell.numFmt = "d-mmm"
  133. c.Assert(cell.FormattedValue(), Equals, "22-Nov")
  134. earlyCell.numFmt = "d-mmm"
  135. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan")
  136. cell.numFmt = "mmm-yy"
  137. c.Assert(cell.FormattedValue(), Equals, "Nov-03")
  138. cell.numFmt = "h:mm am/pm"
  139. c.Assert(cell.FormattedValue(), Equals, "6:00 pm")
  140. smallCell.numFmt = "h:mm am/pm"
  141. c.Assert(smallCell.FormattedValue(), Equals, "12:14 am")
  142. cell.numFmt = "h:mm:ss am/pm"
  143. c.Assert(cell.FormattedValue(), Equals, "6:00:00 pm")
  144. smallCell.numFmt = "h:mm:ss am/pm"
  145. c.Assert(smallCell.FormattedValue(), Equals, "12:14:47 am")
  146. cell.numFmt = "h:mm"
  147. c.Assert(cell.FormattedValue(), Equals, "18:00")
  148. smallCell.numFmt = "h:mm"
  149. c.Assert(smallCell.FormattedValue(), Equals, "00:14")
  150. cell.numFmt = "h:mm:ss"
  151. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  152. // This is wrong, but there's no eary way aroud it in Go right now, AFAICT.
  153. smallCell.numFmt = "h:mm:ss"
  154. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  155. cell.numFmt = "m/d/yy h:mm"
  156. c.Assert(cell.FormattedValue(), Equals, "11/22/03 18:00")
  157. smallCell.numFmt = "m/d/yy h:mm"
  158. c.Assert(smallCell.FormattedValue(), Equals, "12/30/99 00:14") // Note, that's 1899
  159. earlyCell.numFmt = "m/d/yy h:mm"
  160. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00 02:24") // and 1900
  161. cell.numFmt = "mm:ss"
  162. c.Assert(cell.FormattedValue(), Equals, "00:00")
  163. smallCell.numFmt = "mm:ss"
  164. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  165. cell.numFmt = "[h]:mm:ss"
  166. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  167. smallCell.numFmt = "[h]:mm:ss"
  168. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  169. cell.numFmt = "mmss.0" // I'm not sure about these.
  170. c.Assert(cell.FormattedValue(), Equals, "00.8640")
  171. smallCell.numFmt = "mmss.0"
  172. c.Assert(smallCell.FormattedValue(), Equals, "1447.999997")
  173. cell.numFmt = "yyyy\\-mm\\-dd"
  174. c.Assert(cell.FormattedValue(), Equals, "2003\\-11\\-22")
  175. cell.numFmt = "dd/mm/yy"
  176. c.Assert(cell.FormattedValue(), Equals, "22/11/03")
  177. earlyCell.numFmt = "dd/mm/yy"
  178. c.Assert(earlyCell.FormattedValue(), Equals, "01/01/00")
  179. cell.numFmt = "hh:mm:ss"
  180. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  181. smallCell.numFmt = "hh:mm:ss"
  182. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  183. cell.numFmt = "dd/mm/yy\\ hh:mm"
  184. c.Assert(cell.FormattedValue(), Equals, "22/11/03\\ 18:00")
  185. cell.numFmt = "yyyy/mm/dd"
  186. c.Assert(cell.FormattedValue(), Equals, "2003/11/22")
  187. cell.numFmt = "yy-mm-dd"
  188. c.Assert(cell.FormattedValue(), Equals, "03-11-22")
  189. cell.numFmt = "d-mmm-yyyy"
  190. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  191. earlyCell.numFmt = "d-mmm-yyyy"
  192. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-1900")
  193. cell.numFmt = "m/d/yy"
  194. c.Assert(cell.FormattedValue(), Equals, "11/22/03")
  195. earlyCell.numFmt = "m/d/yy"
  196. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00")
  197. cell.numFmt = "m/d/yyyy"
  198. c.Assert(cell.FormattedValue(), Equals, "11/22/2003")
  199. earlyCell.numFmt = "m/d/yyyy"
  200. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/1900")
  201. cell.numFmt = "dd-mmm-yyyy"
  202. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  203. cell.numFmt = "dd/mm/yyyy"
  204. c.Assert(cell.FormattedValue(), Equals, "22/11/2003")
  205. cell.numFmt = "mm/dd/yy hh:mm am/pm"
  206. c.Assert(cell.FormattedValue(), Equals, "11/22/03 06:00 pm")
  207. cell.numFmt = "mm/dd/yyyy hh:mm:ss"
  208. c.Assert(cell.FormattedValue(), Equals, "11/22/2003 18:00:00")
  209. smallCell.numFmt = "mm/dd/yyyy hh:mm:ss"
  210. c.Assert(smallCell.FormattedValue(), Equals, "12/30/1899 00:14:47")
  211. cell.numFmt = "yyyy-mm-dd hh:mm:ss"
  212. c.Assert(cell.FormattedValue(), Equals, "2003-11-22 18:00:00")
  213. smallCell.numFmt = "yyyy-mm-dd hh:mm:ss"
  214. c.Assert(smallCell.FormattedValue(), Equals, "1899-12-30 00:14:47")
  215. }
  216. // test setters and getters
  217. func (s *CellSuite) TestSetterGetters(c *C) {
  218. cell := Cell{}
  219. cell.SetString("hello world")
  220. c.Assert(cell.String(), Equals, "hello world")
  221. c.Assert(cell.Type(), Equals, CellTypeString)
  222. cell.SetInt(1024)
  223. intValue, _ := cell.Int()
  224. c.Assert(intValue, Equals, 1024)
  225. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  226. cell.SetFloat(1.024)
  227. float, _ := cell.Float()
  228. intValue, _ = cell.Int() // convert
  229. c.Assert(float, Equals, 1.024)
  230. c.Assert(intValue, Equals, 1)
  231. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  232. cell.SetFormula("10+20")
  233. c.Assert(cell.Formula(), Equals, "10+20")
  234. c.Assert(cell.Type(), Equals, CellTypeFormula)
  235. }
  236. // TestOddInput is a regression test for #101. When the number format
  237. // was "@" (string), the input below caused a crash in strconv.ParseFloat.
  238. // The solution was to check if cell.Value was both a CellTypeString and
  239. // had a numFmt of "general" or "@" and short-circuit FormattedValue() if so.
  240. func (s *CellSuite) TestOddInput(c *C) {
  241. cell := Cell{}
  242. odd := `[1],[12,"DATE NOT NULL DEFAULT '0000-00-00'"]`
  243. cell.Value = odd
  244. cell.numFmt = "@"
  245. c.Assert(cell.String(), Equals, odd)
  246. }
  247. // TestBool tests basic Bool getting and setting booleans.
  248. func (s *CellSuite) TestBool(c *C) {
  249. cell := Cell{}
  250. cell.SetBool(true)
  251. c.Assert(cell.Value, Equals, "1")
  252. c.Assert(cell.Bool(), Equals, true)
  253. cell.SetBool(false)
  254. c.Assert(cell.Value, Equals, "0")
  255. c.Assert(cell.Bool(), Equals, false)
  256. }
  257. // TestStringBool tests calling Bool on a non CellTypeBool value.
  258. func (s *CellSuite) TestStringBool(c *C) {
  259. cell := Cell{}
  260. cell.SetInt(0)
  261. c.Assert(cell.Bool(), Equals, false)
  262. cell.SetInt(1)
  263. c.Assert(cell.Bool(), Equals, true)
  264. cell.SetString("")
  265. c.Assert(cell.Bool(), Equals, false)
  266. cell.SetString("0")
  267. c.Assert(cell.Bool(), Equals, true)
  268. }