cell_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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, "0.000005")
  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. noScientificValueOutput string
  117. }{
  118. {
  119. value: "18.989999999999998",
  120. formattedValueOutput: "18.99",
  121. noScientificValueOutput: "18.99",
  122. },
  123. {
  124. value: "1.1000000000000001",
  125. formattedValueOutput: "1.1",
  126. noScientificValueOutput: "1.1",
  127. },
  128. {
  129. value: "0.0000000000000001",
  130. formattedValueOutput: "1E-16",
  131. noScientificValueOutput: "0.0000000000000001",
  132. },
  133. {
  134. value: "0.000000000000008",
  135. formattedValueOutput: "8E-15",
  136. noScientificValueOutput: "0.000000000000008",
  137. },
  138. {
  139. value: "1000000000000000000",
  140. formattedValueOutput: "1E+18",
  141. noScientificValueOutput: "1000000000000000000",
  142. },
  143. {
  144. value: "1230000000000000000",
  145. formattedValueOutput: "1.23E+18",
  146. noScientificValueOutput: "1230000000000000000",
  147. },
  148. {
  149. value: "12345678",
  150. formattedValueOutput: "12345678",
  151. noScientificValueOutput: "12345678",
  152. },
  153. {
  154. value: "0",
  155. formattedValueOutput: "0",
  156. noScientificValueOutput: "0",
  157. },
  158. {
  159. value: "-18.989999999999998",
  160. formattedValueOutput: "-18.99",
  161. noScientificValueOutput: "-18.99",
  162. },
  163. {
  164. value: "-1.1000000000000001",
  165. formattedValueOutput: "-1.1",
  166. noScientificValueOutput: "-1.1",
  167. },
  168. {
  169. value: "-0.0000000000000001",
  170. formattedValueOutput: "-1E-16",
  171. noScientificValueOutput: "-0.0000000000000001",
  172. },
  173. {
  174. value: "-0.000000000000008",
  175. formattedValueOutput: "-8E-15",
  176. noScientificValueOutput: "-0.000000000000008",
  177. },
  178. {
  179. value: "-1000000000000000000",
  180. formattedValueOutput: "-1E+18",
  181. noScientificValueOutput: "-1000000000000000000",
  182. },
  183. {
  184. value: "-1230000000000000000",
  185. formattedValueOutput: "-1.23E+18",
  186. noScientificValueOutput: "-1230000000000000000",
  187. },
  188. {
  189. value: "-12345678",
  190. formattedValueOutput: "-12345678",
  191. noScientificValueOutput: "-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.noScientificValueOutput)
  210. }
  211. }
  212. // TestCellTypeFormatHandling tests all cell types other than numeric. Numeric cells are tested above since those
  213. // cells have so many edge cases.
  214. func (l *CellSuite) TestCellTypeFormatHandling(c *C) {
  215. testCases := []struct {
  216. cellType CellType
  217. numFmt string
  218. value string
  219. formattedValueOutput string
  220. expectError bool
  221. }{
  222. // All of the string cell types, will return only the string format if there is no @ symbol in the format.
  223. {
  224. cellType: CellTypeInline,
  225. numFmt: `0;0;0;"Error"`,
  226. value: "asdf",
  227. formattedValueOutput: "Error",
  228. },
  229. {
  230. cellType: CellTypeString,
  231. numFmt: `0;0;0;"Error"`,
  232. value: "asdf",
  233. formattedValueOutput: "Error",
  234. },
  235. {
  236. cellType: CellTypeStringFormula,
  237. numFmt: `0;0;0;"Error"`,
  238. value: "asdf",
  239. formattedValueOutput: "Error",
  240. },
  241. // Errors are returned as is regardless of what the format shows
  242. {
  243. cellType: CellTypeError,
  244. numFmt: `0;0;0;"Error"`,
  245. value: "#NAME?",
  246. formattedValueOutput: "#NAME?",
  247. },
  248. {
  249. cellType: CellTypeError,
  250. numFmt: `"$"@`,
  251. value: "#######",
  252. formattedValueOutput: "#######",
  253. },
  254. // Dates are returned as is regardless of what the format shows
  255. {
  256. cellType: CellTypeDate,
  257. numFmt: `"$"@`,
  258. value: "2017-10-24T15:29:30+00:00",
  259. formattedValueOutput: "2017-10-24T15:29:30+00:00",
  260. },
  261. // Make sure the format used above would have done something for a string
  262. {
  263. cellType: CellTypeString,
  264. numFmt: `"$"@`,
  265. value: "#######",
  266. formattedValueOutput: "$#######",
  267. },
  268. // For bool cells, 0 is false, 1 is true, anything else will error
  269. {
  270. cellType: CellTypeBool,
  271. numFmt: `"$"@`,
  272. value: "1",
  273. formattedValueOutput: "TRUE",
  274. },
  275. {
  276. cellType: CellTypeBool,
  277. numFmt: `"$"@`,
  278. value: "0",
  279. formattedValueOutput: "FALSE",
  280. },
  281. {
  282. cellType: CellTypeBool,
  283. numFmt: `"$"@`,
  284. value: "2",
  285. expectError: true,
  286. formattedValueOutput: "2",
  287. },
  288. {
  289. cellType: CellTypeBool,
  290. numFmt: `"$"@`,
  291. value: "2",
  292. expectError: true,
  293. formattedValueOutput: "2",
  294. },
  295. // Invalid cell type should cause an error
  296. {
  297. cellType: CellType(7),
  298. numFmt: `0`,
  299. value: "1.0",
  300. expectError: true,
  301. formattedValueOutput: "1.0",
  302. },
  303. }
  304. for _, testCase := range testCases {
  305. cell := Cell{
  306. cellType: testCase.cellType,
  307. NumFmt: testCase.numFmt,
  308. Value: testCase.value,
  309. }
  310. val, err := cell.FormattedValue()
  311. if err != nil != testCase.expectError {
  312. c.Fatal(err)
  313. }
  314. c.Assert(val, Equals, testCase.formattedValueOutput)
  315. }
  316. }
  317. func (s *CellSuite) TestGetTime(c *C) {
  318. cell := Cell{}
  319. cell.SetFloat(0)
  320. date, err := cell.GetTime(false)
  321. c.Assert(err, Equals, nil)
  322. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  323. cell.SetFloat(39813.0)
  324. date, err = cell.GetTime(true)
  325. c.Assert(err, Equals, nil)
  326. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  327. cell.Value = "d"
  328. _, err = cell.GetTime(false)
  329. c.Assert(err, NotNil)
  330. }
  331. // FormattedValue returns an error for formatting errors
  332. func (l *CellSuite) TestFormattedValueErrorsOnBadFormat(c *C) {
  333. cell := Cell{Value: "Fudge Cake", cellType: CellTypeNumeric}
  334. cell.NumFmt = "#,##0 ;(#,##0)"
  335. value, err := cell.FormattedValue()
  336. c.Assert(value, Equals, "Fudge Cake")
  337. c.Assert(err, NotNil)
  338. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  339. }
  340. // formattedValueChecker removes all the boilerplate for testing Cell.FormattedValue
  341. // after its change from returning one value (a string) to two values (string, error)
  342. // This allows all the old one-line asserts in the test to continue to be one
  343. // line, instead of multi-line with error checking.
  344. type formattedValueChecker struct {
  345. c *C
  346. }
  347. func (fvc *formattedValueChecker) Equals(cell Cell, expected string) {
  348. val, err := cell.FormattedValue()
  349. if err != nil {
  350. fvc.c.Error(err)
  351. }
  352. fvc.c.Assert(val, Equals, expected)
  353. }
  354. func cellsFormattedValueEquals(t *testing.T, cell *Cell, expected string) {
  355. val, err := cell.FormattedValue()
  356. if err != nil {
  357. t.Error(err)
  358. }
  359. if val != expected {
  360. t.Errorf("Expected cell.FormattedValue() to be %v, got %v", expected, val)
  361. }
  362. }
  363. // We can return a string representation of the formatted data
  364. func (l *CellSuite) TestFormattedValue(c *C) {
  365. cell := Cell{Value: "37947.7500001", cellType: CellTypeNumeric}
  366. negativeCell := Cell{Value: "-37947.7500001", cellType: CellTypeNumeric}
  367. smallCell := Cell{Value: "0.007", cellType: CellTypeNumeric}
  368. earlyCell := Cell{Value: "2.1", cellType: CellTypeNumeric}
  369. fvc := formattedValueChecker{c: c}
  370. cell.NumFmt = "general"
  371. fvc.Equals(cell, "37947.7500001")
  372. negativeCell.NumFmt = "general"
  373. fvc.Equals(negativeCell, "-37947.7500001")
  374. // TODO: This test is currently broken. For a string type cell, I
  375. // don't think FormattedValue() should be doing a numeric conversion on the value
  376. // before returning the string.
  377. cell.NumFmt = "0"
  378. fvc.Equals(cell, "37948")
  379. cell.NumFmt = "#,##0" // For the time being we're not doing
  380. // this comma formatting, so it'll fall back to the related
  381. // non-comma form.
  382. fvc.Equals(cell, "37948")
  383. cell.NumFmt = "#,##0.00;(#,##0.00)"
  384. fvc.Equals(cell, "37947.75")
  385. cell.NumFmt = "0.00"
  386. fvc.Equals(cell, "37947.75")
  387. cell.NumFmt = "#,##0.00" // For the time being we're not doing
  388. // this comma formatting, so it'll fall back to the related
  389. // non-comma form.
  390. fvc.Equals(cell, "37947.75")
  391. cell.NumFmt = "#,##0 ;(#,##0)"
  392. fvc.Equals(cell, "37948")
  393. negativeCell.NumFmt = "#,##0 ;(#,##0)"
  394. fvc.Equals(negativeCell, "(37948)")
  395. cell.NumFmt = "#,##0 ;[red](#,##0)"
  396. fvc.Equals(cell, "37948")
  397. negativeCell.NumFmt = "#,##0 ;[red](#,##0)"
  398. fvc.Equals(negativeCell, "(37948)")
  399. negativeCell.NumFmt = "#,##0.00;(#,##0.00)"
  400. fvc.Equals(negativeCell, "(37947.75)")
  401. cell.NumFmt = "0%"
  402. fvc.Equals(cell, "3794775%")
  403. cell.NumFmt = "0.00%"
  404. fvc.Equals(cell, "3794775.00%")
  405. cell.NumFmt = "0.00e+00"
  406. fvc.Equals(cell, "3.794775e+04")
  407. cell.NumFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  408. fvc.Equals(cell, "3.794775e+04")
  409. cell.NumFmt = "mm-dd-yy"
  410. fvc.Equals(cell, "11-22-03")
  411. cell.NumFmt = "d-mmm-yy"
  412. fvc.Equals(cell, "22-Nov-03")
  413. earlyCell.NumFmt = "d-mmm-yy"
  414. fvc.Equals(earlyCell, "1-Jan-00")
  415. cell.NumFmt = "d-mmm"
  416. fvc.Equals(cell, "22-Nov")
  417. earlyCell.NumFmt = "d-mmm"
  418. fvc.Equals(earlyCell, "1-Jan")
  419. cell.NumFmt = "mmm-yy"
  420. fvc.Equals(cell, "Nov-03")
  421. cell.NumFmt = "h:mm am/pm"
  422. fvc.Equals(cell, "6:00 pm")
  423. smallCell.NumFmt = "h:mm am/pm"
  424. fvc.Equals(smallCell, "12:10 am")
  425. cell.NumFmt = "h:mm:ss am/pm"
  426. fvc.Equals(cell, "6:00:00 pm")
  427. cell.NumFmt = "hh:mm:ss"
  428. fvc.Equals(cell, "18:00:00")
  429. smallCell.NumFmt = "h:mm:ss am/pm"
  430. fvc.Equals(smallCell, "12:10:04 am")
  431. cell.NumFmt = "h:mm"
  432. fvc.Equals(cell, "18:00")
  433. smallCell.NumFmt = "h:mm"
  434. fvc.Equals(smallCell, "00:10")
  435. smallCell.NumFmt = "hh:mm"
  436. fvc.Equals(smallCell, "00:10")
  437. cell.NumFmt = "h:mm:ss"
  438. fvc.Equals(cell, "18:00:00")
  439. cell.NumFmt = "hh:mm:ss"
  440. fvc.Equals(cell, "18:00:00")
  441. smallCell.NumFmt = "hh:mm:ss"
  442. fvc.Equals(smallCell, "00:10:04")
  443. smallCell.NumFmt = "h:mm:ss"
  444. fvc.Equals(smallCell, "00:10:04")
  445. cell.NumFmt = "m/d/yy h:mm"
  446. fvc.Equals(cell, "11/22/03 18:00")
  447. cell.NumFmt = "m/d/yy hh:mm"
  448. fvc.Equals(cell, "11/22/03 18:00")
  449. smallCell.NumFmt = "m/d/yy h:mm"
  450. fvc.Equals(smallCell, "12/30/99 00:10")
  451. smallCell.NumFmt = "m/d/yy hh:mm"
  452. fvc.Equals(smallCell, "12/30/99 00:10")
  453. earlyCell.NumFmt = "m/d/yy hh:mm"
  454. fvc.Equals(earlyCell, "1/1/00 02:24")
  455. earlyCell.NumFmt = "m/d/yy h:mm"
  456. fvc.Equals(earlyCell, "1/1/00 02:24")
  457. cell.NumFmt = "mm:ss"
  458. fvc.Equals(cell, "00:00")
  459. smallCell.NumFmt = "mm:ss"
  460. fvc.Equals(smallCell, "10:04")
  461. cell.NumFmt = "[hh]:mm:ss"
  462. fvc.Equals(cell, "18:00:00")
  463. cell.NumFmt = "[h]:mm:ss"
  464. fvc.Equals(cell, "18:00:00")
  465. smallCell.NumFmt = "[h]:mm:ss"
  466. fvc.Equals(smallCell, "10:04")
  467. const (
  468. expect1 = "0000.0086"
  469. expect2 = "1004.8000"
  470. format = "mmss.0000"
  471. tlen = len(format)
  472. )
  473. for i := 0; i < 3; i++ {
  474. tfmt := format[0 : tlen-i]
  475. cell.NumFmt = tfmt
  476. fvc.Equals(cell, expect1[0:tlen-i])
  477. smallCell.NumFmt = tfmt
  478. fvc.Equals(smallCell, expect2[0:tlen-i])
  479. }
  480. cell.NumFmt = "yyyy\\-mm\\-dd"
  481. fvc.Equals(cell, "2003\\-11\\-22")
  482. cell.NumFmt = "dd/mm/yyyy hh:mm:ss"
  483. fvc.Equals(cell, "22/11/2003 18:00:00")
  484. cell.NumFmt = "dd/mm/yy"
  485. fvc.Equals(cell, "22/11/03")
  486. earlyCell.NumFmt = "dd/mm/yy"
  487. fvc.Equals(earlyCell, "01/01/00")
  488. cell.NumFmt = "hh:mm:ss"
  489. fvc.Equals(cell, "18:00:00")
  490. smallCell.NumFmt = "hh:mm:ss"
  491. fvc.Equals(smallCell, "00:10:04")
  492. cell.NumFmt = "dd/mm/yy\\ hh:mm"
  493. fvc.Equals(cell, "22/11/03\\ 18:00")
  494. cell.NumFmt = "yyyy/mm/dd"
  495. fvc.Equals(cell, "2003/11/22")
  496. cell.NumFmt = "yy-mm-dd"
  497. fvc.Equals(cell, "03-11-22")
  498. cell.NumFmt = "d-mmm-yyyy"
  499. fvc.Equals(cell, "22-Nov-2003")
  500. earlyCell.NumFmt = "d-mmm-yyyy"
  501. fvc.Equals(earlyCell, "1-Jan-1900")
  502. cell.NumFmt = "m/d/yy"
  503. fvc.Equals(cell, "11/22/03")
  504. earlyCell.NumFmt = "m/d/yy"
  505. fvc.Equals(earlyCell, "1/1/00")
  506. cell.NumFmt = "m/d/yyyy"
  507. fvc.Equals(cell, "11/22/2003")
  508. earlyCell.NumFmt = "m/d/yyyy"
  509. fvc.Equals(earlyCell, "1/1/1900")
  510. cell.NumFmt = "dd-mmm-yyyy"
  511. fvc.Equals(cell, "22-Nov-2003")
  512. cell.NumFmt = "dd/mm/yyyy"
  513. fvc.Equals(cell, "22/11/2003")
  514. cell.NumFmt = "mm/dd/yy hh:mm am/pm"
  515. fvc.Equals(cell, "11/22/03 06:00 pm")
  516. cell.NumFmt = "mm/dd/yy h:mm am/pm"
  517. fvc.Equals(cell, "11/22/03 6:00 pm")
  518. cell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  519. fvc.Equals(cell, "11/22/2003 18:00:00")
  520. smallCell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  521. fvc.Equals(smallCell, "12/30/1899 00:10:04")
  522. cell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  523. fvc.Equals(cell, "2003-11-22 18:00:00")
  524. smallCell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  525. fvc.Equals(smallCell, "1899-12-30 00:10:04")
  526. cell.NumFmt = "mmmm d, yyyy"
  527. fvc.Equals(cell, "November 22, 2003")
  528. smallCell.NumFmt = "mmmm d, yyyy"
  529. fvc.Equals(smallCell, "December 30, 1899")
  530. cell.NumFmt = "dddd, mmmm dd, yyyy"
  531. fvc.Equals(cell, "Saturday, November 22, 2003")
  532. smallCell.NumFmt = "dddd, mmmm dd, yyyy"
  533. fvc.Equals(smallCell, "Saturday, December 30, 1899")
  534. }
  535. func (s *CellSuite) TestTimeToExcelTime(c *C) {
  536. c.Assert(0.0, Equals, TimeToExcelTime(time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC), false))
  537. c.Assert(-1462.0, Equals, TimeToExcelTime(time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC), true))
  538. c.Assert(25569.0, Equals, TimeToExcelTime(time.Unix(0, 0), false))
  539. c.Assert(43269.0, Equals, TimeToExcelTime(time.Date(2018, 6, 18, 0, 0, 0, 0, time.UTC), false))
  540. c.Assert(401769.0, Equals, TimeToExcelTime(time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC), false))
  541. smallDate := time.Date(1899, 12, 30, 0, 0, 0, 1000, time.UTC)
  542. smallExcelTime := TimeToExcelTime(smallDate, false)
  543. c.Assert(true, Equals, 0.0 != smallExcelTime)
  544. roundTrippedDate := TimeFromExcelTime(smallExcelTime, false)
  545. c.Assert(roundTrippedDate, Equals, smallDate)
  546. }
  547. // test setters and getters
  548. func (s *CellSuite) TestSetterGetters(c *C) {
  549. cell := Cell{}
  550. cell.SetString("hello world")
  551. if val, err := cell.FormattedValue(); err != nil {
  552. c.Error(err)
  553. } else {
  554. c.Assert(val, Equals, "hello world")
  555. }
  556. c.Assert(cell.Type(), Equals, CellTypeString)
  557. cell.SetInt(1024)
  558. intValue, _ := cell.Int()
  559. c.Assert(intValue, Equals, 1024)
  560. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  561. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  562. cell.SetInt64(1024)
  563. int64Value, _ := cell.Int64()
  564. c.Assert(int64Value, Equals, int64(1024))
  565. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  566. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  567. cell.SetFloat(1.024)
  568. float, _ := cell.Float()
  569. intValue, _ = cell.Int() // convert
  570. c.Assert(float, Equals, 1.024)
  571. c.Assert(intValue, Equals, 1)
  572. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  573. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  574. cell.SetFormula("10+20")
  575. c.Assert(cell.Formula(), Equals, "10+20")
  576. c.Assert(cell.Type(), Equals, CellTypeNumeric)
  577. cell.SetStringFormula("A1")
  578. c.Assert(cell.Formula(), Equals, "A1")
  579. c.Assert(cell.Type(), Equals, CellTypeStringFormula)
  580. }
  581. // TestOddInput is a regression test for #101. When the number format
  582. // was "@" (string), the input below caused a crash in strconv.ParseFloat.
  583. // The solution was to check if cell.Value was both a CellTypeString and
  584. // had a NumFmt of "general" or "@" and short-circuit FormattedValue() if so.
  585. func (s *CellSuite) TestOddInput(c *C) {
  586. cell := Cell{}
  587. odd := `[1],[12,"DATE NOT NULL DEFAULT '0000-00-00'"]`
  588. cell.Value = odd
  589. cell.NumFmt = "@"
  590. if val, err := cell.FormattedValue(); err != nil {
  591. c.Error(err)
  592. } else {
  593. c.Assert(val, Equals, odd)
  594. }
  595. }
  596. // TestBool tests basic Bool getting and setting booleans.
  597. func (s *CellSuite) TestBool(c *C) {
  598. cell := Cell{}
  599. cell.SetBool(true)
  600. c.Assert(cell.Value, Equals, "1")
  601. c.Assert(cell.Bool(), Equals, true)
  602. cell.SetBool(false)
  603. c.Assert(cell.Value, Equals, "0")
  604. c.Assert(cell.Bool(), Equals, false)
  605. }
  606. // TestStringBool tests calling Bool on a non CellTypeBool value.
  607. func (s *CellSuite) TestStringBool(c *C) {
  608. cell := Cell{}
  609. cell.SetInt(0)
  610. c.Assert(cell.Bool(), Equals, false)
  611. cell.SetInt(1)
  612. c.Assert(cell.Bool(), Equals, true)
  613. cell.SetString("")
  614. c.Assert(cell.Bool(), Equals, false)
  615. cell.SetString("0")
  616. c.Assert(cell.Bool(), Equals, true)
  617. }
  618. // TestSetValue tests whether SetValue handle properly for different type values.
  619. func (s *CellSuite) TestSetValue(c *C) {
  620. cell := Cell{}
  621. // int
  622. for _, i := range []interface{}{1, int8(1), int16(1), int32(1), int64(1)} {
  623. cell.SetValue(i)
  624. val, err := cell.Int64()
  625. c.Assert(err, IsNil)
  626. c.Assert(val, Equals, int64(1))
  627. }
  628. // float
  629. for _, i := range []interface{}{1.11, float32(1.11), float64(1.11)} {
  630. cell.SetValue(i)
  631. val, err := cell.Float()
  632. c.Assert(err, IsNil)
  633. c.Assert(val, Equals, 1.11)
  634. }
  635. // In the naive implementation using go fmt "%v", this test would fail and the cell.Value would be "1e-06"
  636. for _, i := range []interface{}{0.000001, float32(0.000001), float64(0.000001)} {
  637. cell.SetValue(i)
  638. c.Assert(cell.Value, Equals, "0.000001")
  639. val, err := cell.Float()
  640. c.Assert(err, IsNil)
  641. c.Assert(val, Equals, 0.000001)
  642. }
  643. // time
  644. cell.SetValue(time.Unix(0, 0))
  645. val, err := cell.Float()
  646. c.Assert(err, IsNil)
  647. c.Assert(math.Floor(val), Equals, 25569.0)
  648. // string and nil
  649. for _, i := range []interface{}{nil, "", []byte("")} {
  650. cell.SetValue(i)
  651. c.Assert(cell.Value, Equals, "")
  652. }
  653. // others
  654. cell.SetValue([]string{"test"})
  655. c.Assert(cell.Value, Equals, "[test]")
  656. }
  657. func (s *CellSuite) TestSetDateWithOptions(c *C) {
  658. cell := Cell{}
  659. // time
  660. cell.SetDate(time.Unix(0, 0))
  661. val, err := cell.Float()
  662. c.Assert(err, IsNil)
  663. c.Assert(math.Floor(val), Equals, 25569.0)
  664. // our test subject
  665. date2016UTC := time.Date(2016, 1, 1, 12, 0, 0, 0, time.UTC)
  666. // test ny timezone
  667. nyTZ, err := time.LoadLocation("America/New_York")
  668. c.Assert(err, IsNil)
  669. cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
  670. ExcelTimeFormat: "test_format1",
  671. Location: nyTZ,
  672. })
  673. val, err = cell.Float()
  674. c.Assert(err, IsNil)
  675. c.Assert(val, Equals, TimeToExcelTime(time.Date(2016, 1, 1, 7, 0, 0, 0, time.UTC), false))
  676. // test jp timezone
  677. jpTZ, err := time.LoadLocation("Asia/Tokyo")
  678. c.Assert(err, IsNil)
  679. cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
  680. ExcelTimeFormat: "test_format2",
  681. Location: jpTZ,
  682. })
  683. val, err = cell.Float()
  684. c.Assert(err, IsNil)
  685. c.Assert(val, Equals, TimeToExcelTime(time.Date(2016, 1, 1, 21, 0, 0, 0, time.UTC), false))
  686. }
  687. func (s *CellSuite) TestIsTimeFormat(c *C) {
  688. c.Assert(isTimeFormat("yy"), Equals, true)
  689. c.Assert(isTimeFormat("hh"), Equals, true)
  690. c.Assert(isTimeFormat("h"), Equals, true)
  691. c.Assert(isTimeFormat("am/pm"), Equals, true)
  692. c.Assert(isTimeFormat("AM/PM"), Equals, true)
  693. c.Assert(isTimeFormat("A/P"), Equals, true)
  694. c.Assert(isTimeFormat("a/p"), Equals, true)
  695. c.Assert(isTimeFormat("ss"), Equals, true)
  696. c.Assert(isTimeFormat("mm"), Equals, true)
  697. c.Assert(isTimeFormat(":"), Equals, false)
  698. c.Assert(isTimeFormat("z"), Equals, false)
  699. }
  700. func (s *CellSuite) TestIs12HourtTime(c *C) {
  701. c.Assert(is12HourTime("am/pm"), Equals, true)
  702. c.Assert(is12HourTime("AM/PM"), Equals, true)
  703. c.Assert(is12HourTime("a/p"), Equals, true)
  704. c.Assert(is12HourTime("A/P"), Equals, true)
  705. c.Assert(is12HourTime("x"), Equals, false)
  706. }