sheet_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. // Create a file with three rows.
  12. var f *File
  13. f = NewFile()
  14. sheet, _ := f.AddSheet("MySheet")
  15. row0 := sheet.AddRow()
  16. cell0 := row0.AddCell()
  17. cell0.Value = "Row 0"
  18. c.Assert(row0, NotNil)
  19. row1 := sheet.AddRow()
  20. cell1 := row1.AddCell()
  21. cell1.Value = "Row 1"
  22. row2 := sheet.AddRow()
  23. cell2 := row2.AddCell()
  24. cell2.Value = "Row 2"
  25. // Check the file
  26. expected := []string{"Row 0", "Row 1", "Row 2"}
  27. c.Assert(len(sheet.Rows), Equals, len(expected))
  28. for i, row := range sheet.Rows {
  29. c.Assert(row.Cells[0].Value, Equals, expected[i])
  30. }
  31. // Insert a row in the middle
  32. row1pt5, err := sheet.AddRowAtIndex(2)
  33. c.Assert(err, IsNil)
  34. cell1pt5 := row1pt5.AddCell()
  35. cell1pt5.Value = "Row 1.5"
  36. expected = []string{"Row 0", "Row 1", "Row 1.5", "Row 2"}
  37. c.Assert(len(sheet.Rows), Equals, len(expected))
  38. for i, row := range sheet.Rows {
  39. c.Assert(row.Cells[0].Value, Equals, expected[i])
  40. }
  41. // Insert a row at the beginning
  42. rowNewStart, err := sheet.AddRowAtIndex(0)
  43. c.Assert(err, IsNil)
  44. cellNewStart := rowNewStart.AddCell()
  45. cellNewStart.Value = "Row -1"
  46. // Insert a row at one index past the end, this is the same as AddRow().
  47. row2pt5, err := sheet.AddRowAtIndex(5)
  48. c.Assert(err, IsNil)
  49. cell2pt5 := row2pt5.AddCell()
  50. cell2pt5.Value = "Row 2.5"
  51. expected = []string{"Row -1", "Row 0", "Row 1", "Row 1.5", "Row 2", "Row 2.5"}
  52. c.Assert(len(sheet.Rows), Equals, len(expected))
  53. for i, row := range sheet.Rows {
  54. c.Assert(row.Cells[0].Value, Equals, expected[i])
  55. }
  56. // Negative and out of range indicies should fail for insert
  57. _, err = sheet.AddRowAtIndex(-1)
  58. c.Assert(err, NotNil)
  59. // Since we allow inserting into the position that does not yet exist, it has to be 1 greater
  60. // than you would think in order to fail.
  61. _, err = sheet.AddRowAtIndex(7)
  62. c.Assert(err, NotNil)
  63. // Negative and out of range indicies should fail for remove
  64. err = sheet.RemoveRowAtIndex(-1)
  65. c.Assert(err, NotNil)
  66. err = sheet.RemoveRowAtIndex(6)
  67. c.Assert(err, NotNil)
  68. // Remove from the beginning, the end, and the middle.
  69. err = sheet.RemoveRowAtIndex(0)
  70. c.Assert(err, IsNil)
  71. err = sheet.RemoveRowAtIndex(4)
  72. c.Assert(err, IsNil)
  73. err = sheet.RemoveRowAtIndex(2)
  74. c.Assert(err, IsNil)
  75. expected = []string{"Row 0", "Row 1", "Row 2"}
  76. c.Assert(len(sheet.Rows), Equals, len(expected))
  77. for i, row := range sheet.Rows {
  78. c.Assert(row.Cells[0].Value, Equals, expected[i])
  79. }
  80. }
  81. func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
  82. file := NewFile()
  83. sheet, _ := file.AddSheet("Sheet1")
  84. row := sheet.AddRow()
  85. cell := row.AddCell()
  86. cell.Value = "A cell!"
  87. refTable := NewSharedStringRefTable()
  88. styles := newXlsxStyleSheet(nil)
  89. xSheet := sheet.makeXLSXSheet(refTable, styles)
  90. c.Assert(xSheet.Dimension.Ref, Equals, "A1")
  91. c.Assert(xSheet.SheetData.Row, HasLen, 1)
  92. xRow := xSheet.SheetData.Row[0]
  93. c.Assert(xRow.R, Equals, 1)
  94. c.Assert(xRow.Spans, Equals, "")
  95. c.Assert(xRow.C, HasLen, 1)
  96. xC := xRow.C[0]
  97. c.Assert(xC.R, Equals, "A1")
  98. c.Assert(xC.S, Equals, 0)
  99. c.Assert(xC.T, Equals, "s") // Shared string type
  100. c.Assert(xC.V, Equals, "0") // reference to shared string
  101. xSST := refTable.makeXLSXSST()
  102. c.Assert(xSST.Count, Equals, 1)
  103. c.Assert(xSST.UniqueCount, Equals, 1)
  104. c.Assert(xSST.SI, HasLen, 1)
  105. xSI := xSST.SI[0]
  106. c.Assert(xSI.T, Equals, "A cell!")
  107. }
  108. // Test if the NumFmts assigned properly according the FormatCode in cell.
  109. func (s *SheetSuite) TestMakeXLSXSheetWithNumFormats(c *C) {
  110. file := NewFile()
  111. sheet, _ := file.AddSheet("Sheet1")
  112. row := sheet.AddRow()
  113. cell1 := row.AddCell()
  114. cell1.Value = "A cell!"
  115. cell1.NumFmt = "general"
  116. cell2 := row.AddCell()
  117. cell2.Value = "37947.7500001"
  118. cell2.NumFmt = "0"
  119. cell3 := row.AddCell()
  120. cell3.Value = "37947.7500001"
  121. cell3.NumFmt = "mm-dd-yy"
  122. cell4 := row.AddCell()
  123. cell4.Value = "37947.7500001"
  124. cell4.NumFmt = "hh:mm:ss"
  125. refTable := NewSharedStringRefTable()
  126. styles := newXlsxStyleSheet(nil)
  127. worksheet := sheet.makeXLSXSheet(refTable, styles)
  128. c.Assert(styles.CellStyleXfs, IsNil)
  129. c.Assert(styles.CellXfs.Count, Equals, 4)
  130. c.Assert(styles.CellXfs.Xf[0].NumFmtId, Equals, 0)
  131. c.Assert(styles.CellXfs.Xf[1].NumFmtId, Equals, 1)
  132. c.Assert(styles.CellXfs.Xf[2].NumFmtId, Equals, 14)
  133. c.Assert(styles.CellXfs.Xf[3].NumFmtId, Equals, 164)
  134. c.Assert(styles.NumFmts.Count, Equals, 1)
  135. c.Assert(styles.NumFmts.NumFmt[0].NumFmtId, Equals, 164)
  136. c.Assert(styles.NumFmts.NumFmt[0].FormatCode, Equals, "hh:mm:ss")
  137. // Finally we check that the cell points to the right CellXf /
  138. // CellStyleXf.
  139. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
  140. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  141. }
  142. // When we create the xlsxSheet we also populate the xlsxStyles struct
  143. // with style information.
  144. func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
  145. file := NewFile()
  146. sheet, _ := file.AddSheet("Sheet1")
  147. row := sheet.AddRow()
  148. cell1 := row.AddCell()
  149. cell1.Value = "A cell!"
  150. style1 := NewStyle()
  151. style1.Font = *NewFont(10, "Verdana")
  152. style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  153. style1.Border = *NewBorder("none", "thin", "none", "thin")
  154. cell1.SetStyle(style1)
  155. // We need a second style to check that Xfs are populated correctly.
  156. cell2 := row.AddCell()
  157. cell2.Value = "Another cell!"
  158. style2 := NewStyle()
  159. style2.Font = *NewFont(10, "Verdana")
  160. style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  161. style2.Border = *NewBorder("none", "thin", "none", "thin")
  162. cell2.SetStyle(style2)
  163. refTable := NewSharedStringRefTable()
  164. styles := newXlsxStyleSheet(nil)
  165. worksheet := sheet.makeXLSXSheet(refTable, styles)
  166. c.Assert(styles.Fonts.Count, Equals, 2)
  167. c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "12")
  168. c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
  169. c.Assert(styles.Fonts.Font[1].Sz.Val, Equals, "10")
  170. c.Assert(styles.Fonts.Font[1].Name.Val, Equals, "Verdana")
  171. c.Assert(styles.Fills.Count, Equals, 3)
  172. c.Assert(styles.Fills.Fill[0].PatternFill.PatternType, Equals, "none")
  173. c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
  174. c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "00000000")
  175. c.Assert(styles.Borders.Count, Equals, 2)
  176. c.Assert(styles.Borders.Border[1].Left.Style, Equals, "none")
  177. c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
  178. c.Assert(styles.Borders.Border[1].Top.Style, Equals, "none")
  179. c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
  180. c.Assert(styles.CellStyleXfs, IsNil)
  181. c.Assert(styles.CellXfs.Count, Equals, 2)
  182. c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
  183. c.Assert(styles.CellXfs.Xf[0].FillId, Equals, 0)
  184. c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
  185. // Finally we check that the cell points to the right CellXf /
  186. // CellStyleXf.
  187. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
  188. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  189. }
  190. // If the column width is not customised, the xslxCol.CustomWidth field is set to 0.
  191. func (s *SheetSuite) TestMakeXLSXSheetDefaultsCustomColWidth(c *C) {
  192. file := NewFile()
  193. sheet, _ := file.AddSheet("Sheet1")
  194. row := sheet.AddRow()
  195. cell1 := row.AddCell()
  196. cell1.Value = "A cell!"
  197. refTable := NewSharedStringRefTable()
  198. styles := newXlsxStyleSheet(nil)
  199. worksheet := sheet.makeXLSXSheet(refTable, styles)
  200. c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, false)
  201. }
  202. // If the column width is customised, the xslxCol.CustomWidth field is set to 1.
  203. func (s *SheetSuite) TestMakeXLSXSheetSetsCustomColWidth(c *C) {
  204. file := NewFile()
  205. sheet, _ := file.AddSheet("Sheet1")
  206. row := sheet.AddRow()
  207. cell1 := row.AddCell()
  208. cell1.Value = "A cell!"
  209. err := sheet.SetColWidth(0, 1, 10.5)
  210. c.Assert(err, IsNil)
  211. refTable := NewSharedStringRefTable()
  212. styles := newXlsxStyleSheet(nil)
  213. worksheet := sheet.makeXLSXSheet(refTable, styles)
  214. c.Assert(worksheet.Cols.Col[1].CustomWidth, Equals, true)
  215. }
  216. func (s *SheetSuite) TestMarshalSheet(c *C) {
  217. file := NewFile()
  218. sheet, _ := file.AddSheet("Sheet1")
  219. row := sheet.AddRow()
  220. cell := row.AddCell()
  221. cell.Value = "A cell!"
  222. refTable := NewSharedStringRefTable()
  223. styles := newXlsxStyleSheet(nil)
  224. xSheet := sheet.makeXLSXSheet(refTable, styles)
  225. output := bytes.NewBufferString(xml.Header)
  226. body, err := xml.Marshal(xSheet)
  227. c.Assert(err, IsNil)
  228. c.Assert(body, NotNil)
  229. _, err = output.Write(body)
  230. c.Assert(err, IsNil)
  231. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  232. <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>`
  233. c.Assert(output.String(), Equals, expectedXLSXSheet)
  234. }
  235. func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
  236. file := NewFile()
  237. sheet, _ := file.AddSheet("Sheet1")
  238. row := sheet.AddRow()
  239. cell := row.AddCell()
  240. cell.Value = "A cell (with value 1)!"
  241. cell = row.AddCell()
  242. cell.Value = "A cell (with value 2)!"
  243. refTable := NewSharedStringRefTable()
  244. styles := newXlsxStyleSheet(nil)
  245. xSheet := sheet.makeXLSXSheet(refTable, styles)
  246. output := bytes.NewBufferString(xml.Header)
  247. body, err := xml.Marshal(xSheet)
  248. c.Assert(err, IsNil)
  249. c.Assert(body, NotNil)
  250. _, err = output.Write(body)
  251. c.Assert(err, IsNil)
  252. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  253. <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>`
  254. c.Assert(output.String(), Equals, expectedXLSXSheet)
  255. }
  256. func (s *SheetSuite) TestSetColWidth(c *C) {
  257. file := NewFile()
  258. sheet, _ := file.AddSheet("Sheet1")
  259. _ = sheet.SetColWidth(0, 0, 10.5)
  260. _ = sheet.SetColWidth(1, 5, 11)
  261. c.Assert(sheet.Cols[0].Width, Equals, 10.5)
  262. c.Assert(sheet.Cols[0].Max, Equals, 1)
  263. c.Assert(sheet.Cols[0].Min, Equals, 1)
  264. c.Assert(sheet.Cols[1].Width, Equals, float64(11))
  265. c.Assert(sheet.Cols[1].Max, Equals, 6)
  266. c.Assert(sheet.Cols[1].Min, Equals, 2)
  267. }
  268. func (s *SheetSuite) TestSetRowHeightCM(c *C) {
  269. file := NewFile()
  270. sheet, _ := file.AddSheet("Sheet1")
  271. row := sheet.AddRow()
  272. row.SetHeightCM(1.5)
  273. c.Assert(row.Height, Equals, 42.51968505)
  274. }
  275. func (s *SheetSuite) TestAlignment(c *C) {
  276. leftalign := *DefaultAlignment()
  277. leftalign.Horizontal = "left"
  278. centerHalign := *DefaultAlignment()
  279. centerHalign.Horizontal = "center"
  280. rightalign := *DefaultAlignment()
  281. rightalign.Horizontal = "right"
  282. file := NewFile()
  283. sheet, _ := file.AddSheet("Sheet1")
  284. style := NewStyle()
  285. hrow := sheet.AddRow()
  286. // Horizontals
  287. cell := hrow.AddCell()
  288. cell.Value = "left"
  289. style.Alignment = leftalign
  290. style.ApplyAlignment = true
  291. cell.SetStyle(style)
  292. style = NewStyle()
  293. cell = hrow.AddCell()
  294. cell.Value = "centerH"
  295. style.Alignment = centerHalign
  296. style.ApplyAlignment = true
  297. cell.SetStyle(style)
  298. style = NewStyle()
  299. cell = hrow.AddCell()
  300. cell.Value = "right"
  301. style.Alignment = rightalign
  302. style.ApplyAlignment = true
  303. cell.SetStyle(style)
  304. // Verticals
  305. topalign := *DefaultAlignment()
  306. topalign.Vertical = "top"
  307. centerValign := *DefaultAlignment()
  308. centerValign.Vertical = "center"
  309. bottomalign := *DefaultAlignment()
  310. bottomalign.Vertical = "bottom"
  311. style = NewStyle()
  312. vrow := sheet.AddRow()
  313. cell = vrow.AddCell()
  314. cell.Value = "top"
  315. style.Alignment = topalign
  316. style.ApplyAlignment = true
  317. cell.SetStyle(style)
  318. style = NewStyle()
  319. cell = vrow.AddCell()
  320. cell.Value = "centerV"
  321. style.Alignment = centerValign
  322. style.ApplyAlignment = true
  323. cell.SetStyle(style)
  324. style = NewStyle()
  325. cell = vrow.AddCell()
  326. cell.Value = "bottom"
  327. style.Alignment = bottomalign
  328. style.ApplyAlignment = true
  329. cell.SetStyle(style)
  330. parts, err := file.MarshallParts()
  331. c.Assert(err, IsNil)
  332. obtained := parts["xl/styles.xml"]
  333. shouldbe := `<?xml version="1.0" encoding="UTF-8"?>
  334. <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><cellXfs count="8"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" 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="bottom" 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="bottom" 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="bottom" 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="general" 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="general" 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="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellXfs></styleSheet>`
  335. expected := bytes.NewBufferString(shouldbe)
  336. c.Assert(obtained, Equals, expected.String())
  337. }
  338. func (s *SheetSuite) TestBorder(c *C) {
  339. file := NewFile()
  340. sheet, _ := file.AddSheet("Sheet1")
  341. row := sheet.AddRow()
  342. cell1 := row.AddCell()
  343. cell1.Value = "A cell!"
  344. style1 := NewStyle()
  345. style1.Border = *NewBorder("thin", "thin", "thin", "thin")
  346. style1.ApplyBorder = true
  347. cell1.SetStyle(style1)
  348. refTable := NewSharedStringRefTable()
  349. styles := newXlsxStyleSheet(nil)
  350. worksheet := sheet.makeXLSXSheet(refTable, styles)
  351. c.Assert(styles.Borders.Border[1].Left.Style, Equals, "thin")
  352. c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
  353. c.Assert(styles.Borders.Border[1].Top.Style, Equals, "thin")
  354. c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
  355. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
  356. }
  357. func (s *SheetSuite) TestOutlineLevels(c *C) {
  358. file := NewFile()
  359. sheet, _ := file.AddSheet("Sheet1")
  360. r1 := sheet.AddRow()
  361. c11 := r1.AddCell()
  362. c11.Value = "A1"
  363. c12 := r1.AddCell()
  364. c12.Value = "B1"
  365. r2 := sheet.AddRow()
  366. c21 := r2.AddCell()
  367. c21.Value = "A2"
  368. c22 := r2.AddCell()
  369. c22.Value = "B2"
  370. r3 := sheet.AddRow()
  371. c31 := r3.AddCell()
  372. c31.Value = "A3"
  373. c32 := r3.AddCell()
  374. c32.Value = "B3"
  375. // Add some groups
  376. r1.OutlineLevel = 1
  377. r2.OutlineLevel = 2
  378. sheet.Col(0).OutlineLevel = 1
  379. refTable := NewSharedStringRefTable()
  380. styles := newXlsxStyleSheet(nil)
  381. worksheet := sheet.makeXLSXSheet(refTable, styles)
  382. c.Assert(worksheet.SheetFormatPr.OutlineLevelCol, Equals, uint8(1))
  383. c.Assert(worksheet.SheetFormatPr.OutlineLevelRow, Equals, uint8(2))
  384. c.Assert(worksheet.Cols.Col[0].OutlineLevel, Equals, uint8(1))
  385. c.Assert(worksheet.Cols.Col[1].OutlineLevel, Equals, uint8(0))
  386. c.Assert(worksheet.SheetData.Row[0].OutlineLevel, Equals, uint8(1))
  387. c.Assert(worksheet.SheetData.Row[1].OutlineLevel, Equals, uint8(2))
  388. c.Assert(worksheet.SheetData.Row[2].OutlineLevel, Equals, uint8(0))
  389. }
  390. func (s *SheetSuite) TestAutoFilter(c *C) {
  391. file := NewFile()
  392. sheet, _ := file.AddSheet("Sheet1")
  393. r1 := sheet.AddRow()
  394. r1.AddCell()
  395. r1.AddCell()
  396. r1.AddCell()
  397. r2 := sheet.AddRow()
  398. r2.AddCell()
  399. r2.AddCell()
  400. r2.AddCell()
  401. r3 := sheet.AddRow()
  402. r3.AddCell()
  403. r3.AddCell()
  404. r3.AddCell()
  405. // Define a filter area
  406. sheet.AutoFilter = &AutoFilter{TopLeftCell: "B2", BottomRightCell: "C3"}
  407. refTable := NewSharedStringRefTable()
  408. styles := newXlsxStyleSheet(nil)
  409. worksheet := sheet.makeXLSXSheet(refTable, styles)
  410. c.Assert(worksheet.AutoFilter, NotNil)
  411. c.Assert(worksheet.AutoFilter.Ref, Equals, "B2:C3")
  412. }