cell_test.go 13 KB

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