cell_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package xlsx
  2. import (
  3. "math"
  4. "time"
  5. . "gopkg.in/check.v1"
  6. )
  7. type CellSuite struct{}
  8. var _ = Suite(&CellSuite{})
  9. // Test that we can set and get a Value from a Cell
  10. func (s *CellSuite) TestValueSet(c *C) {
  11. // Note, this test is fairly pointless, it serves mostly to
  12. // reinforce that this functionality is important, and should
  13. // the mechanics of this all change at some point, to remind
  14. // us not to lose this.
  15. cell := Cell{}
  16. cell.Value = "A string"
  17. c.Assert(cell.Value, Equals, "A string")
  18. }
  19. // Test that GetStyle correctly converts the xlsxStyle.Fonts.
  20. func (s *CellSuite) TestGetStyleWithFonts(c *C) {
  21. font := NewFont(10, "Calibra")
  22. style := NewStyle()
  23. style.Font = *font
  24. cell := &Cell{Value: "123", style: style}
  25. style = cell.GetStyle()
  26. c.Assert(style, NotNil)
  27. c.Assert(style.Font.Size, Equals, 10)
  28. c.Assert(style.Font.Name, Equals, "Calibra")
  29. }
  30. // Test that SetStyle correctly translates into a xlsxFont element
  31. func (s *CellSuite) TestSetStyleWithFonts(c *C) {
  32. file := NewFile()
  33. sheet, _ := file.AddSheet("Test")
  34. row := sheet.AddRow()
  35. cell := row.AddCell()
  36. font := NewFont(12, "Calibra")
  37. style := NewStyle()
  38. style.Font = *font
  39. cell.SetStyle(style)
  40. style = cell.GetStyle()
  41. xFont, _, _, _ := style.makeXLSXStyleElements()
  42. c.Assert(xFont.Sz.Val, Equals, "12")
  43. c.Assert(xFont.Name.Val, Equals, "Calibra")
  44. }
  45. // Test that GetStyle correctly converts the xlsxStyle.Fills.
  46. func (s *CellSuite) TestGetStyleWithFills(c *C) {
  47. fill := *NewFill("solid", "FF000000", "00FF0000")
  48. style := NewStyle()
  49. style.Fill = fill
  50. cell := &Cell{Value: "123", style: style}
  51. style = cell.GetStyle()
  52. _, xFill, _, _ := style.makeXLSXStyleElements()
  53. c.Assert(xFill.PatternFill.PatternType, Equals, "solid")
  54. c.Assert(xFill.PatternFill.BgColor.RGB, Equals, "00FF0000")
  55. c.Assert(xFill.PatternFill.FgColor.RGB, Equals, "FF000000")
  56. }
  57. // Test that SetStyle correctly updates xlsxStyle.Fills.
  58. func (s *CellSuite) TestSetStyleWithFills(c *C) {
  59. file := NewFile()
  60. sheet, _ := file.AddSheet("Test")
  61. row := sheet.AddRow()
  62. cell := row.AddCell()
  63. fill := NewFill("solid", "00FF0000", "FF000000")
  64. style := NewStyle()
  65. style.Fill = *fill
  66. cell.SetStyle(style)
  67. style = cell.GetStyle()
  68. _, xFill, _, _ := style.makeXLSXStyleElements()
  69. xPatternFill := xFill.PatternFill
  70. c.Assert(xPatternFill.PatternType, Equals, "solid")
  71. c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
  72. c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
  73. }
  74. // Test that GetStyle correctly converts the xlsxStyle.Borders.
  75. func (s *CellSuite) TestGetStyleWithBorders(c *C) {
  76. border := *NewBorder("thin", "thin", "thin", "thin")
  77. style := NewStyle()
  78. style.Border = border
  79. cell := Cell{Value: "123", style: style}
  80. style = cell.GetStyle()
  81. _, _, xBorder, _ := style.makeXLSXStyleElements()
  82. c.Assert(xBorder.Left.Style, Equals, "thin")
  83. c.Assert(xBorder.Right.Style, Equals, "thin")
  84. c.Assert(xBorder.Top.Style, Equals, "thin")
  85. c.Assert(xBorder.Bottom.Style, Equals, "thin")
  86. }
  87. // We can return a string representation of the formatted data
  88. func (l *CellSuite) TestSetFloatWithFormat(c *C) {
  89. cell := Cell{}
  90. cell.SetFloatWithFormat(37947.75334343, "yyyy/mm/dd")
  91. c.Assert(cell.Value, Equals, "37947.75334343")
  92. c.Assert(cell.NumFmt, Equals, "yyyy/mm/dd")
  93. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  94. }
  95. func (l *CellSuite) TestSetFloat(c *C) {
  96. cell := Cell{}
  97. cell.SetFloat(0)
  98. c.Assert(cell.Value, Equals, "0")
  99. cell.SetFloat(0.000005)
  100. c.Assert(cell.Value, Equals, "5e-06")
  101. cell.SetFloat(100.0)
  102. c.Assert(cell.Value, Equals, "100")
  103. cell.SetFloat(37947.75334343)
  104. c.Assert(cell.Value, Equals, "37947.75334343")
  105. }
  106. func (s *CellSuite) TestGetTime(c *C) {
  107. cell := Cell{}
  108. cell.SetFloat(0)
  109. date, err := cell.GetTime(false)
  110. c.Assert(err, Equals, nil)
  111. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  112. cell.SetFloat(39813.0)
  113. date, err = cell.GetTime(true)
  114. c.Assert(err, Equals, nil)
  115. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  116. cell.Value = "d"
  117. _, err = cell.GetTime(false)
  118. c.Assert(err, NotNil)
  119. }
  120. // FormattedValue returns an error for formatting errors
  121. func (l *CellSuite) TestFormattedValueErrorsOnBadFormat(c *C) {
  122. cell := Cell{Value: "Fudge Cake"}
  123. cell.NumFmt = "#,##0 ;(#,##0)"
  124. value, err := cell.FormattedValue()
  125. c.Assert(value, Equals, "Fudge Cake")
  126. c.Assert(err, NotNil)
  127. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  128. }
  129. // FormattedValue returns a string containing error text for formatting errors
  130. func (l *CellSuite) TestFormattedValueReturnsErrorAsValueForBadFormat(c *C) {
  131. cell := Cell{Value: "Fudge Cake"}
  132. cell.NumFmt = "#,##0 ;(#,##0)"
  133. _, err := cell.FormattedValue()
  134. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  135. }
  136. // formattedValueChecker removes all the boilerplate for testing Cell.FormattedValue
  137. // after its change from returning one value (a string) to two values (string, error)
  138. // This allows all the old one-line asserts in the test to continue to be one
  139. // line, instead of multi-line with error checking.
  140. type formattedValueChecker struct {
  141. c *C
  142. }
  143. func (fvc *formattedValueChecker) Equals(cell Cell, expected string) {
  144. val, err := cell.FormattedValue()
  145. if err != nil {
  146. fvc.c.Error(err)
  147. }
  148. fvc.c.Assert(val, Equals, expected)
  149. }
  150. // We can return a string representation of the formatted data
  151. func (l *CellSuite) TestFormattedValue(c *C) {
  152. // XXX TODO, this test should probably be split down, and made
  153. // in terms of SafeFormattedValue, as FormattedValue wraps
  154. // that function now.
  155. cell := Cell{Value: "37947.7500001"}
  156. negativeCell := Cell{Value: "-37947.7500001"}
  157. smallCell := Cell{Value: "0.007"}
  158. earlyCell := Cell{Value: "2.1"}
  159. fvc := formattedValueChecker{c: c}
  160. cell.NumFmt = "general"
  161. fvc.Equals(cell, "37947.7500001")
  162. negativeCell.NumFmt = "general"
  163. fvc.Equals(negativeCell, "-37947.7500001")
  164. // TODO: This test is currently broken. For a string type cell, I
  165. // don't think FormattedValue() should be doing a numeric conversion on the value
  166. // before returning the string.
  167. cell.NumFmt = "0"
  168. fvc.Equals(cell, "37947")
  169. cell.NumFmt = "#,##0" // For the time being we're not doing
  170. // this comma formatting, so it'll fall back to the related
  171. // non-comma form.
  172. fvc.Equals(cell, "37947")
  173. cell.NumFmt = "#,##0.00;(#,##0.00)"
  174. fvc.Equals(cell, "37947.75")
  175. cell.NumFmt = "0.00"
  176. fvc.Equals(cell, "37947.75")
  177. cell.NumFmt = "#,##0.00" // For the time being we're not doing
  178. // this comma formatting, so it'll fall back to the related
  179. // non-comma form.
  180. fvc.Equals(cell, "37947.75")
  181. cell.NumFmt = "#,##0 ;(#,##0)"
  182. fvc.Equals(cell, "37947")
  183. negativeCell.NumFmt = "#,##0 ;(#,##0)"
  184. fvc.Equals(negativeCell, "(37947)")
  185. cell.NumFmt = "#,##0 ;[red](#,##0)"
  186. fvc.Equals(cell, "37947")
  187. negativeCell.NumFmt = "#,##0 ;[red](#,##0)"
  188. fvc.Equals(negativeCell, "(37947)")
  189. negativeCell.NumFmt = "#,##0.00;(#,##0.00)"
  190. fvc.Equals(negativeCell, "(-37947.75)")
  191. cell.NumFmt = "0%"
  192. fvc.Equals(cell, "3794775%")
  193. cell.NumFmt = "0.00%"
  194. fvc.Equals(cell, "3794775.00%")
  195. cell.NumFmt = "0.00e+00"
  196. fvc.Equals(cell, "3.794775e+04")
  197. cell.NumFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  198. fvc.Equals(cell, "3.794775e+04")
  199. cell.NumFmt = "mm-dd-yy"
  200. fvc.Equals(cell, "11-22-03")
  201. cell.NumFmt = "d-mmm-yy"
  202. fvc.Equals(cell, "22-Nov-03")
  203. earlyCell.NumFmt = "d-mmm-yy"
  204. fvc.Equals(earlyCell, "1-Jan-00")
  205. cell.NumFmt = "d-mmm"
  206. fvc.Equals(cell, "22-Nov")
  207. earlyCell.NumFmt = "d-mmm"
  208. fvc.Equals(earlyCell, "1-Jan")
  209. cell.NumFmt = "mmm-yy"
  210. fvc.Equals(cell, "Nov-03")
  211. cell.NumFmt = "h:mm am/pm"
  212. fvc.Equals(cell, "6:00 pm")
  213. smallCell.NumFmt = "h:mm am/pm"
  214. fvc.Equals(smallCell, "12:10 am")
  215. cell.NumFmt = "h:mm:ss am/pm"
  216. fvc.Equals(cell, "6:00:00 pm")
  217. cell.NumFmt = "hh:mm:ss"
  218. fvc.Equals(cell, "18:00:00")
  219. smallCell.NumFmt = "h:mm:ss am/pm"
  220. fvc.Equals(smallCell, "12:10:04 am")
  221. cell.NumFmt = "h:mm"
  222. fvc.Equals(cell, "6:00")
  223. smallCell.NumFmt = "h:mm"
  224. fvc.Equals(smallCell, "12:10")
  225. smallCell.NumFmt = "hh:mm"
  226. fvc.Equals(smallCell, "00:10")
  227. cell.NumFmt = "h:mm:ss"
  228. fvc.Equals(cell, "6:00:00")
  229. cell.NumFmt = "hh:mm:ss"
  230. fvc.Equals(cell, "18:00:00")
  231. smallCell.NumFmt = "hh:mm:ss"
  232. fvc.Equals(smallCell, "00:10:04")
  233. smallCell.NumFmt = "h:mm:ss"
  234. fvc.Equals(smallCell, "12:10:04")
  235. cell.NumFmt = "m/d/yy h:mm"
  236. fvc.Equals(cell, "11/22/03 6:00")
  237. cell.NumFmt = "m/d/yy hh:mm"
  238. fvc.Equals(cell, "11/22/03 18:00")
  239. smallCell.NumFmt = "m/d/yy h:mm"
  240. fvc.Equals(smallCell, "12/30/99 12:10")
  241. smallCell.NumFmt = "m/d/yy hh:mm"
  242. fvc.Equals(smallCell, "12/30/99 00:10")
  243. earlyCell.NumFmt = "m/d/yy hh:mm"
  244. fvc.Equals(earlyCell, "1/1/00 02:24")
  245. earlyCell.NumFmt = "m/d/yy h:mm"
  246. fvc.Equals(earlyCell, "1/1/00 2:24")
  247. cell.NumFmt = "mm:ss"
  248. fvc.Equals(cell, "00:00")
  249. smallCell.NumFmt = "mm:ss"
  250. fvc.Equals(smallCell, "10:04")
  251. cell.NumFmt = "[hh]:mm:ss"
  252. fvc.Equals(cell, "18:00:00")
  253. cell.NumFmt = "[h]:mm:ss"
  254. fvc.Equals(cell, "6:00:00")
  255. smallCell.NumFmt = "[h]:mm:ss"
  256. fvc.Equals(smallCell, "10:04")
  257. const (
  258. expect1 = "0000.0086"
  259. expect2 = "1004.8000"
  260. format = "mmss.0000"
  261. tlen = len(format)
  262. )
  263. for i := 0; i < 3; i++ {
  264. tfmt := format[0 : tlen-i]
  265. cell.NumFmt = tfmt
  266. fvc.Equals(cell, expect1[0:tlen-i])
  267. smallCell.NumFmt = tfmt
  268. fvc.Equals(smallCell, expect2[0:tlen-i])
  269. }
  270. cell.NumFmt = "yyyy\\-mm\\-dd"
  271. fvc.Equals(cell, "2003\\-11\\-22")
  272. cell.NumFmt = "dd/mm/yyyy hh:mm:ss"
  273. fvc.Equals(cell, "22/11/2003 18:00:00")
  274. cell.NumFmt = "dd/mm/yy"
  275. fvc.Equals(cell, "22/11/03")
  276. earlyCell.NumFmt = "dd/mm/yy"
  277. fvc.Equals(earlyCell, "01/01/00")
  278. cell.NumFmt = "hh:mm:ss"
  279. fvc.Equals(cell, "18:00:00")
  280. smallCell.NumFmt = "hh:mm:ss"
  281. fvc.Equals(smallCell, "00:10:04")
  282. cell.NumFmt = "dd/mm/yy\\ hh:mm"
  283. fvc.Equals(cell, "22/11/03\\ 18:00")
  284. cell.NumFmt = "yyyy/mm/dd"
  285. fvc.Equals(cell, "2003/11/22")
  286. cell.NumFmt = "yy-mm-dd"
  287. fvc.Equals(cell, "03-11-22")
  288. cell.NumFmt = "d-mmm-yyyy"
  289. fvc.Equals(cell, "22-Nov-2003")
  290. earlyCell.NumFmt = "d-mmm-yyyy"
  291. fvc.Equals(earlyCell, "1-Jan-1900")
  292. cell.NumFmt = "m/d/yy"
  293. fvc.Equals(cell, "11/22/03")
  294. earlyCell.NumFmt = "m/d/yy"
  295. fvc.Equals(earlyCell, "1/1/00")
  296. cell.NumFmt = "m/d/yyyy"
  297. fvc.Equals(cell, "11/22/2003")
  298. earlyCell.NumFmt = "m/d/yyyy"
  299. fvc.Equals(earlyCell, "1/1/1900")
  300. cell.NumFmt = "dd-mmm-yyyy"
  301. fvc.Equals(cell, "22-Nov-2003")
  302. cell.NumFmt = "dd/mm/yyyy"
  303. fvc.Equals(cell, "22/11/2003")
  304. cell.NumFmt = "mm/dd/yy hh:mm am/pm"
  305. fvc.Equals(cell, "11/22/03 18:00 pm")
  306. cell.NumFmt = "mm/dd/yy h:mm am/pm"
  307. fvc.Equals(cell, "11/22/03 6:00 pm")
  308. cell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  309. fvc.Equals(cell, "11/22/2003 18:00:00")
  310. smallCell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  311. fvc.Equals(smallCell, "12/30/1899 00:10:04")
  312. cell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  313. fvc.Equals(cell, "2003-11-22 18:00:00")
  314. smallCell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  315. fvc.Equals(smallCell, "1899-12-30 00:10:04")
  316. cell.NumFmt = "mmmm d, yyyy"
  317. fvc.Equals(cell, "November 22, 2003")
  318. smallCell.NumFmt = "mmmm d, yyyy"
  319. fvc.Equals(smallCell, "December 30, 1899")
  320. cell.NumFmt = "dddd, mmmm dd, yyyy"
  321. fvc.Equals(cell, "Saturday, November 22, 2003")
  322. smallCell.NumFmt = "dddd, mmmm dd, yyyy"
  323. fvc.Equals(smallCell, "Saturday, December 30, 1899")
  324. }
  325. // test setters and getters
  326. func (s *CellSuite) TestSetterGetters(c *C) {
  327. cell := Cell{}
  328. cell.SetString("hello world")
  329. if val, err := cell.String(); err != nil {
  330. c.Error(err)
  331. } else {
  332. c.Assert(val, Equals, "hello world")
  333. }
  334. c.Assert(cell.Type(), Equals, CellTypeString)
  335. cell.SetInt(1024)
  336. intValue, _ := cell.Int()
  337. c.Assert(intValue, Equals, 1024)
  338. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  339. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  340. cell.SetInt64(1024)
  341. int64Value, _ := cell.Int64()
  342. c.Assert(int64Value, Equals, int64(1024))
  343. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  344. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  345. cell.SetFloat(1.024)
  346. float, _ := cell.Float()
  347. intValue, _ = cell.Int() // convert
  348. c.Assert(float, Equals, 1.024)
  349. c.Assert(intValue, Equals, 1)
  350. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  351. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  352. cell.SetFormula("10+20")
  353. c.Assert(cell.Formula(), Equals, "10+20")
  354. c.Assert(cell.Type(), Equals, CellTypeFormula)
  355. }
  356. // TestOddInput is a regression test for #101. When the number format
  357. // was "@" (string), the input below caused a crash in strconv.ParseFloat.
  358. // The solution was to check if cell.Value was both a CellTypeString and
  359. // had a NumFmt of "general" or "@" and short-circuit FormattedValue() if so.
  360. func (s *CellSuite) TestOddInput(c *C) {
  361. cell := Cell{}
  362. odd := `[1],[12,"DATE NOT NULL DEFAULT '0000-00-00'"]`
  363. cell.Value = odd
  364. cell.NumFmt = "@"
  365. if val, err := cell.String(); err != nil {
  366. c.Error(err)
  367. } else {
  368. c.Assert(val, Equals, odd)
  369. }
  370. }
  371. // TestBool tests basic Bool getting and setting booleans.
  372. func (s *CellSuite) TestBool(c *C) {
  373. cell := Cell{}
  374. cell.SetBool(true)
  375. c.Assert(cell.Value, Equals, "1")
  376. c.Assert(cell.Bool(), Equals, true)
  377. cell.SetBool(false)
  378. c.Assert(cell.Value, Equals, "0")
  379. c.Assert(cell.Bool(), Equals, false)
  380. }
  381. // TestStringBool tests calling Bool on a non CellTypeBool value.
  382. func (s *CellSuite) TestStringBool(c *C) {
  383. cell := Cell{}
  384. cell.SetInt(0)
  385. c.Assert(cell.Bool(), Equals, false)
  386. cell.SetInt(1)
  387. c.Assert(cell.Bool(), Equals, true)
  388. cell.SetString("")
  389. c.Assert(cell.Bool(), Equals, false)
  390. cell.SetString("0")
  391. c.Assert(cell.Bool(), Equals, true)
  392. }
  393. // TestSetValue tests whether SetValue handle properly for different type values.
  394. func (s *CellSuite) TestSetValue(c *C) {
  395. cell := Cell{}
  396. // int
  397. for _, i := range []interface{}{1, int8(1), int16(1), int32(1), int64(1)} {
  398. cell.SetValue(i)
  399. val, err := cell.Int64()
  400. c.Assert(err, IsNil)
  401. c.Assert(val, Equals, int64(1))
  402. }
  403. // float
  404. for _, i := range []interface{}{1.11, float32(1.11), float64(1.11)} {
  405. cell.SetValue(i)
  406. val, err := cell.Float()
  407. c.Assert(err, IsNil)
  408. c.Assert(val, Equals, 1.11)
  409. }
  410. // time
  411. cell.SetValue(time.Unix(0, 0))
  412. val, err := cell.Float()
  413. c.Assert(err, IsNil)
  414. c.Assert(math.Floor(val), Equals, 25569.0)
  415. // string and nil
  416. for _, i := range []interface{}{nil, "", []byte("")} {
  417. cell.SetValue(i)
  418. c.Assert(cell.Value, Equals, "")
  419. }
  420. // others
  421. cell.SetValue([]string{"test"})
  422. c.Assert(cell.Value, Equals, "[test]")
  423. }