sheet_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. . "gopkg.in/check.v1"
  6. )
  7. type SheetSuite struct{}
  8. var _ = Suite(&SheetSuite{})
  9. // Test we can add a Row to a Sheet
  10. func (s *SheetSuite) TestAddRow(c *C) {
  11. var f *File
  12. f = NewFile()
  13. sheet := f.AddSheet("MySheet")
  14. row := sheet.AddRow()
  15. c.Assert(row, NotNil)
  16. c.Assert(len(sheet.Rows), Equals, 1)
  17. }
  18. func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
  19. file := NewFile()
  20. sheet := file.AddSheet("Sheet1")
  21. row := sheet.AddRow()
  22. cell := row.AddCell()
  23. cell.cellType = CellTypeString
  24. cell.Value = "A cell!"
  25. refTable := NewSharedStringRefTable()
  26. styles := newXlsxStyleSheet(nil)
  27. xSheet := sheet.makeXLSXSheet(refTable, styles)
  28. c.Assert(xSheet.Dimension.Ref, Equals, "A1")
  29. c.Assert(xSheet.SheetData.Row, HasLen, 1)
  30. xRow := xSheet.SheetData.Row[0]
  31. c.Assert(xRow.R, Equals, 1)
  32. c.Assert(xRow.Spans, Equals, "")
  33. c.Assert(xRow.C, HasLen, 1)
  34. xC := xRow.C[0]
  35. c.Assert(xC.R, Equals, "A1")
  36. c.Assert(xC.S, Equals, 0)
  37. c.Assert(xC.T, Equals, "s") // Shared string type
  38. c.Assert(xC.V, Equals, "0") // reference to shared string
  39. xSST := refTable.makeXLSXSST()
  40. c.Assert(xSST.Count, Equals, 1)
  41. c.Assert(xSST.UniqueCount, Equals, 1)
  42. c.Assert(xSST.SI, HasLen, 1)
  43. xSI := xSST.SI[0]
  44. c.Assert(xSI.T, Equals, "A cell!")
  45. }
  46. // Test if the NumFmts assigned properly according the FormatCode in cell.
  47. func (s *SheetSuite) TestMakeXLSXSheetWithNumFormats(c *C) {
  48. file := NewFile()
  49. sheet := file.AddSheet("Sheet1")
  50. row := sheet.AddRow()
  51. cell1 := row.AddCell()
  52. cell1.Value = "A cell!"
  53. cell1.numFmt = "general"
  54. cell2 := row.AddCell()
  55. cell2.Value = "37947.7500001"
  56. cell2.numFmt = "0"
  57. cell3 := row.AddCell()
  58. cell3.Value = "37947.7500001"
  59. cell3.numFmt = "mm-dd-yy"
  60. cell4 := row.AddCell()
  61. cell4.Value = "37947.7500001"
  62. cell4.numFmt = "hh:mm:ss"
  63. refTable := NewSharedStringRefTable()
  64. styles := newXlsxStyleSheet(nil)
  65. worksheet := sheet.makeXLSXSheet(refTable, styles)
  66. c.Assert(styles.CellStyleXfs.Count, Equals, 1)
  67. // The 0th CellStyleXf could just be getting the zero values by default
  68. c.Assert(styles.CellStyleXfs.Xf[0].FontId, Equals, 0)
  69. c.Assert(styles.CellStyleXfs.Xf[0].FillId, Equals, 0)
  70. c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
  71. c.Assert(styles.CellXfs.Count, Equals, 4)
  72. c.Assert(styles.CellXfs.Xf[0].NumFmtId, Equals, 0)
  73. c.Assert(styles.CellXfs.Xf[1].NumFmtId, Equals, 1)
  74. c.Assert(styles.CellXfs.Xf[2].NumFmtId, Equals, 14)
  75. c.Assert(styles.CellXfs.Xf[3].NumFmtId, Equals, 164)
  76. c.Assert(styles.NumFmts.Count, Equals, 1)
  77. c.Assert(styles.NumFmts.NumFmt[0].NumFmtId, Equals, 164)
  78. c.Assert(styles.NumFmts.NumFmt[0].FormatCode, Equals, "hh:mm:ss")
  79. // Finally we check that the cell points to the right CellXf /
  80. // CellStyleXf.
  81. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
  82. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  83. }
  84. // When we create the xlsxSheet we also populate the xlsxStyles struct
  85. // with style information.
  86. func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
  87. file := NewFile()
  88. sheet := file.AddSheet("Sheet1")
  89. row := sheet.AddRow()
  90. cell1 := row.AddCell()
  91. cell1.Value = "A cell!"
  92. style1 := NewStyle()
  93. style1.Font = *NewFont(10, "Verdana")
  94. style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  95. style1.Border = *NewBorder("none", "thin", "none", "thin")
  96. cell1.SetStyle(style1)
  97. // We need a second style to check that Xfs are populated correctly.
  98. cell2 := row.AddCell()
  99. cell2.Value = "Another cell!"
  100. style2 := NewStyle()
  101. style2.Font = *NewFont(10, "Verdana")
  102. style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  103. style2.Border = *NewBorder("none", "thin", "none", "thin")
  104. cell2.SetStyle(style2)
  105. refTable := NewSharedStringRefTable()
  106. styles := newXlsxStyleSheet(nil)
  107. worksheet := sheet.makeXLSXSheet(refTable, styles)
  108. c.Assert(styles.Fonts.Count, Equals, 2)
  109. c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "12")
  110. c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
  111. c.Assert(styles.Fonts.Font[1].Sz.Val, Equals, "10")
  112. c.Assert(styles.Fonts.Font[1].Name.Val, Equals, "Verdana")
  113. c.Assert(styles.Fills.Count, Equals, 3)
  114. c.Assert(styles.Fills.Fill[0].PatternFill.PatternType, Equals, "none")
  115. c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
  116. c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "00000000")
  117. c.Assert(styles.Borders.Count, Equals, 2)
  118. c.Assert(styles.Borders.Border[1].Left.Style, Equals, "none")
  119. c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
  120. c.Assert(styles.Borders.Border[1].Top.Style, Equals, "none")
  121. c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
  122. c.Assert(styles.CellStyleXfs.Count, Equals, 2)
  123. // The 0th CellStyleXf could just be getting the zero values by default
  124. c.Assert(styles.CellStyleXfs.Xf[0].FontId, Equals, 0)
  125. c.Assert(styles.CellStyleXfs.Xf[0].FillId, Equals, 0)
  126. c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
  127. c.Assert(styles.CellXfs.Count, Equals, 2)
  128. c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
  129. c.Assert(styles.CellXfs.Xf[0].FillId, Equals, 0)
  130. c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
  131. // Finally we check that the cell points to the right CellXf /
  132. // CellStyleXf.
  133. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
  134. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  135. }
  136. // If the column width is not customised, the xslxCol.CustomWidth field is set to 0.
  137. func (s *SheetSuite) TestMakeXLSXSheetDefaultsCustomColWidth(c *C) {
  138. file := NewFile()
  139. sheet := file.AddSheet("Sheet1")
  140. row := sheet.AddRow()
  141. cell1 := row.AddCell()
  142. cell1.Value = "A cell!"
  143. refTable := NewSharedStringRefTable()
  144. styles := newXlsxStyleSheet(nil)
  145. worksheet := sheet.makeXLSXSheet(refTable, styles)
  146. c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, 0)
  147. }
  148. // If the column width is customised, the xslxCol.CustomWidth field is set to 1.
  149. func (s *SheetSuite) TestMakeXLSXSheetSetsCustomColWidth(c *C) {
  150. file := NewFile()
  151. sheet := file.AddSheet("Sheet1")
  152. err := sheet.SetColWidth(0, 0, 10.5)
  153. c.Assert(err, IsNil)
  154. refTable := NewSharedStringRefTable()
  155. styles := newXlsxStyleSheet(nil)
  156. worksheet := sheet.makeXLSXSheet(refTable, styles)
  157. c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, 1)
  158. }
  159. func (s *SheetSuite) TestMarshalSheet(c *C) {
  160. file := NewFile()
  161. sheet := file.AddSheet("Sheet1")
  162. row := sheet.AddRow()
  163. cell := row.AddCell()
  164. cell.Value = "A cell!"
  165. cell.cellType = CellTypeString
  166. refTable := NewSharedStringRefTable()
  167. styles := newXlsxStyleSheet(nil)
  168. xSheet := sheet.makeXLSXSheet(refTable, styles)
  169. output := bytes.NewBufferString(xml.Header)
  170. body, err := xml.Marshal(xSheet)
  171. c.Assert(err, IsNil)
  172. c.Assert(body, NotNil)
  173. _, err = output.Write(body)
  174. c.Assert(err, IsNil)
  175. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  176. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetPr filterMode="false"><pageSetUpPr fitToPage="false"></pageSetUpPr></sheetPr><dimension ref="A1"></dimension><sheetViews><sheetView windowProtection="false" showFormulas="false" showGridLines="true" showRowColHeaders="true" showZeros="true" rightToLeft="false" tabSelected="true" showOutlineSymbols="true" defaultGridColor="true" view="normal" topLeftCell="A1" colorId="64" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100" workbookViewId="0"><selection pane="topLeft" activeCell="A1" activeCellId="0" sqref="A1"></selection></sheetView></sheetViews><sheetFormatPr defaultRowHeight="12.85"></sheetFormatPr><cols><col collapsed="false" hidden="false" max="1" min="1" style="0" width="9.5"></col></cols><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c></row></sheetData><printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"></printOptions><pageMargins left="0.7875" right="0.7875" top="1.05277777777778" bottom="1.05277777777778" header="0.7875" footer="0.7875"></pageMargins><pageSetup paperSize="9" scale="100" firstPageNumber="1" fitToWidth="1" fitToHeight="1" pageOrder="downThenOver" orientation="portrait" usePrinterDefaults="false" blackAndWhite="false" draft="false" cellComments="none" useFirstPageNumber="true" horizontalDpi="300" verticalDpi="300" copies="1"></pageSetup><headerFooter differentFirst="false" differentOddEven="false"><oddHeader>&amp;C&amp;&#34;Times New Roman,Regular&#34;&amp;12&amp;A</oddHeader><oddFooter>&amp;C&amp;&#34;Times New Roman,Regular&#34;&amp;12Page &amp;P</oddFooter></headerFooter></worksheet>`
  177. c.Assert(output.String(), Equals, expectedXLSXSheet)
  178. }
  179. func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
  180. file := NewFile()
  181. sheet := file.AddSheet("Sheet1")
  182. row := sheet.AddRow()
  183. cell := row.AddCell()
  184. cell.cellType = CellTypeString
  185. cell.Value = "A cell (with value 1)!"
  186. cell = row.AddCell()
  187. cell.cellType = CellTypeString
  188. cell.Value = "A cell (with value 2)!"
  189. refTable := NewSharedStringRefTable()
  190. styles := newXlsxStyleSheet(nil)
  191. xSheet := sheet.makeXLSXSheet(refTable, styles)
  192. output := bytes.NewBufferString(xml.Header)
  193. body, err := xml.Marshal(xSheet)
  194. c.Assert(err, IsNil)
  195. c.Assert(body, NotNil)
  196. _, err = output.Write(body)
  197. c.Assert(err, IsNil)
  198. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  199. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetPr filterMode="false"><pageSetUpPr fitToPage="false"></pageSetUpPr></sheetPr><dimension ref="A1:B1"></dimension><sheetViews><sheetView windowProtection="false" showFormulas="false" showGridLines="true" showRowColHeaders="true" showZeros="true" rightToLeft="false" tabSelected="true" showOutlineSymbols="true" defaultGridColor="true" view="normal" topLeftCell="A1" colorId="64" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100" workbookViewId="0"><selection pane="topLeft" activeCell="A1" activeCellId="0" sqref="A1"></selection></sheetView></sheetViews><sheetFormatPr defaultRowHeight="12.85"></sheetFormatPr><cols><col collapsed="false" hidden="false" max="1" min="1" style="0" width="9.5"></col><col collapsed="false" hidden="false" max="2" min="2" style="0" width="9.5"></col></cols><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c></row></sheetData><printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"></printOptions><pageMargins left="0.7875" right="0.7875" top="1.05277777777778" bottom="1.05277777777778" header="0.7875" footer="0.7875"></pageMargins><pageSetup paperSize="9" scale="100" firstPageNumber="1" fitToWidth="1" fitToHeight="1" pageOrder="downThenOver" orientation="portrait" usePrinterDefaults="false" blackAndWhite="false" draft="false" cellComments="none" useFirstPageNumber="true" horizontalDpi="300" verticalDpi="300" copies="1"></pageSetup><headerFooter differentFirst="false" differentOddEven="false"><oddHeader>&amp;C&amp;&#34;Times New Roman,Regular&#34;&amp;12&amp;A</oddHeader><oddFooter>&amp;C&amp;&#34;Times New Roman,Regular&#34;&amp;12Page &amp;P</oddFooter></headerFooter></worksheet>`
  200. c.Assert(output.String(), Equals, expectedXLSXSheet)
  201. }
  202. func (s *SheetSuite) TestSetColWidth(c *C) {
  203. file := NewFile()
  204. sheet := file.AddSheet("Sheet1")
  205. _ = sheet.SetColWidth(0, 0, 10.5)
  206. _ = sheet.SetColWidth(1, 5, 11)
  207. c.Assert(sheet.Cols[0].Width, Equals, 10.5)
  208. c.Assert(sheet.Cols[0].Max, Equals, 1)
  209. c.Assert(sheet.Cols[0].Min, Equals, 1)
  210. c.Assert(sheet.Cols[1].Width, Equals, float64(11))
  211. c.Assert(sheet.Cols[1].Max, Equals, 6)
  212. c.Assert(sheet.Cols[1].Min, Equals, 2)
  213. }
  214. func (s *SheetSuite) TestSetRowHeightCM(c *C) {
  215. file := NewFile()
  216. sheet := file.AddSheet("Sheet1")
  217. row := sheet.AddRow()
  218. row.SetHeightCM(1.5)
  219. c.Assert(row.Height, Equals, 42.51968505)
  220. }
  221. func (s *SheetSuite) TestAlignment(c *C) {
  222. leftalign := Alignment{Horizontal: "left"}
  223. centerHalign := Alignment{Horizontal: "center"}
  224. rightalign := Alignment{Horizontal: "right"}
  225. file := NewFile()
  226. sheet := file.AddSheet("Sheet1")
  227. style := NewStyle()
  228. hrow := sheet.AddRow()
  229. // Horizontals
  230. cell := hrow.AddCell()
  231. cell.Value = "left"
  232. style.Alignment = leftalign
  233. style.ApplyAlignment = true
  234. cell.SetStyle(style)
  235. style = NewStyle()
  236. cell = hrow.AddCell()
  237. cell.Value = "centerH"
  238. style.Alignment = centerHalign
  239. style.ApplyAlignment = true
  240. cell.SetStyle(style)
  241. style = NewStyle()
  242. cell = hrow.AddCell()
  243. cell.Value = "right"
  244. style.Alignment = rightalign
  245. style.ApplyAlignment = true
  246. cell.SetStyle(style)
  247. // Verticals
  248. topalign := Alignment{Vertical: "top"}
  249. centerValign := Alignment{Vertical: "center"}
  250. bottomalign := Alignment{Vertical: "bottom"}
  251. style = NewStyle()
  252. vrow := sheet.AddRow()
  253. cell = vrow.AddCell()
  254. cell.Value = "top"
  255. style.Alignment = topalign
  256. style.ApplyAlignment = true
  257. cell.SetStyle(style)
  258. style = NewStyle()
  259. cell = vrow.AddCell()
  260. cell.Value = "centerV"
  261. style.Alignment = centerValign
  262. style.ApplyAlignment = true
  263. cell.SetStyle(style)
  264. style = NewStyle()
  265. cell = vrow.AddCell()
  266. cell.Value = "bottom"
  267. style.Alignment = bottomalign
  268. style.ApplyAlignment = true
  269. cell.SetStyle(style)
  270. parts, err := file.MarshallParts()
  271. c.Assert(err, IsNil)
  272. stylepart := parts["xl/styles.xml"]
  273. shouldbe := `<?xml version="1.0" encoding="UTF-8"?>
  274. <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><fills count="2"><fill><patternFill patternType="none"><fgColor rgb="FFFFFFFF"/><bgColor rgb="00000000"/></patternFill></fill><fill><patternFill patternType="lightGray"/></fill></fills><borders count="1"><border><left style="none"></left><right style="none"></right><top style="none"></top><bottom style="none"></bottom></border></borders><cellStyleXfs count="7"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="center" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="right" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="top" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="center" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellStyleXfs><cellXfs count="7"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="center" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="right" indent="0" shrinkToFit="0" textRotation="0" vertical="" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="top" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="center" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellXfs></styleSheet>`
  275. output := bytes.NewBufferString(shouldbe)
  276. c.Assert(output.String(), Equals, stylepart)
  277. }