cell_test.go 14 KB

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