cell_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. formattedValueOutput string
  115. noExpValueOutput string
  116. }{
  117. {
  118. value: "18.989999999999998",
  119. formattedValueOutput: "18.99",
  120. noExpValueOutput: "18.99",
  121. },
  122. {
  123. value: "1.1000000000000001",
  124. formattedValueOutput: "1.1",
  125. noExpValueOutput: "1.1",
  126. },
  127. {
  128. value: "0.0000000000000001",
  129. formattedValueOutput: "1E-16",
  130. noExpValueOutput: "0.0000000000000001",
  131. },
  132. {
  133. value: "0.000000000000008",
  134. formattedValueOutput: "8E-15",
  135. noExpValueOutput: "0.000000000000008",
  136. },
  137. {
  138. value: "1000000000000000000",
  139. formattedValueOutput: "1E+18",
  140. noExpValueOutput: "1000000000000000000",
  141. },
  142. {
  143. value: "1230000000000000000",
  144. formattedValueOutput: "1.23E+18",
  145. noExpValueOutput: "1230000000000000000",
  146. },
  147. {
  148. value: "12345678",
  149. formattedValueOutput: "12345678",
  150. noExpValueOutput: "12345678",
  151. },
  152. {
  153. value: "0",
  154. formattedValueOutput: "0",
  155. noExpValueOutput: "0",
  156. },
  157. {
  158. value: "-18.989999999999998",
  159. formattedValueOutput: "-18.99",
  160. noExpValueOutput: "-18.99",
  161. },
  162. {
  163. value: "-1.1000000000000001",
  164. formattedValueOutput: "-1.1",
  165. noExpValueOutput: "-1.1",
  166. },
  167. {
  168. value: "-0.0000000000000001",
  169. formattedValueOutput: "-1E-16",
  170. noExpValueOutput: "-0.0000000000000001",
  171. },
  172. {
  173. value: "-0.000000000000008",
  174. formattedValueOutput: "-8E-15",
  175. noExpValueOutput: "-0.000000000000008",
  176. },
  177. {
  178. value: "-1000000000000000000",
  179. formattedValueOutput: "-1E+18",
  180. noExpValueOutput: "-1000000000000000000",
  181. },
  182. {
  183. value: "-1230000000000000000",
  184. formattedValueOutput: "-1.23E+18",
  185. noExpValueOutput: "-1230000000000000000",
  186. },
  187. {
  188. value: "-12345678",
  189. formattedValueOutput: "-12345678",
  190. noExpValueOutput: "-12345678",
  191. },
  192. }
  193. for _, testCase := range testCases {
  194. cell := Cell{
  195. cellType: CellTypeNumeric,
  196. NumFmt: builtInNumFmt[builtInNumFmtIndex_GENERAL],
  197. Value: testCase.value,
  198. }
  199. val, err := cell.FormattedValue()
  200. if err != nil {
  201. c.Fatal(err)
  202. }
  203. c.Assert(val, Equals, testCase.formattedValueOutput)
  204. val, err = cell.GeneralNumericWithoutScientific()
  205. if err != nil {
  206. c.Fatal(err)
  207. }
  208. c.Assert(val, Equals, testCase.noExpValueOutput)
  209. }
  210. }
  211. func (s *CellSuite) TestGetTime(c *C) {
  212. cell := Cell{}
  213. cell.SetFloat(0)
  214. date, err := cell.GetTime(false)
  215. c.Assert(err, Equals, nil)
  216. c.Assert(date, Equals, time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC))
  217. cell.SetFloat(39813.0)
  218. date, err = cell.GetTime(true)
  219. c.Assert(err, Equals, nil)
  220. c.Assert(date, Equals, time.Date(2013, 1, 1, 0, 0, 0, 0, time.UTC))
  221. cell.Value = "d"
  222. _, err = cell.GetTime(false)
  223. c.Assert(err, NotNil)
  224. }
  225. // FormattedValue returns an error for formatting errors
  226. func (l *CellSuite) TestFormattedValueErrorsOnBadFormat(c *C) {
  227. cell := Cell{Value: "Fudge Cake"}
  228. cell.NumFmt = "#,##0 ;(#,##0)"
  229. value, err := cell.FormattedValue()
  230. c.Assert(value, Equals, "Fudge Cake")
  231. c.Assert(err, NotNil)
  232. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  233. }
  234. // FormattedValue returns a string containing error text for formatting errors
  235. func (l *CellSuite) TestFormattedValueReturnsErrorAsValueForBadFormat(c *C) {
  236. cell := Cell{Value: "Fudge Cake"}
  237. cell.NumFmt = "#,##0 ;(#,##0)"
  238. _, err := cell.FormattedValue()
  239. c.Assert(err.Error(), Equals, "strconv.ParseFloat: parsing \"Fudge Cake\": invalid syntax")
  240. }
  241. // formattedValueChecker removes all the boilerplate for testing Cell.FormattedValue
  242. // after its change from returning one value (a string) to two values (string, error)
  243. // This allows all the old one-line asserts in the test to continue to be one
  244. // line, instead of multi-line with error checking.
  245. type formattedValueChecker struct {
  246. c *C
  247. }
  248. func (fvc *formattedValueChecker) Equals(cell Cell, expected string) {
  249. val, err := cell.FormattedValue()
  250. if err != nil {
  251. fvc.c.Error(err)
  252. }
  253. fvc.c.Assert(val, Equals, expected)
  254. }
  255. // We can return a string representation of the formatted data
  256. func (l *CellSuite) TestFormattedValue(c *C) {
  257. // XXX TODO, this test should probably be split down, and made
  258. // in terms of SafeFormattedValue, as FormattedValue wraps
  259. // that function now.
  260. cell := Cell{Value: "37947.7500001"}
  261. negativeCell := Cell{Value: "-37947.7500001"}
  262. smallCell := Cell{Value: "0.007"}
  263. earlyCell := Cell{Value: "2.1"}
  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, "37947")
  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, "37947")
  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, "37947")
  288. negativeCell.NumFmt = "#,##0 ;(#,##0)"
  289. fvc.Equals(negativeCell, "(37947)")
  290. cell.NumFmt = "#,##0 ;[red](#,##0)"
  291. fvc.Equals(cell, "37947")
  292. negativeCell.NumFmt = "#,##0 ;[red](#,##0)"
  293. fvc.Equals(negativeCell, "(37947)")
  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, true)
  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. }