cell_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 (l *CellSuite) TestGeneralNumberHandling(c *C) {
  107. // If you go to Excel, make a new file, type 18.99 in a cell, and save, what you will get is a
  108. // cell where the format is General and the storage type is Number, that contains the value 18.989999999999998.
  109. // The correct way to format this should be 18.99.
  110. // 1.1 will get you the same, with a stored value of 1.1000000000000001.
  111. // Also, numbers greater than 1e11 and less than 1e-9 wil be shown as scientific notation.
  112. testCases := []struct {
  113. value string
  114. output string
  115. }{
  116. {
  117. value: "18.989999999999998",
  118. output: "18.99",
  119. },
  120. {
  121. value: "1.1000000000000001",
  122. output: "1.1",
  123. },
  124. {
  125. value: "0.0000000000000001",
  126. output: "1E-16",
  127. },
  128. {
  129. value: "0.000000000000008",
  130. output: "8E-15",
  131. },
  132. {
  133. value: "1000000000000000000",
  134. output: "1E+18",
  135. },
  136. {
  137. value: "1230000000000000000",
  138. output: "1.23E+18",
  139. },
  140. {
  141. value: "12345678",
  142. output: "12345678",
  143. },
  144. }
  145. for _, testCase := range testCases {
  146. cell := Cell{
  147. cellType: CellTypeNumeric,
  148. NumFmt: builtInNumFmt[builtInNumFmtIndex_GENERAL],
  149. Value: testCase.value,
  150. }
  151. val, err := cell.FormattedValue()
  152. if err != nil {
  153. c.Fatal(err)
  154. }
  155. c.Assert(val, Equals, testCase.output)
  156. }
  157. }
  158. func (s *CellSuite) TestGetTime(c *C) {
  159. cell := Cell{}
  160. cell.SetFloat(0)
  161. date, err := cell.GetTime(false)
  162. c.Assert(err, Equals, nil)
  163. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  164. cell.SetFloat(39813.0)
  165. date, err = cell.GetTime(true)
  166. c.Assert(err, Equals, nil)
  167. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  168. cell.Value = "d"
  169. _, err = cell.GetTime(false)
  170. c.Assert(err, NotNil)
  171. }
  172. // FormattedValue returns an error for formatting errors
  173. func (l *CellSuite) TestFormattedValueErrorsOnBadFormat(c *C) {
  174. cell := Cell{Value: "Fudge Cake"}
  175. cell.NumFmt = "#,##0 ;(#,##0)"
  176. value, err := cell.FormattedValue()
  177. c.Assert(value, Equals, "Fudge Cake")
  178. c.Assert(err, NotNil)
  179. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  180. }
  181. // FormattedValue returns a string containing error text for formatting errors
  182. func (l *CellSuite) TestFormattedValueReturnsErrorAsValueForBadFormat(c *C) {
  183. cell := Cell{Value: "Fudge Cake"}
  184. cell.NumFmt = "#,##0 ;(#,##0)"
  185. _, err := cell.FormattedValue()
  186. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  187. }
  188. // formattedValueChecker removes all the boilerplate for testing Cell.FormattedValue
  189. // after its change from returning one value (a string) to two values (string, error)
  190. // This allows all the old one-line asserts in the test to continue to be one
  191. // line, instead of multi-line with error checking.
  192. type formattedValueChecker struct {
  193. c *C
  194. }
  195. func (fvc *formattedValueChecker) Equals(cell Cell, expected string) {
  196. val, err := cell.FormattedValue()
  197. if err != nil {
  198. fvc.c.Error(err)
  199. }
  200. fvc.c.Assert(val, Equals, expected)
  201. }
  202. // We can return a string representation of the formatted data
  203. func (l *CellSuite) TestFormattedValue(c *C) {
  204. // XXX TODO, this test should probably be split down, and made
  205. // in terms of SafeFormattedValue, as FormattedValue wraps
  206. // that function now.
  207. cell := Cell{Value: "37947.7500001"}
  208. negativeCell := Cell{Value: "-37947.7500001"}
  209. smallCell := Cell{Value: "0.007"}
  210. earlyCell := Cell{Value: "2.1"}
  211. fvc := formattedValueChecker{c: c}
  212. cell.NumFmt = "general"
  213. fvc.Equals(cell, "37947.7500001")
  214. negativeCell.NumFmt = "general"
  215. fvc.Equals(negativeCell, "-37947.7500001")
  216. // TODO: This test is currently broken. For a string type cell, I
  217. // don't think FormattedValue() should be doing a numeric conversion on the value
  218. // before returning the string.
  219. cell.NumFmt = "0"
  220. fvc.Equals(cell, "37947")
  221. cell.NumFmt = "#,##0" // For the time being we're not doing
  222. // this comma formatting, so it'll fall back to the related
  223. // non-comma form.
  224. fvc.Equals(cell, "37947")
  225. cell.NumFmt = "#,##0.00;(#,##0.00)"
  226. fvc.Equals(cell, "37947.75")
  227. cell.NumFmt = "0.00"
  228. fvc.Equals(cell, "37947.75")
  229. cell.NumFmt = "#,##0.00" // For the time being we're not doing
  230. // this comma formatting, so it'll fall back to the related
  231. // non-comma form.
  232. fvc.Equals(cell, "37947.75")
  233. cell.NumFmt = "#,##0 ;(#,##0)"
  234. fvc.Equals(cell, "37947")
  235. negativeCell.NumFmt = "#,##0 ;(#,##0)"
  236. fvc.Equals(negativeCell, "(37947)")
  237. cell.NumFmt = "#,##0 ;[red](#,##0)"
  238. fvc.Equals(cell, "37947")
  239. negativeCell.NumFmt = "#,##0 ;[red](#,##0)"
  240. fvc.Equals(negativeCell, "(37947)")
  241. negativeCell.NumFmt = "#,##0.00;(#,##0.00)"
  242. fvc.Equals(negativeCell, "(-37947.75)")
  243. cell.NumFmt = "0%"
  244. fvc.Equals(cell, "3794775%")
  245. cell.NumFmt = "0.00%"
  246. fvc.Equals(cell, "3794775.00%")
  247. cell.NumFmt = "0.00e+00"
  248. fvc.Equals(cell, "3.794775e+04")
  249. cell.NumFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  250. fvc.Equals(cell, "3.794775e+04")
  251. cell.NumFmt = "mm-dd-yy"
  252. fvc.Equals(cell, "11-22-03")
  253. cell.NumFmt = "d-mmm-yy"
  254. fvc.Equals(cell, "22-Nov-03")
  255. earlyCell.NumFmt = "d-mmm-yy"
  256. fvc.Equals(earlyCell, "1-Jan-00")
  257. cell.NumFmt = "d-mmm"
  258. fvc.Equals(cell, "22-Nov")
  259. earlyCell.NumFmt = "d-mmm"
  260. fvc.Equals(earlyCell, "1-Jan")
  261. cell.NumFmt = "mmm-yy"
  262. fvc.Equals(cell, "Nov-03")
  263. cell.NumFmt = "h:mm am/pm"
  264. fvc.Equals(cell, "6:00 pm")
  265. smallCell.NumFmt = "h:mm am/pm"
  266. fvc.Equals(smallCell, "12:10 am")
  267. cell.NumFmt = "h:mm:ss am/pm"
  268. fvc.Equals(cell, "6:00:00 pm")
  269. cell.NumFmt = "hh:mm:ss"
  270. fvc.Equals(cell, "18:00:00")
  271. smallCell.NumFmt = "h:mm:ss am/pm"
  272. fvc.Equals(smallCell, "12:10:04 am")
  273. cell.NumFmt = "h:mm"
  274. fvc.Equals(cell, "18:00")
  275. smallCell.NumFmt = "h:mm"
  276. fvc.Equals(smallCell, "00:10")
  277. smallCell.NumFmt = "hh:mm"
  278. fvc.Equals(smallCell, "00:10")
  279. cell.NumFmt = "h:mm:ss"
  280. fvc.Equals(cell, "18:00:00")
  281. cell.NumFmt = "hh:mm:ss"
  282. fvc.Equals(cell, "18:00:00")
  283. smallCell.NumFmt = "hh:mm:ss"
  284. fvc.Equals(smallCell, "00:10:04")
  285. smallCell.NumFmt = "h:mm:ss"
  286. fvc.Equals(smallCell, "00:10:04")
  287. cell.NumFmt = "m/d/yy h:mm"
  288. fvc.Equals(cell, "11/22/03 18:00")
  289. cell.NumFmt = "m/d/yy hh:mm"
  290. fvc.Equals(cell, "11/22/03 18:00")
  291. smallCell.NumFmt = "m/d/yy h:mm"
  292. fvc.Equals(smallCell, "12/30/99 00:10")
  293. smallCell.NumFmt = "m/d/yy hh:mm"
  294. fvc.Equals(smallCell, "12/30/99 00:10")
  295. earlyCell.NumFmt = "m/d/yy hh:mm"
  296. fvc.Equals(earlyCell, "1/1/00 02:24")
  297. earlyCell.NumFmt = "m/d/yy h:mm"
  298. fvc.Equals(earlyCell, "1/1/00 02:24")
  299. cell.NumFmt = "mm:ss"
  300. fvc.Equals(cell, "00:00")
  301. smallCell.NumFmt = "mm:ss"
  302. fvc.Equals(smallCell, "10:04")
  303. cell.NumFmt = "[hh]:mm:ss"
  304. fvc.Equals(cell, "18:00:00")
  305. cell.NumFmt = "[h]:mm:ss"
  306. fvc.Equals(cell, "18:00:00")
  307. smallCell.NumFmt = "[h]:mm:ss"
  308. fvc.Equals(smallCell, "10:04")
  309. const (
  310. expect1 = "0000.0086"
  311. expect2 = "1004.8000"
  312. format = "mmss.0000"
  313. tlen = len(format)
  314. )
  315. for i := 0; i < 3; i++ {
  316. tfmt := format[0 : tlen-i]
  317. cell.NumFmt = tfmt
  318. fvc.Equals(cell, expect1[0:tlen-i])
  319. smallCell.NumFmt = tfmt
  320. fvc.Equals(smallCell, expect2[0:tlen-i])
  321. }
  322. cell.NumFmt = "yyyy\\-mm\\-dd"
  323. fvc.Equals(cell, "2003\\-11\\-22")
  324. cell.NumFmt = "dd/mm/yyyy hh:mm:ss"
  325. fvc.Equals(cell, "22/11/2003 18:00:00")
  326. cell.NumFmt = "dd/mm/yy"
  327. fvc.Equals(cell, "22/11/03")
  328. earlyCell.NumFmt = "dd/mm/yy"
  329. fvc.Equals(earlyCell, "01/01/00")
  330. cell.NumFmt = "hh:mm:ss"
  331. fvc.Equals(cell, "18:00:00")
  332. smallCell.NumFmt = "hh:mm:ss"
  333. fvc.Equals(smallCell, "00:10:04")
  334. cell.NumFmt = "dd/mm/yy\\ hh:mm"
  335. fvc.Equals(cell, "22/11/03\\ 18:00")
  336. cell.NumFmt = "yyyy/mm/dd"
  337. fvc.Equals(cell, "2003/11/22")
  338. cell.NumFmt = "yy-mm-dd"
  339. fvc.Equals(cell, "03-11-22")
  340. cell.NumFmt = "d-mmm-yyyy"
  341. fvc.Equals(cell, "22-Nov-2003")
  342. earlyCell.NumFmt = "d-mmm-yyyy"
  343. fvc.Equals(earlyCell, "1-Jan-1900")
  344. cell.NumFmt = "m/d/yy"
  345. fvc.Equals(cell, "11/22/03")
  346. earlyCell.NumFmt = "m/d/yy"
  347. fvc.Equals(earlyCell, "1/1/00")
  348. cell.NumFmt = "m/d/yyyy"
  349. fvc.Equals(cell, "11/22/2003")
  350. earlyCell.NumFmt = "m/d/yyyy"
  351. fvc.Equals(earlyCell, "1/1/1900")
  352. cell.NumFmt = "dd-mmm-yyyy"
  353. fvc.Equals(cell, "22-Nov-2003")
  354. cell.NumFmt = "dd/mm/yyyy"
  355. fvc.Equals(cell, "22/11/2003")
  356. cell.NumFmt = "mm/dd/yy hh:mm am/pm"
  357. fvc.Equals(cell, "11/22/03 06:00 pm")
  358. cell.NumFmt = "mm/dd/yy h:mm am/pm"
  359. fvc.Equals(cell, "11/22/03 6:00 pm")
  360. cell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  361. fvc.Equals(cell, "11/22/2003 18:00:00")
  362. smallCell.NumFmt = "mm/dd/yyyy hh:mm:ss"
  363. fvc.Equals(smallCell, "12/30/1899 00:10:04")
  364. cell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  365. fvc.Equals(cell, "2003-11-22 18:00:00")
  366. smallCell.NumFmt = "yyyy-mm-dd hh:mm:ss"
  367. fvc.Equals(smallCell, "1899-12-30 00:10:04")
  368. cell.NumFmt = "mmmm d, yyyy"
  369. fvc.Equals(cell, "November 22, 2003")
  370. smallCell.NumFmt = "mmmm d, yyyy"
  371. fvc.Equals(smallCell, "December 30, 1899")
  372. cell.NumFmt = "dddd, mmmm dd, yyyy"
  373. fvc.Equals(cell, "Saturday, November 22, 2003")
  374. smallCell.NumFmt = "dddd, mmmm dd, yyyy"
  375. fvc.Equals(smallCell, "Saturday, December 30, 1899")
  376. }
  377. // test setters and getters
  378. func (s *CellSuite) TestSetterGetters(c *C) {
  379. cell := Cell{}
  380. cell.SetString("hello world")
  381. if val, err := cell.FormattedValue(); err != nil {
  382. c.Error(err)
  383. } else {
  384. c.Assert(val, Equals, "hello world")
  385. }
  386. c.Assert(cell.Type(), Equals, CellTypeString)
  387. cell.SetInt(1024)
  388. intValue, _ := cell.Int()
  389. c.Assert(intValue, Equals, 1024)
  390. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  391. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  392. cell.SetInt64(1024)
  393. int64Value, _ := cell.Int64()
  394. c.Assert(int64Value, Equals, int64(1024))
  395. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  396. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  397. cell.SetFloat(1.024)
  398. float, _ := cell.Float()
  399. intValue, _ = cell.Int() // convert
  400. c.Assert(float, Equals, 1.024)
  401. c.Assert(intValue, Equals, 1)
  402. c.Assert(cell.NumFmt, Equals, builtInNumFmt[builtInNumFmtIndex_GENERAL])
  403. c.Assert(cell.Type(), Equals, CellTypeGeneral)
  404. cell.SetFormula("10+20")
  405. c.Assert(cell.Formula(), Equals, "10+20")
  406. c.Assert(cell.Type(), Equals, CellTypeFormula)
  407. }
  408. // TestOddInput is a regression test for #101. When the number format
  409. // was "@" (string), the input below caused a crash in strconv.ParseFloat.
  410. // The solution was to check if cell.Value was both a CellTypeString and
  411. // had a NumFmt of "general" or "@" and short-circuit FormattedValue() if so.
  412. func (s *CellSuite) TestOddInput(c *C) {
  413. cell := Cell{}
  414. odd := `[1],[12,"DATE NOT NULL DEFAULT '0000-00-00'"]`
  415. cell.Value = odd
  416. cell.NumFmt = "@"
  417. if val, err := cell.FormattedValue(); err != nil {
  418. c.Error(err)
  419. } else {
  420. c.Assert(val, Equals, odd)
  421. }
  422. }
  423. // TestBool tests basic Bool getting and setting booleans.
  424. func (s *CellSuite) TestBool(c *C) {
  425. cell := Cell{}
  426. cell.SetBool(true)
  427. c.Assert(cell.Value, Equals, "1")
  428. c.Assert(cell.Bool(), Equals, true)
  429. cell.SetBool(false)
  430. c.Assert(cell.Value, Equals, "0")
  431. c.Assert(cell.Bool(), Equals, false)
  432. }
  433. // TestStringBool tests calling Bool on a non CellTypeBool value.
  434. func (s *CellSuite) TestStringBool(c *C) {
  435. cell := Cell{}
  436. cell.SetInt(0)
  437. c.Assert(cell.Bool(), Equals, false)
  438. cell.SetInt(1)
  439. c.Assert(cell.Bool(), Equals, true)
  440. cell.SetString("")
  441. c.Assert(cell.Bool(), Equals, false)
  442. cell.SetString("0")
  443. c.Assert(cell.Bool(), Equals, true)
  444. }
  445. // TestSetValue tests whether SetValue handle properly for different type values.
  446. func (s *CellSuite) TestSetValue(c *C) {
  447. cell := Cell{}
  448. // int
  449. for _, i := range []interface{}{1, int8(1), int16(1), int32(1), int64(1)} {
  450. cell.SetValue(i)
  451. val, err := cell.Int64()
  452. c.Assert(err, IsNil)
  453. c.Assert(val, Equals, int64(1))
  454. }
  455. // float
  456. for _, i := range []interface{}{1.11, float32(1.11), float64(1.11)} {
  457. cell.SetValue(i)
  458. val, err := cell.Float()
  459. c.Assert(err, IsNil)
  460. c.Assert(val, Equals, 1.11)
  461. }
  462. // time
  463. cell.SetValue(time.Unix(0, 0))
  464. val, err := cell.Float()
  465. c.Assert(err, IsNil)
  466. c.Assert(math.Floor(val), Equals, 25569.0)
  467. // string and nil
  468. for _, i := range []interface{}{nil, "", []byte("")} {
  469. cell.SetValue(i)
  470. c.Assert(cell.Value, Equals, "")
  471. }
  472. // others
  473. cell.SetValue([]string{"test"})
  474. c.Assert(cell.Value, Equals, "[test]")
  475. }
  476. func (s *CellSuite) TestSetDateWithOptions(c *C) {
  477. cell := Cell{}
  478. // time
  479. cell.SetDate(time.Unix(0, 0))
  480. val, err := cell.Float()
  481. c.Assert(err, IsNil)
  482. c.Assert(math.Floor(val), Equals, 25569.0)
  483. // our test subject
  484. date2016UTC := time.Date(2016, 1, 1, 12, 0, 0, 0, time.UTC)
  485. // test ny timezone
  486. nyTZ, err := time.LoadLocation("America/New_York")
  487. c.Assert(err, IsNil)
  488. cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
  489. ExcelTimeFormat: "test_format1",
  490. Location: nyTZ,
  491. })
  492. val, err = cell.Float()
  493. c.Assert(err, IsNil)
  494. c.Assert(val, Equals, TimeToExcelTime(time.Date(2016, 1, 1, 7, 0, 0, 0, time.UTC)))
  495. // test jp timezone
  496. jpTZ, err := time.LoadLocation("Asia/Tokyo")
  497. c.Assert(err, IsNil)
  498. cell.SetDateWithOptions(date2016UTC, DateTimeOptions{
  499. ExcelTimeFormat: "test_format2",
  500. Location: jpTZ,
  501. })
  502. val, err = cell.Float()
  503. c.Assert(err, IsNil)
  504. c.Assert(val, Equals, TimeToExcelTime(time.Date(2016, 1, 1, 21, 0, 0, 0, time.UTC)))
  505. }
  506. func (s *CellSuite) TestIsTimeFormat(c *C) {
  507. c.Assert(isTimeFormat("yy"), Equals, true)
  508. c.Assert(isTimeFormat("hh"), Equals, true)
  509. c.Assert(isTimeFormat("h"), Equals, true)
  510. c.Assert(isTimeFormat("am/pm"), Equals, true)
  511. c.Assert(isTimeFormat("AM/PM"), Equals, true)
  512. c.Assert(isTimeFormat("A/P"), Equals, true)
  513. c.Assert(isTimeFormat("a/p"), Equals, true)
  514. c.Assert(isTimeFormat("ss"), Equals, true)
  515. c.Assert(isTimeFormat("mm"), Equals, true)
  516. c.Assert(isTimeFormat(":"), Equals, true)
  517. c.Assert(isTimeFormat("z"), Equals, false)
  518. }
  519. func (s *CellSuite) TestIs12HourtTime(c *C) {
  520. c.Assert(is12HourTime("am/pm"), Equals, true)
  521. c.Assert(is12HourTime("AM/PM"), Equals, true)
  522. c.Assert(is12HourTime("a/p"), Equals, true)
  523. c.Assert(is12HourTime("A/P"), Equals, true)
  524. c.Assert(is12HourTime("x"), Equals, false)
  525. }