cell_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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) TestSetFloatWithFormat(c *C) {
  87. cell := Cell{}
  88. cell.SetFloatWithFormat(37947.75334343, "yyyy/mm/dd")
  89. c.Assert(cell.Value, Equals, "37947.75334343")
  90. c.Assert(cell.numFmt, Equals, "yyyy/mm/dd")
  91. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  92. }
  93. func (l *CellSuite) TestSetFloat(c *C) {
  94. cell := Cell{}
  95. cell.SetFloat(0)
  96. c.Assert(cell.Value, Equals, "0")
  97. cell.SetFloat(0.000005)
  98. c.Assert(cell.Value, Equals, "5e-06")
  99. cell.SetFloat(100.0)
  100. c.Assert(cell.Value, Equals, "100")
  101. cell.SetFloat(37947.75334343)
  102. c.Assert(cell.Value, Equals, "37947.75334343")
  103. }
  104. // We can return a string representation of the formatted data
  105. func (l *CellSuite) TestFormattedValue(c *C) {
  106. cell := Cell{Value: "37947.7500001"}
  107. negativeCell := Cell{Value: "-37947.7500001"}
  108. smallCell := Cell{Value: "0.007"}
  109. earlyCell := Cell{Value: "2.1"}
  110. cell.numFmt = "general"
  111. c.Assert(cell.FormattedValue(), Equals, "37947.7500001")
  112. negativeCell.numFmt = "general"
  113. c.Assert(negativeCell.FormattedValue(), Equals, "-37947.7500001")
  114. // TODO: This test is currently broken. For a string type cell, I
  115. // don't think FormattedValue() should be doing a numeric conversion on the value
  116. // before returning the string.
  117. cell.numFmt = "0"
  118. c.Assert(cell.FormattedValue(), Equals, "37947")
  119. cell.numFmt = "#,##0" // For the time being we're not doing
  120. // this comma formatting, so it'll fall back to the related
  121. // non-comma form.
  122. c.Assert(cell.FormattedValue(), Equals, "37947")
  123. cell.numFmt = "#,##0.00;(#,##0.00)"
  124. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  125. cell.numFmt = "0.00"
  126. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  127. cell.numFmt = "#,##0.00" // For the time being we're not doing
  128. // this comma formatting, so it'll fall back to the related
  129. // non-comma form.
  130. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  131. cell.numFmt = "#,##0 ;(#,##0)"
  132. c.Assert(cell.FormattedValue(), Equals, "37947")
  133. negativeCell.numFmt = "#,##0 ;(#,##0)"
  134. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  135. cell.numFmt = "#,##0 ;[red](#,##0)"
  136. c.Assert(cell.FormattedValue(), Equals, "37947")
  137. negativeCell.numFmt = "#,##0 ;[red](#,##0)"
  138. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  139. negativeCell.numFmt = "#,##0.00;(#,##0.00)"
  140. c.Assert(negativeCell.FormattedValue(), Equals, "(-37947.75)")
  141. cell.numFmt = "0%"
  142. c.Assert(cell.FormattedValue(), Equals, "3794775%")
  143. cell.numFmt = "0.00%"
  144. c.Assert(cell.FormattedValue(), Equals, "3794775.00%")
  145. cell.numFmt = "0.00e+00"
  146. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  147. cell.numFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  148. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  149. cell.numFmt = "mm-dd-yy"
  150. c.Assert(cell.FormattedValue(), Equals, "11-22-03")
  151. cell.numFmt = "d-mmm-yy"
  152. c.Assert(cell.FormattedValue(), Equals, "22-Nov-03")
  153. earlyCell.numFmt = "d-mmm-yy"
  154. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-00")
  155. cell.numFmt = "d-mmm"
  156. c.Assert(cell.FormattedValue(), Equals, "22-Nov")
  157. earlyCell.numFmt = "d-mmm"
  158. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan")
  159. cell.numFmt = "mmm-yy"
  160. c.Assert(cell.FormattedValue(), Equals, "Nov-03")
  161. cell.numFmt = "h:mm am/pm"
  162. c.Assert(cell.FormattedValue(), Equals, "6:00 pm")
  163. smallCell.numFmt = "h:mm am/pm"
  164. c.Assert(smallCell.FormattedValue(), Equals, "12:14 am")
  165. cell.numFmt = "h:mm:ss am/pm"
  166. c.Assert(cell.FormattedValue(), Equals, "6:00:00 pm")
  167. cell.numFmt = "hh:mm:ss"
  168. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  169. smallCell.numFmt = "h:mm:ss am/pm"
  170. c.Assert(smallCell.FormattedValue(), Equals, "12:14:47 am")
  171. cell.numFmt = "h:mm"
  172. c.Assert(cell.FormattedValue(), Equals, "6:00")
  173. smallCell.numFmt = "h:mm"
  174. c.Assert(smallCell.FormattedValue(), Equals, "12:14")
  175. smallCell.numFmt = "hh:mm"
  176. c.Assert(smallCell.FormattedValue(), Equals, "00:14")
  177. cell.numFmt = "h:mm:ss"
  178. c.Assert(cell.FormattedValue(), Equals, "6:00: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. smallCell.numFmt = "h:mm:ss"
  184. c.Assert(smallCell.FormattedValue(), Equals, "12:14:47")
  185. cell.numFmt = "m/d/yy h:mm"
  186. c.Assert(cell.FormattedValue(), Equals, "11/22/03 6:00")
  187. cell.numFmt = "m/d/yy hh:mm"
  188. c.Assert(cell.FormattedValue(), Equals, "11/22/03 18:00")
  189. smallCell.numFmt = "m/d/yy h:mm"
  190. c.Assert(smallCell.FormattedValue(), Equals, "12/30/99 12:14") // Note, that's 1899
  191. smallCell.numFmt = "m/d/yy hh:mm"
  192. c.Assert(smallCell.FormattedValue(), Equals, "12/30/99 00:14") // Note, that's 1899
  193. earlyCell.numFmt = "m/d/yy hh:mm"
  194. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00 02:24") // and 1900
  195. earlyCell.numFmt = "m/d/yy h:mm"
  196. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00 2:24") // and 1900
  197. cell.numFmt = "mm:ss"
  198. c.Assert(cell.FormattedValue(), Equals, "00:00")
  199. smallCell.numFmt = "mm:ss"
  200. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  201. cell.numFmt = "[hh]:mm:ss"
  202. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  203. cell.numFmt = "[h]:mm:ss"
  204. c.Assert(cell.FormattedValue(), Equals, "6:00:00")
  205. smallCell.numFmt = "[h]:mm:ss"
  206. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  207. cell.numFmt = "mmss.0" // I'm not sure about these.
  208. c.Assert(cell.FormattedValue(), Equals, "0000.0086")
  209. smallCell.numFmt = "mmss.0"
  210. c.Assert(smallCell.FormattedValue(), Equals, "1447.9999")
  211. cell.numFmt = "yyyy\\-mm\\-dd"
  212. c.Assert(cell.FormattedValue(), Equals, "2003\\-11\\-22")
  213. cell.numFmt = "dd/mm/yyyy hh:mm:ss"
  214. c.Assert(cell.FormattedValue(), Equals, "22/11/2003 18:00:00")
  215. cell.numFmt = "dd/mm/yy"
  216. c.Assert(cell.FormattedValue(), Equals, "22/11/03")
  217. earlyCell.numFmt = "dd/mm/yy"
  218. c.Assert(earlyCell.FormattedValue(), Equals, "01/01/00")
  219. cell.numFmt = "hh:mm:ss"
  220. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  221. smallCell.numFmt = "hh:mm:ss"
  222. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  223. cell.numFmt = "dd/mm/yy\\ hh:mm"
  224. c.Assert(cell.FormattedValue(), Equals, "22/11/03\\ 18:00")
  225. cell.numFmt = "yyyy/mm/dd"
  226. c.Assert(cell.FormattedValue(), Equals, "2003/11/22")
  227. cell.numFmt = "yy-mm-dd"
  228. c.Assert(cell.FormattedValue(), Equals, "03-11-22")
  229. cell.numFmt = "d-mmm-yyyy"
  230. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  231. earlyCell.numFmt = "d-mmm-yyyy"
  232. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-1900")
  233. cell.numFmt = "m/d/yy"
  234. c.Assert(cell.FormattedValue(), Equals, "11/22/03")
  235. earlyCell.numFmt = "m/d/yy"
  236. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00")
  237. cell.numFmt = "m/d/yyyy"
  238. c.Assert(cell.FormattedValue(), Equals, "11/22/2003")
  239. earlyCell.numFmt = "m/d/yyyy"
  240. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/1900")
  241. cell.numFmt = "dd-mmm-yyyy"
  242. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  243. cell.numFmt = "dd/mm/yyyy"
  244. c.Assert(cell.FormattedValue(), Equals, "22/11/2003")
  245. cell.numFmt = "mm/dd/yy hh:mm am/pm"
  246. c.Assert(cell.FormattedValue(), Equals, "11/22/03 18:00 pm")
  247. cell.numFmt = "mm/dd/yy h:mm am/pm"
  248. c.Assert(cell.FormattedValue(), Equals, "11/22/03 6:00 pm")
  249. cell.numFmt = "mm/dd/yyyy hh:mm:ss"
  250. c.Assert(cell.FormattedValue(), Equals, "11/22/2003 18:00:00")
  251. smallCell.numFmt = "mm/dd/yyyy hh:mm:ss"
  252. c.Assert(smallCell.FormattedValue(), Equals, "12/30/1899 00:14:47")
  253. cell.numFmt = "yyyy-mm-dd hh:mm:ss"
  254. c.Assert(cell.FormattedValue(), Equals, "2003-11-22 18:00:00")
  255. smallCell.numFmt = "yyyy-mm-dd hh:mm:ss"
  256. c.Assert(smallCell.FormattedValue(), Equals, "1899-12-30 00:14:47")
  257. }
  258. // test setters and getters
  259. func (s *CellSuite) TestSetterGetters(c *C) {
  260. cell := Cell{}
  261. cell.SetString("hello world")
  262. c.Assert(cell.String(), Equals, "hello world")
  263. c.Assert(cell.Type(), Equals, CellTypeString)
  264. cell.SetInt(1024)
  265. intValue, _ := cell.Int()
  266. c.Assert(intValue, Equals, 1024)
  267. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  268. cell.SetInt64(1024)
  269. int64Value, _ := cell.Int64()
  270. c.Assert(int64Value, Equals, int64(1024))
  271. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  272. cell.SetFloat(1.024)
  273. float, _ := cell.Float()
  274. intValue, _ = cell.Int() // convert
  275. c.Assert(float, Equals, 1.024)
  276. c.Assert(intValue, Equals, 1)
  277. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  278. cell.SetFormula("10+20")
  279. c.Assert(cell.Formula(), Equals, "10+20")
  280. c.Assert(cell.Type(), Equals, CellTypeFormula)
  281. }
  282. // TestOddInput is a regression test for #101. When the number format
  283. // was "@" (string), the input below caused a crash in strconv.ParseFloat.
  284. // The solution was to check if cell.Value was both a CellTypeString and
  285. // had a numFmt of "general" or "@" and short-circuit FormattedValue() if so.
  286. func (s *CellSuite) TestOddInput(c *C) {
  287. cell := Cell{}
  288. odd := `[1],[12,"DATE NOT NULL DEFAULT '0000-00-00'"]`
  289. cell.Value = odd
  290. cell.numFmt = "@"
  291. c.Assert(cell.String(), Equals, odd)
  292. }
  293. // TestBool tests basic Bool getting and setting booleans.
  294. func (s *CellSuite) TestBool(c *C) {
  295. cell := Cell{}
  296. cell.SetBool(true)
  297. c.Assert(cell.Value, Equals, "1")
  298. c.Assert(cell.Bool(), Equals, true)
  299. cell.SetBool(false)
  300. c.Assert(cell.Value, Equals, "0")
  301. c.Assert(cell.Bool(), Equals, false)
  302. }
  303. // TestStringBool tests calling Bool on a non CellTypeBool value.
  304. func (s *CellSuite) TestStringBool(c *C) {
  305. cell := Cell{}
  306. cell.SetInt(0)
  307. c.Assert(cell.Bool(), Equals, false)
  308. cell.SetInt(1)
  309. c.Assert(cell.Bool(), Equals, true)
  310. cell.SetString("")
  311. c.Assert(cell.Bool(), Equals, false)
  312. cell.SetString("0")
  313. c.Assert(cell.Bool(), Equals, true)
  314. }