cell_test.go 14 KB

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