cell_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. package xlsx
  2. import (
  3. "math"
  4. "testing"
  5. "time"
  6. . "gopkg.in/check.v1"
  7. )
  8. type CellSuite struct{}
  9. var _ = Suite(&CellSuite{})
  10. // Test that we can set and get a Value from a Cell
  11. func (s *CellSuite) TestValueSet(c *C) {
  12. // Note, this test is fairly pointless, it serves mostly to
  13. // reinforce that this functionality is important, and should
  14. // the mechanics of this all change at some point, to remind
  15. // us not to lose this.
  16. cell := Cell{}
  17. cell.Value = "A string"
  18. c.Assert(cell.Value, Equals, "A string")
  19. }
  20. // Test that GetStyle correctly converts the xlsxStyle.Fonts.
  21. func (s *CellSuite) TestGetStyleWithFonts(c *C) {
  22. font := NewFont(10, "Calibra")
  23. style := NewStyle()
  24. style.Font = *font
  25. cell := &Cell{Value: "123", style: style}
  26. style = cell.GetStyle()
  27. c.Assert(style, NotNil)
  28. c.Assert(style.Font.Size, Equals, 10)
  29. c.Assert(style.Font.Name, Equals, "Calibra")
  30. }
  31. // Test that SetStyle correctly translates into a xlsxFont element
  32. func (s *CellSuite) TestSetStyleWithFonts(c *C) {
  33. file := NewFile()
  34. sheet, _ := file.AddSheet("Test")
  35. row := sheet.AddRow()
  36. cell := row.AddCell()
  37. font := NewFont(12, "Calibra")
  38. style := NewStyle()
  39. style.Font = *font
  40. cell.SetStyle(style)
  41. style = cell.GetStyle()
  42. xFont, _, _, _ := style.makeXLSXStyleElements()
  43. c.Assert(xFont.Sz.Val, Equals, "12")
  44. c.Assert(xFont.Name.Val, Equals, "Calibra")
  45. }
  46. // Test that GetStyle correctly converts the xlsxStyle.Fills.
  47. func (s *CellSuite) TestGetStyleWithFills(c *C) {
  48. fill := *NewFill("solid", "FF000000", "00FF0000")
  49. style := NewStyle()
  50. style.Fill = fill
  51. cell := &Cell{Value: "123", style: style}
  52. style = cell.GetStyle()
  53. _, xFill, _, _ := style.makeXLSXStyleElements()
  54. c.Assert(xFill.PatternFill.PatternType, Equals, "solid")
  55. c.Assert(xFill.PatternFill.BgColor.RGB, Equals, "00FF0000")
  56. c.Assert(xFill.PatternFill.FgColor.RGB, Equals, "FF000000")
  57. }
  58. // Test that SetStyle correctly updates xlsxStyle.Fills.
  59. func (s *CellSuite) TestSetStyleWithFills(c *C) {
  60. file := NewFile()
  61. sheet, _ := file.AddSheet("Test")
  62. row := sheet.AddRow()
  63. cell := row.AddCell()
  64. fill := NewFill("solid", "00FF0000", "FF000000")
  65. style := NewStyle()
  66. style.Fill = *fill
  67. cell.SetStyle(style)
  68. style = cell.GetStyle()
  69. _, xFill, _, _ := style.makeXLSXStyleElements()
  70. xPatternFill := xFill.PatternFill
  71. c.Assert(xPatternFill.PatternType, Equals, "solid")
  72. c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
  73. c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
  74. }
  75. // Test that GetStyle correctly converts the xlsxStyle.Borders.
  76. func (s *CellSuite) TestGetStyleWithBorders(c *C) {
  77. border := *NewBorder("thin", "thin", "thin", "thin")
  78. style := NewStyle()
  79. style.Border = border
  80. cell := Cell{Value: "123", style: style}
  81. style = cell.GetStyle()
  82. _, _, xBorder, _ := style.makeXLSXStyleElements()
  83. c.Assert(xBorder.Left.Style, Equals, "thin")
  84. c.Assert(xBorder.Right.Style, Equals, "thin")
  85. c.Assert(xBorder.Top.Style, Equals, "thin")
  86. c.Assert(xBorder.Bottom.Style, Equals, "thin")
  87. }
  88. // We can return a string representation of the formatted data
  89. func (l *CellSuite) TestSetFloatWithFormat(c *C) {
  90. cell := Cell{}
  91. cell.SetFloatWithFormat(37947.75334343, "yyyy/mm/dd")
  92. c.Assert(cell.Value, Equals, "37947.75334343")
  93. c.Assert(cell.NumFmt, Equals, "yyyy/mm/dd")
  94. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  95. }
  96. func (l *CellSuite) TestSetFloat(c *C) {
  97. cell := Cell{}
  98. cell.SetFloat(0)
  99. c.Assert(cell.Value, Equals, "0")
  100. cell.SetFloat(0.000005)
  101. c.Assert(cell.Value, Equals, "5e-06")
  102. cell.SetFloat(100.0)
  103. c.Assert(cell.Value, Equals, "100")
  104. cell.SetFloat(37947.75334343)
  105. c.Assert(cell.Value, Equals, "37947.75334343")
  106. }
  107. func (l *CellSuite) TestGeneralNumberHandling(c *C) {
  108. // If you go to Excel, make a new file, type 18.99 in a cell, and save, what you will get is a
  109. // cell where the format is General and the storage type is Number, that contains the value 18.989999999999998.
  110. // The correct way to format this should be 18.99.
  111. // 1.1 will get you the same, with a stored value of 1.1000000000000001.
  112. // Also, numbers greater than 1e11 and less than 1e-9 wil be shown as scientific notation.
  113. testCases := []struct {
  114. value string
  115. formattedValueOutput string
  116. noExpValueOutput string
  117. }{
  118. {
  119. value: "18.989999999999998",
  120. formattedValueOutput: "18.99",
  121. noExpValueOutput: "18.99",
  122. },
  123. {
  124. value: "1.1000000000000001",
  125. formattedValueOutput: "1.1",
  126. noExpValueOutput: "1.1",
  127. },
  128. {
  129. value: "0.0000000000000001",
  130. formattedValueOutput: "1E-16",
  131. noExpValueOutput: "0.0000000000000001",
  132. },
  133. {
  134. value: "0.000000000000008",
  135. formattedValueOutput: "8E-15",
  136. noExpValueOutput: "0.000000000000008",
  137. },
  138. {
  139. value: "1000000000000000000",
  140. formattedValueOutput: "1E+18",
  141. noExpValueOutput: "1000000000000000000",
  142. },
  143. {
  144. value: "1230000000000000000",
  145. formattedValueOutput: "1.23E+18",
  146. noExpValueOutput: "1230000000000000000",
  147. },
  148. {
  149. value: "12345678",
  150. formattedValueOutput: "12345678",
  151. noExpValueOutput: "12345678",
  152. },
  153. {
  154. value: "0",
  155. formattedValueOutput: "0",
  156. noExpValueOutput: "0",
  157. },
  158. {
  159. value: "-18.989999999999998",
  160. formattedValueOutput: "-18.99",
  161. noExpValueOutput: "-18.99",
  162. },
  163. {
  164. value: "-1.1000000000000001",
  165. formattedValueOutput: "-1.1",
  166. noExpValueOutput: "-1.1",
  167. },
  168. {
  169. value: "-0.0000000000000001",
  170. formattedValueOutput: "-1E-16",
  171. noExpValueOutput: "-0.0000000000000001",
  172. },
  173. {
  174. value: "-0.000000000000008",
  175. formattedValueOutput: "-8E-15",
  176. noExpValueOutput: "-0.000000000000008",
  177. },
  178. {
  179. value: "-1000000000000000000",
  180. formattedValueOutput: "-1E+18",
  181. noExpValueOutput: "-1000000000000000000",
  182. },
  183. {
  184. value: "-1230000000000000000",
  185. formattedValueOutput: "-1.23E+18",
  186. noExpValueOutput: "-1230000000000000000",
  187. },
  188. {
  189. value: "-12345678",
  190. formattedValueOutput: "-12345678",
  191. noExpValueOutput: "-12345678",
  192. },
  193. }
  194. for _, testCase := range testCases {
  195. cell := Cell{
  196. cellType: CellTypeNumeric,
  197. NumFmt: builtInNumFmt[builtInNumFmtIndex_GENERAL],
  198. Value: testCase.value,
  199. }
  200. val, err := cell.FormattedValue()
  201. if err != nil {
  202. c.Fatal(err)
  203. }
  204. c.Assert(val, Equals, testCase.formattedValueOutput)
  205. val, err = cell.GeneralNumericWithoutScientific()
  206. if err != nil {
  207. c.Fatal(err)
  208. }
  209. c.Assert(val, Equals, testCase.noExpValueOutput)
  210. }
  211. }
  212. func (s *CellSuite) TestGetTime(c *C) {
  213. cell := Cell{}
  214. cell.SetFloat(0)
  215. date, err := cell.GetTime(false)
  216. c.Assert(err, Equals, nil)
  217. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  218. cell.SetFloat(39813.0)
  219. date, err = cell.GetTime(true)
  220. c.Assert(err, Equals, nil)
  221. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  222. cell.Value = "d"
  223. _, err = cell.GetTime(false)
  224. c.Assert(err, NotNil)
  225. }
  226. // FormattedValue returns an error for formatting errors
  227. func (l *CellSuite) TestFormattedValueErrorsOnBadFormat(c *C) {
  228. cell := Cell{Value: "Fudge Cake", cellType: CellTypeNumeric}
  229. cell.NumFmt = "#,##0 ;(#,##0)"
  230. value, err := cell.FormattedValue()
  231. c.Assert(value, Equals, "Fudge Cake")
  232. c.Assert(err, NotNil)
  233. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  234. }
  235. // formattedValueChecker removes all the boilerplate for testing Cell.FormattedValue
  236. // after its change from returning one value (a string) to two values (string, error)
  237. // This allows all the old one-line asserts in the test to continue to be one
  238. // line, instead of multi-line with error checking.
  239. type formattedValueChecker struct {
  240. c *C
  241. }
  242. func (fvc *formattedValueChecker) Equals(cell Cell, expected string) {
  243. val, err := cell.FormattedValue()
  244. if err != nil {
  245. fvc.c.Error(err)
  246. }
  247. fvc.c.Assert(val, Equals, expected)
  248. }
  249. func cellsFormattedValueEquals(t *testing.T, cell *Cell, expected string) {
  250. val, err := cell.FormattedValue()
  251. if err != nil {
  252. t.Error(err)
  253. }
  254. if val != expected {
  255. t.Errorf("Expected cell.FormattedValue() to be %v, got %v", expected, val)
  256. }
  257. }
  258. // We can return a string representation of the formatted data
  259. func (l *CellSuite) TestFormattedValue(c *C) {
  260. cell := Cell{Value: "37947.7500001", cellType: CellTypeNumeric}
  261. negativeCell := Cell{Value: "-37947.7500001", cellType: CellTypeNumeric}
  262. smallCell := Cell{Value: "0.007", cellType: CellTypeNumeric}
  263. earlyCell := Cell{Value: "2.1", cellType: CellTypeNumeric}
  264. fvc := formattedValueChecker{c: c}
  265. cell.NumFmt = "general"
  266. fvc.Equals(cell, "37947.7500001")
  267. negativeCell.NumFmt = "general"
  268. fvc.Equals(negativeCell, "-37947.7500001")
  269. // TODO: This test is currently broken. For a string type cell, I
  270. // don't think FormattedValue() should be doing a numeric conversion on the value
  271. // before returning the string.
  272. cell.NumFmt = "0"
  273. fvc.Equals(cell, "37948")
  274. cell.NumFmt = "#,##0" // For the time being we're not doing
  275. // this comma formatting, so it'll fall back to the related
  276. // non-comma form.
  277. fvc.Equals(cell, "37948")
  278. cell.NumFmt = "#,##0.00;(#,##0.00)"
  279. fvc.Equals(cell, "37947.75")
  280. cell.NumFmt = "0.00"
  281. fvc.Equals(cell, "37947.75")
  282. cell.NumFmt = "#,##0.00" // For the time being we're not doing
  283. // this comma formatting, so it'll fall back to the related
  284. // non-comma form.
  285. fvc.Equals(cell, "37947.75")
  286. cell.NumFmt = "#,##0 ;(#,##0)"
  287. fvc.Equals(cell, "37948")
  288. negativeCell.NumFmt = "#,##0 ;(#,##0)"
  289. fvc.Equals(negativeCell, "(37948)")
  290. cell.NumFmt = "#,##0 ;[red](#,##0)"
  291. fvc.Equals(cell, "37948")
  292. negativeCell.NumFmt = "#,##0 ;[red](#,##0)"
  293. fvc.Equals(negativeCell, "(37948)")
  294. negativeCell.NumFmt = "#,##0.00;(#,##0.00)"
  295. fvc.Equals(negativeCell, "(37947.75)")
  296. cell.NumFmt = "0%"
  297. fvc.Equals(cell, "3794775%")
  298. cell.NumFmt = "0.00%"
  299. fvc.Equals(cell, "3794775.00%")
  300. cell.NumFmt = "0.00e+00"
  301. fvc.Equals(cell, "3.794775e+04")
  302. cell.NumFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  303. fvc.Equals(cell, "3.794775e+04")
  304. cell.NumFmt = "mm-dd-yy"
  305. fvc.Equals(cell, "11-22-03")
  306. cell.NumFmt = "d-mmm-yy"
  307. fvc.Equals(cell, "22-Nov-03")
  308. earlyCell.NumFmt = "d-mmm-yy"
  309. fvc.Equals(earlyCell, "1-Jan-00")
  310. cell.NumFmt = "d-mmm"
  311. fvc.Equals(cell, "22-Nov")
  312. earlyCell.NumFmt = "d-mmm"
  313. fvc.Equals(earlyCell, "1-Jan")
  314. cell.NumFmt = "mmm-yy"
  315. fvc.Equals(cell, "Nov-03")
  316. cell.NumFmt = "h:mm am/pm"
  317. fvc.Equals(cell, "6:00 pm")
  318. smallCell.NumFmt = "h:mm am/pm"
  319. fvc.Equals(smallCell, "12:10 am")
  320. cell.NumFmt = "h:mm:ss am/pm"
  321. fvc.Equals(cell, "6:00:00 pm")
  322. cell.NumFmt = "hh:mm:ss"
  323. fvc.Equals(cell, "18:00:00")
  324. smallCell.NumFmt = "h:mm:ss am/pm"
  325. fvc.Equals(smallCell, "12:10:04 am")
  326. cell.NumFmt = "h:mm"
  327. fvc.Equals(cell, "18:00")
  328. smallCell.NumFmt = "h:mm"
  329. fvc.Equals(smallCell, "00:10")
  330. smallCell.NumFmt = "hh:mm"
  331. fvc.Equals(smallCell, "00:10")
  332. cell.NumFmt = "h:mm:ss"
  333. fvc.Equals(cell, "18:00:00")
  334. cell.NumFmt = "hh:mm:ss"
  335. fvc.Equals(cell, "18:00:00")
  336. smallCell.NumFmt = "hh:mm:ss"
  337. fvc.Equals(smallCell, "00:10:04")
  338. smallCell.NumFmt = "h:mm:ss"
  339. fvc.Equals(smallCell, "00:10:04")
  340. cell.NumFmt = "m/d/yy h:mm"
  341. fvc.Equals(cell, "11/22/03 18:00")
  342. cell.NumFmt = "m/d/yy hh:mm"
  343. fvc.Equals(cell, "11/22/03 18:00")
  344. smallCell.NumFmt = "m/d/yy h:mm"
  345. fvc.Equals(smallCell, "12/30/99 00:10")
  346. smallCell.NumFmt = "m/d/yy hh:mm"
  347. fvc.Equals(smallCell, "12/30/99 00:10")
  348. earlyCell.NumFmt = "m/d/yy hh:mm"
  349. fvc.Equals(earlyCell, "1/1/00 02:24")
  350. earlyCell.NumFmt = "m/d/yy h:mm"
  351. fvc.Equals(earlyCell, "1/1/00 02:24")
  352. cell.NumFmt = "mm:ss"
  353. fvc.Equals(cell, "00:00")
  354. smallCell.NumFmt = "mm:ss"
  355. fvc.Equals(smallCell, "10:04")
  356. cell.NumFmt = "[hh]:mm:ss"
  357. fvc.Equals(cell, "18:00:00")
  358. cell.NumFmt = "[h]:mm:ss"
  359. fvc.Equals(cell, "18:00:00")
  360. smallCell.NumFmt = "[h]:mm:ss"
  361. fvc.Equals(smallCell, "10:04")
  362. const (
  363. expect1 = "0000.0086"
  364. expect2 = "1004.8000"
  365. format = "mmss.0000"
  366. tlen = len(format)
  367. )
  368. for i := 0; i < 3; i++ {
  369. tfmt := format[0 : tlen-i]
  370. cell.NumFmt = tfmt
  371. fvc.Equals(cell, expect1[0:tlen-i])
  372. smallCell.NumFmt = tfmt
  373. fvc.Equals(smallCell, expect2[0:tlen-i])
  374. }
  375. cell.NumFmt = "yyyy\\-mm\\-dd"
  376. fvc.Equals(cell, "2003\\-11\\-22")
  377. cell.NumFmt = "dd/mm/yyyy hh:mm:ss"
  378. fvc.Equals(cell, "22/11/2003 18:00:00")
  379. cell.NumFmt = "dd/mm/yy"
  380. fvc.Equals(cell, "22/11/03")
  381. earlyCell.NumFmt = "dd/mm/yy"
  382. fvc.Equals(earlyCell, "01/01/00")
  383. cell.NumFmt = "hh:mm:ss"
  384. fvc.Equals(cell, "18:00:00")
  385. smallCell.NumFmt = "hh:mm:ss"
  386. fvc.Equals(smallCell, "00:10:04")
  387. cell.NumFmt = "dd/mm/yy\\ hh:mm"
  388. fvc.Equals(cell, "22/11/03\\ 18:00")
  389. cell.NumFmt = "yyyy/mm/dd"
  390. fvc.Equals(cell, "2003/11/22")
  391. cell.NumFmt = "yy-mm-dd"
  392. fvc.Equals(cell, "03-11-22")
  393. cell.NumFmt = "d-mmm-yyyy"
  394. fvc.Equals(cell, "22-Nov-2003")
  395. earlyCell.NumFmt = "d-mmm-yyyy"
  396. fvc.Equals(earlyCell, "1-Jan-1900")
  397. cell.NumFmt = "m/d/yy"
  398. fvc.Equals(cell, "11/22/03")
  399. earlyCell.NumFmt = "m/d/yy"
  400. fvc.Equals(earlyCell, "1/1/00")
  401. cell.NumFmt = "m/d/yyyy"
  402. fvc.Equals(cell, "11/22/2003")
  403. earlyCell.NumFmt = "m/d/yyyy"
  404. fvc.Equals(earlyCell, "1/1/1900")
  405. cell.NumFmt = "dd-mmm-yyyy"
  406. fvc.Equals(cell, "22-Nov-2003")
  407. cell.NumFmt = "dd/mm/yyyy"
  408. fvc.Equals(cell, "22/11/2003")
  409. cell.NumFmt = "mm/dd/yy hh:mm am/pm"
  410. fvc.Equals(cell, "11/22/03 06:00 pm")
  411. cell.NumFmt = "mm/dd/yy h:mm am/pm"
  412. fvc.Equals(cell, "11/22/03 6:00 pm")
  413. cell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  414. fvc.Equals(cell, "11/22/2003 18:00:00")
  415. smallCell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  416. fvc.Equals(smallCell, "12/30/1899 00:10:04")
  417. cell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  418. fvc.Equals(cell, "2003-11-22 18:00:00")
  419. smallCell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  420. fvc.Equals(smallCell, "1899-12-30 00:10:04")
  421. cell.NumFmt = "mmmm d, yyyy"
  422. fvc.Equals(cell, "November 22, 2003")
  423. smallCell.NumFmt = "mmmm d, yyyy"
  424. fvc.Equals(smallCell, "December 30, 1899")
  425. cell.NumFmt = "dddd, mmmm dd, yyyy"
  426. fvc.Equals(cell, "Saturday, November 22, 2003")
  427. smallCell.NumFmt = "dddd, mmmm dd, yyyy"
  428. fvc.Equals(smallCell, "Saturday, December 30, 1899")
  429. }
  430. // test setters and getters
  431. func (s *CellSuite) TestSetterGetters(c *C) {
  432. cell := Cell{}
  433. cell.SetString("hello world")
  434. if val, err := cell.FormattedValue(); err != nil {
  435. c.Error(err)
  436. } else {
  437. c.Assert(val, Equals, "hello world")
  438. }
  439. c.Assert(cell.Type(), Equals, CellTypeString)
  440. cell.SetInt(1024)
  441. intValue, _ := cell.Int()
  442. c.Assert(intValue, Equals, 1024)
  443. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  444. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  445. cell.SetInt64(1024)
  446. int64Value, _ := cell.Int64()
  447. c.Assert(int64Value, Equals, int64(1024))
  448. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  449. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  450. cell.SetFloat(1.024)
  451. float, _ := cell.Float()
  452. intValue, _ = cell.Int() // convert
  453. c.Assert(float, Equals, 1.024)
  454. c.Assert(intValue, Equals, 1)
  455. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  456. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  457. cell.SetFormula("10+20")
  458. c.Assert(cell.Formula(), Equals, "10+20")
  459. c.Assert(cell.Type(), Equals, CellTypeFormula)
  460. }
  461. // TestOddInput is a regression test for #101. When the number format
  462. // was "@" (string), the input below caused a crash in strconv.ParseFloat.
  463. // The solution was to check if cell.Value was both a CellTypeString and
  464. // had a NumFmt of "general" or "@" and short-circuit FormattedValue() if so.
  465. func (s *CellSuite) TestOddInput(c *C) {
  466. cell := Cell{}
  467. odd := `[1],[12,"DATE NOT NULL DEFAULT '0000-00-00'"]`
  468. cell.Value = odd
  469. cell.NumFmt = "@"
  470. if val, err := cell.FormattedValue(); err != nil {
  471. c.Error(err)
  472. } else {
  473. c.Assert(val, Equals, odd)
  474. }
  475. }
  476. // TestBool tests basic Bool getting and setting booleans.
  477. func (s *CellSuite) TestBool(c *C) {
  478. cell := Cell{}
  479. cell.SetBool(true)
  480. c.Assert(cell.Value, Equals, "1")
  481. c.Assert(cell.Bool(), Equals, true)
  482. cell.SetBool(false)
  483. c.Assert(cell.Value, Equals, "0")
  484. c.Assert(cell.Bool(), Equals, false)
  485. }
  486. // TestStringBool tests calling Bool on a non CellTypeBool value.
  487. func (s *CellSuite) TestStringBool(c *C) {
  488. cell := Cell{}
  489. cell.SetInt(0)
  490. c.Assert(cell.Bool(), Equals, false)
  491. cell.SetInt(1)
  492. c.Assert(cell.Bool(), Equals, true)
  493. cell.SetString("")
  494. c.Assert(cell.Bool(), Equals, false)
  495. cell.SetString("0")
  496. c.Assert(cell.Bool(), Equals, true)
  497. }
  498. // TestSetValue tests whether SetValue handle properly for different type values.
  499. func (s *CellSuite) TestSetValue(c *C) {
  500. cell := Cell{}
  501. // int
  502. for _, i := range []interface{}{1, int8(1), int16(1), int32(1), int64(1)} {
  503. cell.SetValue(i)
  504. val, err := cell.Int64()
  505. c.Assert(err, IsNil)
  506. c.Assert(val, Equals, int64(1))
  507. }
  508. // float
  509. for _, i := range []interface{}{1.11, float32(1.11), float64(1.11)} {
  510. cell.SetValue(i)
  511. val, err := cell.Float()
  512. c.Assert(err, IsNil)
  513. c.Assert(val, Equals, 1.11)
  514. }
  515. // time
  516. cell.SetValue(time.Unix(0, 0))
  517. val, err := cell.Float()
  518. c.Assert(err, IsNil)
  519. c.Assert(math.Floor(val), Equals, 25569.0)
  520. // string and nil
  521. for _, i := range []interface{}{nil, "", []byte("")} {
  522. cell.SetValue(i)
  523. c.Assert(cell.Value, Equals, "")
  524. }
  525. // others
  526. cell.SetValue([]string{"test"})
  527. c.Assert(cell.Value, Equals, "[test]")
  528. }
  529. func (s *CellSuite) TestSetDateWithOptions(c *C) {
  530. cell := Cell{}
  531. // time
  532. cell.SetDate(time.Unix(0, 0))
  533. val, err := cell.Float()
  534. c.Assert(err, IsNil)
  535. c.Assert(math.Floor(val), Equals, 25569.0)
  536. // our test subject
  537. date2016UTC := time.Date(2016, 1, 1, 12, 0, 0, 0, time.UTC)
  538. // test ny timezone
  539. nyTZ, err := time.LoadLocation("America/New_York")
  540. c.Assert(err, IsNil)
  541. cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
  542. ExcelTimeFormat: "test_format1",
  543. Location: nyTZ,
  544. })
  545. val, err = cell.Float()
  546. c.Assert(err, IsNil)
  547. c.Assert(val, Equals, TimeToExcelTime(time.Date(2016, 1, 1, 7, 0, 0, 0, time.UTC)))
  548. // test jp timezone
  549. jpTZ, err := time.LoadLocation("Asia/Tokyo")
  550. c.Assert(err, IsNil)
  551. cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
  552. ExcelTimeFormat: "test_format2",
  553. Location: jpTZ,
  554. })
  555. val, err = cell.Float()
  556. c.Assert(err, IsNil)
  557. c.Assert(val, Equals, TimeToExcelTime(time.Date(2016, 1, 1, 21, 0, 0, 0, time.UTC)))
  558. }
  559. func (s *CellSuite) TestIsTimeFormat(c *C) {
  560. c.Assert(isTimeFormat("yy"), Equals, true)
  561. c.Assert(isTimeFormat("hh"), Equals, true)
  562. c.Assert(isTimeFormat("h"), Equals, true)
  563. c.Assert(isTimeFormat("am/pm"), Equals, true)
  564. c.Assert(isTimeFormat("AM/PM"), Equals, true)
  565. c.Assert(isTimeFormat("A/P"), Equals, true)
  566. c.Assert(isTimeFormat("a/p"), Equals, true)
  567. c.Assert(isTimeFormat("ss"), Equals, true)
  568. c.Assert(isTimeFormat("mm"), Equals, true)
  569. c.Assert(isTimeFormat(":"), Equals, false)
  570. c.Assert(isTimeFormat("z"), Equals, false)
  571. }
  572. func (s *CellSuite) TestIs12HourtTime(c *C) {
  573. c.Assert(is12HourTime("am/pm"), Equals, true)
  574. c.Assert(is12HourTime("AM/PM"), Equals, true)
  575. c.Assert(is12HourTime("a/p"), Equals, true)
  576. c.Assert(is12HourTime("A/P"), Equals, true)
  577. c.Assert(is12HourTime("x"), Equals, false)
  578. }