cell_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type CellSuite struct{}
  6. var _ = Suite(&CellSuite{})
  7. // Test that we can set and get a Value from a Cell
  8. func (s *CellSuite) TestValueSet(c *C) {
  9. // Note, this test is fairly pointless, it serves mostly to
  10. // reinforce that this functionality is important, and should
  11. // the mechanics of this all change at some point, to remind
  12. // us not to lose this.
  13. cell := Cell{}
  14. cell.Value = "A string"
  15. c.Assert(cell.Value, Equals, "A string")
  16. }
  17. // Test that GetStyle correctly converts the xlsxStyle.Fonts.
  18. func (s *CellSuite) TestGetStyleWithFonts(c *C) {
  19. font := NewFont(10, "Calibra")
  20. style := *NewStyle()
  21. style.Font = *font
  22. cell := &Cell{Value: "123", style: style}
  23. style = cell.GetStyle()
  24. c.Assert(style, NotNil)
  25. c.Assert(style.Font.Size, Equals, 10)
  26. c.Assert(style.Font.Name, Equals, "Calibra")
  27. }
  28. // Test that SetStyle correctly translates into a xlsxFont element
  29. func (s *CellSuite) TestSetStyleWithFonts(c *C) {
  30. file := NewFile()
  31. sheet := file.AddSheet("Test")
  32. row := sheet.AddRow()
  33. cell := row.AddCell()
  34. font := NewFont(12, "Calibra")
  35. style := *NewStyle()
  36. style.Font = *font
  37. cell.SetStyle(style)
  38. style = cell.GetStyle()
  39. xFont, _, _, _, _ := style.makeXLSXStyleElements()
  40. c.Assert(xFont.Sz.Val, Equals, "12")
  41. c.Assert(xFont.Name.Val, Equals, "Calibra")
  42. }
  43. // Test that GetStyle correctly converts the xlsxStyle.Fills.
  44. func (s *CellSuite) TestGetStyleWithFills(c *C) {
  45. fill := *NewFill("solid", "FF000000", "00FF0000")
  46. style := *NewStyle()
  47. style.Fill = fill
  48. cell := &Cell{Value: "123", style: style}
  49. style = cell.GetStyle()
  50. _, xFill, _, _, _ := style.makeXLSXStyleElements()
  51. c.Assert(xFill.PatternFill.PatternType, Equals, "solid")
  52. c.Assert(xFill.PatternFill.BgColor.RGB, Equals, "00FF0000")
  53. c.Assert(xFill.PatternFill.FgColor.RGB, Equals, "FF000000")
  54. }
  55. // Test that SetStyle correctly updates xlsxStyle.Fills.
  56. func (s *CellSuite) TestSetStyleWithFills(c *C) {
  57. file := NewFile()
  58. sheet := file.AddSheet("Test")
  59. row := sheet.AddRow()
  60. cell := row.AddCell()
  61. fill := NewFill("solid", "00FF0000", "FF000000")
  62. style := *NewStyle()
  63. style.Fill = *fill
  64. cell.SetStyle(style)
  65. style = cell.GetStyle()
  66. _, xFill, _, _, _ := style.makeXLSXStyleElements()
  67. xPatternFill := xFill.PatternFill
  68. c.Assert(xPatternFill.PatternType, Equals, "solid")
  69. c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
  70. c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
  71. }
  72. // Test that GetStyle correctly converts the xlsxStyle.Borders.
  73. func (s *CellSuite) TestGetStyleWithBorders(c *C) {
  74. border := *NewBorder("thin", "thin", "thin", "thin")
  75. style := *NewStyle()
  76. style.Border = border
  77. cell := Cell{Value: "123", style: style}
  78. style = cell.GetStyle()
  79. _, _, xBorder, _, _ := style.makeXLSXStyleElements()
  80. c.Assert(xBorder.Left.Style, Equals, "thin")
  81. c.Assert(xBorder.Right.Style, Equals, "thin")
  82. c.Assert(xBorder.Top.Style, Equals, "thin")
  83. c.Assert(xBorder.Bottom.Style, Equals, "thin")
  84. }
  85. // We can return a string representation of the formatted data
  86. func (l *CellSuite) TestFormattedValue(c *C) {
  87. cell := Cell{Value: "37947.7500001"}
  88. negativeCell := Cell{Value: "-37947.7500001"}
  89. smallCell := Cell{Value: "0.007"}
  90. earlyCell := Cell{Value: "2.1"}
  91. cell.numFmt = "general"
  92. c.Assert(cell.FormattedValue(), Equals, "37947.7500001")
  93. negativeCell.numFmt = "general"
  94. c.Assert(negativeCell.FormattedValue(), Equals, "-37947.7500001")
  95. cell.numFmt = "0"
  96. c.Assert(cell.FormattedValue(), Equals, "37947")
  97. cell.numFmt = "#,##0" // For the time being we're not doing
  98. // this comma formatting, so it'll fall back to the related
  99. // non-comma form.
  100. c.Assert(cell.FormattedValue(), Equals, "37947")
  101. cell.numFmt = "0.00"
  102. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  103. cell.numFmt = "#,##0.00" // For the time being we're not doing
  104. // this comma formatting, so it'll fall back to the related
  105. // non-comma form.
  106. c.Assert(cell.FormattedValue(), Equals, "37947.75")
  107. cell.numFmt = "#,##0 ;(#,##0)"
  108. c.Assert(cell.FormattedValue(), Equals, "37947")
  109. negativeCell.numFmt = "#,##0 ;(#,##0)"
  110. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  111. cell.numFmt = "#,##0 ;[red](#,##0)"
  112. c.Assert(cell.FormattedValue(), Equals, "37947")
  113. negativeCell.numFmt = "#,##0 ;[red](#,##0)"
  114. c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
  115. cell.numFmt = "0%"
  116. c.Assert(cell.FormattedValue(), Equals, "3794775%")
  117. cell.numFmt = "0.00%"
  118. c.Assert(cell.FormattedValue(), Equals, "3794775.00%")
  119. cell.numFmt = "0.00e+00"
  120. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  121. cell.numFmt = "##0.0e+0" // This is wrong, but we'll use it for now.
  122. c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
  123. cell.numFmt = "mm-dd-yy"
  124. c.Assert(cell.FormattedValue(), Equals, "11-22-03")
  125. cell.numFmt = "d-mmm-yy"
  126. c.Assert(cell.FormattedValue(), Equals, "22-Nov-03")
  127. earlyCell.numFmt = "d-mmm-yy"
  128. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-00")
  129. cell.numFmt = "d-mmm"
  130. c.Assert(cell.FormattedValue(), Equals, "22-Nov")
  131. earlyCell.numFmt = "d-mmm"
  132. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan")
  133. cell.numFmt = "mmm-yy"
  134. c.Assert(cell.FormattedValue(), Equals, "Nov-03")
  135. cell.numFmt = "h:mm am/pm"
  136. c.Assert(cell.FormattedValue(), Equals, "6:00 pm")
  137. smallCell.numFmt = "h:mm am/pm"
  138. c.Assert(smallCell.FormattedValue(), Equals, "12:14 am")
  139. cell.numFmt = "h:mm:ss am/pm"
  140. c.Assert(cell.FormattedValue(), Equals, "6:00:00 pm")
  141. smallCell.numFmt = "h:mm:ss am/pm"
  142. c.Assert(smallCell.FormattedValue(), Equals, "12:14:47 am")
  143. cell.numFmt = "h:mm"
  144. c.Assert(cell.FormattedValue(), Equals, "18:00")
  145. smallCell.numFmt = "h:mm"
  146. c.Assert(smallCell.FormattedValue(), Equals, "00:14")
  147. cell.numFmt = "h:mm:ss"
  148. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  149. // This is wrong, but there's no eary way aroud it in Go right now, AFAICT.
  150. smallCell.numFmt = "h:mm:ss"
  151. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  152. cell.numFmt = "m/d/yy h:mm"
  153. c.Assert(cell.FormattedValue(), Equals, "11/22/03 18:00")
  154. smallCell.numFmt = "m/d/yy h:mm"
  155. c.Assert(smallCell.FormattedValue(), Equals, "12/30/99 00:14") // Note, that's 1899
  156. earlyCell.numFmt = "m/d/yy h:mm"
  157. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00 02:24") // and 1900
  158. cell.numFmt = "mm:ss"
  159. c.Assert(cell.FormattedValue(), Equals, "00:00")
  160. smallCell.numFmt = "mm:ss"
  161. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  162. cell.numFmt = "[h]:mm:ss"
  163. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  164. smallCell.numFmt = "[h]:mm:ss"
  165. c.Assert(smallCell.FormattedValue(), Equals, "14:47")
  166. cell.numFmt = "mmss.0" // I'm not sure about these.
  167. c.Assert(cell.FormattedValue(), Equals, "00.8640")
  168. smallCell.numFmt = "mmss.0"
  169. c.Assert(smallCell.FormattedValue(), Equals, "1447.999997")
  170. cell.numFmt = "yyyy\\-mm\\-dd"
  171. c.Assert(cell.FormattedValue(), Equals, "2003\\-11\\-22")
  172. cell.numFmt = "dd/mm/yy"
  173. c.Assert(cell.FormattedValue(), Equals, "22/11/03")
  174. earlyCell.numFmt = "dd/mm/yy"
  175. c.Assert(earlyCell.FormattedValue(), Equals, "01/01/00")
  176. cell.numFmt = "hh:mm:ss"
  177. c.Assert(cell.FormattedValue(), Equals, "18:00:00")
  178. smallCell.numFmt = "hh:mm:ss"
  179. c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
  180. cell.numFmt = "dd/mm/yy\\ hh:mm"
  181. c.Assert(cell.FormattedValue(), Equals, "22/11/03\\ 18:00")
  182. cell.numFmt = "yy-mm-dd"
  183. c.Assert(cell.FormattedValue(), Equals, "03-11-22")
  184. cell.numFmt = "d-mmm-yyyy"
  185. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  186. earlyCell.numFmt = "d-mmm-yyyy"
  187. c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-1900")
  188. cell.numFmt = "m/d/yy"
  189. c.Assert(cell.FormattedValue(), Equals, "11/22/03")
  190. earlyCell.numFmt = "m/d/yy"
  191. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00")
  192. cell.numFmt = "m/d/yyyy"
  193. c.Assert(cell.FormattedValue(), Equals, "11/22/2003")
  194. earlyCell.numFmt = "m/d/yyyy"
  195. c.Assert(earlyCell.FormattedValue(), Equals, "1/1/1900")
  196. cell.numFmt = "dd-mmm-yyyy"
  197. c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
  198. cell.numFmt = "dd/mm/yyyy"
  199. c.Assert(cell.FormattedValue(), Equals, "22/11/2003")
  200. cell.numFmt = "mm/dd/yy hh:mm am/pm"
  201. c.Assert(cell.FormattedValue(), Equals, "11/22/03 06:00 pm")
  202. cell.numFmt = "mm/dd/yyyy hh:mm:ss"
  203. c.Assert(cell.FormattedValue(), Equals, "11/22/2003 18:00:00")
  204. smallCell.numFmt = "mm/dd/yyyy hh:mm:ss"
  205. c.Assert(smallCell.FormattedValue(), Equals, "12/30/1899 00:14:47")
  206. cell.numFmt = "yyyy-mm-dd hh:mm:ss"
  207. c.Assert(cell.FormattedValue(), Equals, "2003-11-22 18:00:00")
  208. smallCell.numFmt = "yyyy-mm-dd hh:mm:ss"
  209. c.Assert(smallCell.FormattedValue(), Equals, "1899-12-30 00:14:47")
  210. }