sheet_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.Value = "A cell!"
  24. refTable := NewSharedStringRefTable()
  25. styles := newXlsxStyleSheet(nil)
  26. xSheet := sheet.makeXLSXSheet(refTable, styles)
  27. c.Assert(xSheet.Dimension.Ref, Equals, "A1")
  28. c.Assert(xSheet.SheetData.Row, HasLen, 1)
  29. xRow := xSheet.SheetData.Row[0]
  30. c.Assert(xRow.R, Equals, 1)
  31. c.Assert(xRow.Spans, Equals, "")
  32. c.Assert(xRow.C, HasLen, 1)
  33. xC := xRow.C[0]
  34. c.Assert(xC.R, Equals, "A1")
  35. c.Assert(xC.S, Equals, 0)
  36. c.Assert(xC.T, Equals, "s") // Shared string type
  37. c.Assert(xC.V, Equals, "0") // reference to shared string
  38. xSST := refTable.makeXLSXSST()
  39. c.Assert(xSST.Count, Equals, 1)
  40. c.Assert(xSST.UniqueCount, Equals, 1)
  41. c.Assert(xSST.SI, HasLen, 1)
  42. xSI := xSST.SI[0]
  43. c.Assert(xSI.T, Equals, "A cell!")
  44. }
  45. // Test if the NumFmts assigned properly according the FormatCode in cell.
  46. func (s *SheetSuite) TestMakeXLSXSheetWithNumFormats(c *C) {
  47. file := NewFile()
  48. sheet := file.AddSheet("Sheet1")
  49. row := sheet.AddRow()
  50. cell1 := row.AddCell()
  51. cell1.Value = "A cell!"
  52. cell1.numFmt = "general"
  53. cell2 := row.AddCell()
  54. cell2.Value = "37947.7500001"
  55. cell2.numFmt = "0"
  56. cell3 := row.AddCell()
  57. cell3.Value = "37947.7500001"
  58. cell3.numFmt = "mm-dd-yy"
  59. cell4 := row.AddCell()
  60. cell4.Value = "37947.7500001"
  61. cell4.numFmt = "hh:mm:ss"
  62. refTable := NewSharedStringRefTable()
  63. styles := newXlsxStyleSheet(nil)
  64. worksheet := sheet.makeXLSXSheet(refTable, styles)
  65. c.Assert(styles.CellStyleXfs.Count, Equals, 1)
  66. // The 0th CellStyleXf could just be getting the zero values by default
  67. c.Assert(styles.CellStyleXfs.Xf[0].FontId, Equals, 0)
  68. c.Assert(styles.CellStyleXfs.Xf[0].FillId, Equals, 0)
  69. c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
  70. c.Assert(styles.CellXfs.Count, Equals, 4)
  71. c.Assert(styles.CellXfs.Xf[0].NumFmtId, Equals, 0)
  72. c.Assert(styles.CellXfs.Xf[1].NumFmtId, Equals, 1)
  73. c.Assert(styles.CellXfs.Xf[2].NumFmtId, Equals, 14)
  74. c.Assert(styles.CellXfs.Xf[3].NumFmtId, Equals, 164)
  75. c.Assert(styles.NumFmts.Count, Equals, 1)
  76. c.Assert(styles.NumFmts.NumFmt[0].NumFmtId, Equals, 164)
  77. c.Assert(styles.NumFmts.NumFmt[0].FormatCode, Equals, "hh:mm:ss")
  78. // Finally we check that the cell points to the right CellXf /
  79. // CellStyleXf.
  80. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
  81. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  82. }
  83. // When we create the xlsxSheet we also populate the xlsxStyles struct
  84. // with style information.
  85. func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
  86. file := NewFile()
  87. sheet := file.AddSheet("Sheet1")
  88. row := sheet.AddRow()
  89. cell1 := row.AddCell()
  90. cell1.Value = "A cell!"
  91. style1 := NewStyle()
  92. style1.Font = *NewFont(10, "Verdana")
  93. style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  94. style1.Border = *NewBorder("none", "thin", "none", "thin")
  95. cell1.SetStyle(style1)
  96. // We need a second style to check that Xfs are populated correctly.
  97. cell2 := row.AddCell()
  98. cell2.Value = "Another cell!"
  99. style2 := NewStyle()
  100. style2.Font = *NewFont(10, "Verdana")
  101. style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  102. style2.Border = *NewBorder("none", "thin", "none", "thin")
  103. cell2.SetStyle(style2)
  104. refTable := NewSharedStringRefTable()
  105. styles := newXlsxStyleSheet(nil)
  106. worksheet := sheet.makeXLSXSheet(refTable, styles)
  107. c.Assert(styles.Fonts.Count, Equals, 1)
  108. c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "10")
  109. c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
  110. c.Assert(styles.Fills.Count, Equals, 2)
  111. c.Assert(styles.Fills.Fill[0].PatternFill.PatternType, Equals, "solid")
  112. c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
  113. c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "00000000")
  114. c.Assert(styles.Borders.Count, Equals, 1)
  115. c.Assert(styles.Borders.Border[0].Left.Style, Equals, "none")
  116. c.Assert(styles.Borders.Border[0].Right.Style, Equals, "thin")
  117. c.Assert(styles.Borders.Border[0].Top.Style, Equals, "none")
  118. c.Assert(styles.Borders.Border[0].Bottom.Style, Equals, "thin")
  119. c.Assert(styles.CellStyleXfs.Count, Equals, 1)
  120. // The 0th CellStyleXf could just be getting the zero values by default
  121. c.Assert(styles.CellStyleXfs.Xf[0].FontId, Equals, 0)
  122. c.Assert(styles.CellStyleXfs.Xf[0].FillId, Equals, 0)
  123. c.Assert(styles.CellStyleXfs.Xf[0].BorderId, Equals, 0)
  124. c.Assert(styles.CellXfs.Count, Equals, 1)
  125. c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
  126. c.Assert(styles.CellXfs.Xf[0].FillId, Equals, 0)
  127. c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
  128. // Finally we check that the cell points to the right CellXf /
  129. // CellStyleXf.
  130. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
  131. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 0)
  132. }
  133. func (s *SheetSuite) TestMarshalSheet(c *C) {
  134. file := NewFile()
  135. sheet := file.AddSheet("Sheet1")
  136. row := sheet.AddRow()
  137. cell := row.AddCell()
  138. cell.Value = "A cell!"
  139. refTable := NewSharedStringRefTable()
  140. styles := newXlsxStyleSheet(nil)
  141. xSheet := sheet.makeXLSXSheet(refTable, styles)
  142. output := bytes.NewBufferString(xml.Header)
  143. body, err := xml.Marshal(xSheet)
  144. c.Assert(err, IsNil)
  145. c.Assert(body, NotNil)
  146. _, err = output.Write(body)
  147. c.Assert(err, IsNil)
  148. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  149. <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" 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>`
  150. c.Assert(output.String(), Equals, expectedXLSXSheet)
  151. }
  152. func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
  153. file := NewFile()
  154. sheet := file.AddSheet("Sheet1")
  155. row := sheet.AddRow()
  156. cell := row.AddCell()
  157. cell.Value = "A cell (with value 1)!"
  158. cell = row.AddCell()
  159. cell.Value = "A cell (with value 2)!"
  160. refTable := NewSharedStringRefTable()
  161. styles := newXlsxStyleSheet(nil)
  162. xSheet := sheet.makeXLSXSheet(refTable, styles)
  163. output := bytes.NewBufferString(xml.Header)
  164. body, err := xml.Marshal(xSheet)
  165. c.Assert(err, IsNil)
  166. c.Assert(body, NotNil)
  167. _, err = output.Write(body)
  168. c.Assert(err, IsNil)
  169. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  170. <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" width="9.5"></col><col collapsed="false" hidden="false" max="2" min="2" 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>`
  171. c.Assert(output.String(), Equals, expectedXLSXSheet)
  172. }
  173. func (s *SheetSuite) TestSetColWidth(c *C) {
  174. file := NewFile()
  175. sheet := file.AddSheet("Sheet1")
  176. _ = sheet.SetColWidth(0, 0, 10.5)
  177. _ = sheet.SetColWidth(1, 5, 11)
  178. c.Assert(sheet.Cols[0].Width, Equals, 10.5)
  179. c.Assert(sheet.Cols[0].Max, Equals, 1)
  180. c.Assert(sheet.Cols[0].Min, Equals, 1)
  181. c.Assert(sheet.Cols[1].Width, Equals, float64(11))
  182. c.Assert(sheet.Cols[1].Max, Equals, 6)
  183. c.Assert(sheet.Cols[1].Min, Equals, 2)
  184. }