cell_test.go 12 KB

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