sheet_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. // Test we can get row by index from Sheet
  82. func (s *SheetSuite) TestGetRowByIndex(c *C) {
  83. var f *File
  84. f = NewFile()
  85. sheet, _ := f.AddSheet("MySheet")
  86. row := sheet.Row(10)
  87. c.Assert(row, NotNil)
  88. c.Assert(len(sheet.Rows), Equals, 11)
  89. row = sheet.Row(2)
  90. c.Assert(row, NotNil)
  91. c.Assert(len(sheet.Rows), Equals, 11)
  92. }
  93. func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
  94. file := NewFile()
  95. sheet, _ := file.AddSheet("Sheet1")
  96. row := sheet.AddRow()
  97. cell := row.AddCell()
  98. cell.Value = "A cell!"
  99. refTable := NewSharedStringRefTable()
  100. styles := newXlsxStyleSheet(nil)
  101. xSheet := sheet.makeXLSXSheet(refTable, styles)
  102. c.Assert(xSheet.Dimension.Ref, Equals, "A1")
  103. c.Assert(xSheet.SheetData.Row, HasLen, 1)
  104. xRow := xSheet.SheetData.Row[0]
  105. c.Assert(xRow.R, Equals, 1)
  106. c.Assert(xRow.Spans, Equals, "")
  107. c.Assert(xRow.C, HasLen, 1)
  108. xC := xRow.C[0]
  109. c.Assert(xC.R, Equals, "A1")
  110. c.Assert(xC.S, Equals, 0)
  111. c.Assert(xC.T, Equals, "s") // Shared string type
  112. c.Assert(xC.V, Equals, "0") // reference to shared string
  113. xSST := refTable.makeXLSXSST()
  114. c.Assert(xSST.Count, Equals, 1)
  115. c.Assert(xSST.UniqueCount, Equals, 1)
  116. c.Assert(xSST.SI, HasLen, 1)
  117. xSI := xSST.SI[0]
  118. c.Assert(xSI.T, Equals, "A cell!")
  119. }
  120. // Test if the NumFmts assigned properly according the FormatCode in cell.
  121. func (s *SheetSuite) TestMakeXLSXSheetWithNumFormats(c *C) {
  122. file := NewFile()
  123. sheet, _ := file.AddSheet("Sheet1")
  124. row := sheet.AddRow()
  125. cell1 := row.AddCell()
  126. cell1.Value = "A cell!"
  127. cell1.NumFmt = "general"
  128. cell2 := row.AddCell()
  129. cell2.Value = "37947.7500001"
  130. cell2.NumFmt = "0"
  131. cell3 := row.AddCell()
  132. cell3.Value = "37947.7500001"
  133. cell3.NumFmt = "mm-dd-yy"
  134. cell4 := row.AddCell()
  135. cell4.Value = "37947.7500001"
  136. cell4.NumFmt = "hh:mm:ss"
  137. refTable := NewSharedStringRefTable()
  138. styles := newXlsxStyleSheet(nil)
  139. worksheet := sheet.makeXLSXSheet(refTable, styles)
  140. c.Assert(styles.CellStyleXfs, IsNil)
  141. c.Assert(styles.CellXfs.Count, Equals, 4)
  142. c.Assert(styles.CellXfs.Xf[0].NumFmtId, Equals, 0)
  143. c.Assert(styles.CellXfs.Xf[1].NumFmtId, Equals, 1)
  144. c.Assert(styles.CellXfs.Xf[2].NumFmtId, Equals, 14)
  145. c.Assert(styles.CellXfs.Xf[3].NumFmtId, Equals, 164)
  146. c.Assert(styles.NumFmts.Count, Equals, 1)
  147. c.Assert(styles.NumFmts.NumFmt[0].NumFmtId, Equals, 164)
  148. c.Assert(styles.NumFmts.NumFmt[0].FormatCode, Equals, "hh:mm:ss")
  149. // Finally we check that the cell points to the right CellXf /
  150. // CellStyleXf.
  151. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
  152. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  153. }
  154. // When we create the xlsxSheet we also populate the xlsxStyles struct
  155. // with style information.
  156. func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
  157. file := NewFile()
  158. sheet, _ := file.AddSheet("Sheet1")
  159. row := sheet.AddRow()
  160. cell1 := row.AddCell()
  161. cell1.Value = "A cell!"
  162. style1 := NewStyle()
  163. style1.Font = *NewFont(10, "Verdana")
  164. style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  165. style1.Border = *NewBorder("none", "thin", "none", "thin")
  166. cell1.SetStyle(style1)
  167. // We need a second style to check that Xfs are populated correctly.
  168. cell2 := row.AddCell()
  169. cell2.Value = "Another cell!"
  170. style2 := NewStyle()
  171. style2.Font = *NewFont(10, "Verdana")
  172. style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  173. style2.Border = *NewBorder("none", "thin", "none", "thin")
  174. cell2.SetStyle(style2)
  175. refTable := NewSharedStringRefTable()
  176. styles := newXlsxStyleSheet(nil)
  177. worksheet := sheet.makeXLSXSheet(refTable, styles)
  178. c.Assert(styles.Fonts.Count, Equals, 2)
  179. c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "12")
  180. c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
  181. c.Assert(styles.Fonts.Font[1].Sz.Val, Equals, "10")
  182. c.Assert(styles.Fonts.Font[1].Name.Val, Equals, "Verdana")
  183. c.Assert(styles.Fills.Count, Equals, 3)
  184. c.Assert(styles.Fills.Fill[0].PatternFill.PatternType, Equals, "none")
  185. c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "")
  186. c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "")
  187. c.Assert(styles.Borders.Count, Equals, 2)
  188. c.Assert(styles.Borders.Border[1].Left.Style, Equals, "none")
  189. c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
  190. c.Assert(styles.Borders.Border[1].Top.Style, Equals, "none")
  191. c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
  192. c.Assert(styles.CellStyleXfs, IsNil)
  193. c.Assert(styles.CellXfs.Count, Equals, 2)
  194. c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
  195. c.Assert(styles.CellXfs.Xf[0].FillId, Equals, 0)
  196. c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
  197. // Finally we check that the cell points to the right CellXf /
  198. // CellStyleXf.
  199. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
  200. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  201. }
  202. // If the column width is not customised, the xslxCol.CustomWidth field is set to 0.
  203. func (s *SheetSuite) TestMakeXLSXSheetDefaultsCustomColWidth(c *C) {
  204. file := NewFile()
  205. sheet, _ := file.AddSheet("Sheet1")
  206. row := sheet.AddRow()
  207. cell1 := row.AddCell()
  208. cell1.Value = "A cell!"
  209. refTable := NewSharedStringRefTable()
  210. styles := newXlsxStyleSheet(nil)
  211. worksheet := sheet.makeXLSXSheet(refTable, styles)
  212. c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, false)
  213. }
  214. // If the column width is customised, the xslxCol.CustomWidth field is set to 1.
  215. func (s *SheetSuite) TestMakeXLSXSheetSetsCustomColWidth(c *C) {
  216. file := NewFile()
  217. sheet, _ := file.AddSheet("Sheet1")
  218. row := sheet.AddRow()
  219. cell1 := row.AddCell()
  220. cell1.Value = "A cell!"
  221. err := sheet.SetColWidth(0, 1, 10.5)
  222. c.Assert(err, IsNil)
  223. refTable := NewSharedStringRefTable()
  224. styles := newXlsxStyleSheet(nil)
  225. worksheet := sheet.makeXLSXSheet(refTable, styles)
  226. c.Assert(worksheet.Cols.Col[1].CustomWidth, Equals, true)
  227. }
  228. func (s *SheetSuite) TestMarshalSheet(c *C) {
  229. file := NewFile()
  230. sheet, _ := file.AddSheet("Sheet1")
  231. row := sheet.AddRow()
  232. cell := row.AddCell()
  233. cell.Value = "A cell!"
  234. refTable := NewSharedStringRefTable()
  235. styles := newXlsxStyleSheet(nil)
  236. xSheet := sheet.makeXLSXSheet(refTable, styles)
  237. output := bytes.NewBufferString(xml.Header)
  238. body, err := xml.Marshal(xSheet)
  239. c.Assert(err, IsNil)
  240. c.Assert(body, NotNil)
  241. _, err = output.Write(body)
  242. c.Assert(err, IsNil)
  243. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  244. <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>`
  245. c.Assert(output.String(), Equals, expectedXLSXSheet)
  246. }
  247. func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
  248. file := NewFile()
  249. sheet, _ := file.AddSheet("Sheet1")
  250. row := sheet.AddRow()
  251. cell := row.AddCell()
  252. cell.Value = "A cell (with value 1)!"
  253. cell = row.AddCell()
  254. cell.Value = "A cell (with value 2)!"
  255. refTable := NewSharedStringRefTable()
  256. styles := newXlsxStyleSheet(nil)
  257. xSheet := sheet.makeXLSXSheet(refTable, styles)
  258. output := bytes.NewBufferString(xml.Header)
  259. body, err := xml.Marshal(xSheet)
  260. c.Assert(err, IsNil)
  261. c.Assert(body, NotNil)
  262. _, err = output.Write(body)
  263. c.Assert(err, IsNil)
  264. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  265. <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>`
  266. c.Assert(output.String(), Equals, expectedXLSXSheet)
  267. }
  268. func (s *SheetSuite) TestSetColWidth(c *C) {
  269. file := NewFile()
  270. sheet, _ := file.AddSheet("Sheet1")
  271. _ = sheet.SetColWidth(0, 0, 10.5)
  272. _ = sheet.SetColWidth(1, 5, 11)
  273. c.Assert(sheet.Cols[0].Width, Equals, 10.5)
  274. c.Assert(sheet.Cols[0].Max, Equals, 1)
  275. c.Assert(sheet.Cols[0].Min, Equals, 1)
  276. c.Assert(sheet.Cols[1].Width, Equals, float64(11))
  277. c.Assert(sheet.Cols[1].Max, Equals, 2)
  278. c.Assert(sheet.Cols[1].Min, Equals, 2)
  279. }
  280. func (s *SheetSuite) TestSetRowHeightCM(c *C) {
  281. file := NewFile()
  282. sheet, _ := file.AddSheet("Sheet1")
  283. row := sheet.AddRow()
  284. row.SetHeightCM(1.5)
  285. c.Assert(row.Height, Equals, 42.51968505)
  286. }
  287. func (s *SheetSuite) TestAlignment(c *C) {
  288. leftalign := *DefaultAlignment()
  289. leftalign.Horizontal = "left"
  290. centerHalign := *DefaultAlignment()
  291. centerHalign.Horizontal = "center"
  292. rightalign := *DefaultAlignment()
  293. rightalign.Horizontal = "right"
  294. file := NewFile()
  295. sheet, _ := file.AddSheet("Sheet1")
  296. style := NewStyle()
  297. hrow := sheet.AddRow()
  298. // Horizontals
  299. cell := hrow.AddCell()
  300. cell.Value = "left"
  301. style.Alignment = leftalign
  302. style.ApplyAlignment = true
  303. cell.SetStyle(style)
  304. style = NewStyle()
  305. cell = hrow.AddCell()
  306. cell.Value = "centerH"
  307. style.Alignment = centerHalign
  308. style.ApplyAlignment = true
  309. cell.SetStyle(style)
  310. style = NewStyle()
  311. cell = hrow.AddCell()
  312. cell.Value = "right"
  313. style.Alignment = rightalign
  314. style.ApplyAlignment = true
  315. cell.SetStyle(style)
  316. // Verticals
  317. topalign := *DefaultAlignment()
  318. topalign.Vertical = "top"
  319. centerValign := *DefaultAlignment()
  320. centerValign.Vertical = "center"
  321. bottomalign := *DefaultAlignment()
  322. bottomalign.Vertical = "bottom"
  323. style = NewStyle()
  324. vrow := sheet.AddRow()
  325. cell = vrow.AddCell()
  326. cell.Value = "top"
  327. style.Alignment = topalign
  328. style.ApplyAlignment = true
  329. cell.SetStyle(style)
  330. style = NewStyle()
  331. cell = vrow.AddCell()
  332. cell.Value = "centerV"
  333. style.Alignment = centerValign
  334. style.ApplyAlignment = true
  335. cell.SetStyle(style)
  336. style = NewStyle()
  337. cell = vrow.AddCell()
  338. cell.Value = "bottom"
  339. style.Alignment = bottomalign
  340. style.ApplyAlignment = true
  341. cell.SetStyle(style)
  342. parts, err := file.MarshallParts()
  343. c.Assert(err, IsNil)
  344. obtained := parts["xl/styles.xml"]
  345. shouldbe := `<?xml version="1.0" encoding="UTF-8"?>
  346. <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"/></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>`
  347. expected := bytes.NewBufferString(shouldbe)
  348. c.Assert(obtained, Equals, expected.String())
  349. }
  350. func (s *SheetSuite) TestBorder(c *C) {
  351. file := NewFile()
  352. sheet, _ := file.AddSheet("Sheet1")
  353. row := sheet.AddRow()
  354. cell1 := row.AddCell()
  355. cell1.Value = "A cell!"
  356. style1 := NewStyle()
  357. style1.Border = *NewBorder("thin", "thin", "thin", "thin")
  358. style1.ApplyBorder = true
  359. cell1.SetStyle(style1)
  360. refTable := NewSharedStringRefTable()
  361. styles := newXlsxStyleSheet(nil)
  362. worksheet := sheet.makeXLSXSheet(refTable, styles)
  363. c.Assert(styles.Borders.Border[1].Left.Style, Equals, "thin")
  364. c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
  365. c.Assert(styles.Borders.Border[1].Top.Style, Equals, "thin")
  366. c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
  367. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
  368. }
  369. func (s *SheetSuite) TestOutlineLevels(c *C) {
  370. file := NewFile()
  371. sheet, _ := file.AddSheet("Sheet1")
  372. r1 := sheet.AddRow()
  373. c11 := r1.AddCell()
  374. c11.Value = "A1"
  375. c12 := r1.AddCell()
  376. c12.Value = "B1"
  377. r2 := sheet.AddRow()
  378. c21 := r2.AddCell()
  379. c21.Value = "A2"
  380. c22 := r2.AddCell()
  381. c22.Value = "B2"
  382. r3 := sheet.AddRow()
  383. c31 := r3.AddCell()
  384. c31.Value = "A3"
  385. c32 := r3.AddCell()
  386. c32.Value = "B3"
  387. // Add some groups
  388. r1.OutlineLevel = 1
  389. r2.OutlineLevel = 2
  390. sheet.Col(0).OutlineLevel = 1
  391. refTable := NewSharedStringRefTable()
  392. styles := newXlsxStyleSheet(nil)
  393. worksheet := sheet.makeXLSXSheet(refTable, styles)
  394. c.Assert(worksheet.SheetFormatPr.OutlineLevelCol, Equals, uint8(1))
  395. c.Assert(worksheet.SheetFormatPr.OutlineLevelRow, Equals, uint8(2))
  396. c.Assert(worksheet.Cols.Col[0].OutlineLevel, Equals, uint8(1))
  397. c.Assert(worksheet.Cols.Col[1].OutlineLevel, Equals, uint8(0))
  398. c.Assert(worksheet.SheetData.Row[0].OutlineLevel, Equals, uint8(1))
  399. c.Assert(worksheet.SheetData.Row[1].OutlineLevel, Equals, uint8(2))
  400. c.Assert(worksheet.SheetData.Row[2].OutlineLevel, Equals, uint8(0))
  401. }
  402. func (s *SheetSuite) TestAutoFilter(c *C) {
  403. file := NewFile()
  404. sheet, _ := file.AddSheet("Sheet1")
  405. r1 := sheet.AddRow()
  406. r1.AddCell()
  407. r1.AddCell()
  408. r1.AddCell()
  409. r2 := sheet.AddRow()
  410. r2.AddCell()
  411. r2.AddCell()
  412. r2.AddCell()
  413. r3 := sheet.AddRow()
  414. r3.AddCell()
  415. r3.AddCell()
  416. r3.AddCell()
  417. // Define a filter area
  418. sheet.AutoFilter = &AutoFilter{TopLeftCell: "B2", BottomRightCell: "C3"}
  419. refTable := NewSharedStringRefTable()
  420. styles := newXlsxStyleSheet(nil)
  421. worksheet := sheet.makeXLSXSheet(refTable, styles)
  422. c.Assert(worksheet.AutoFilter, NotNil)
  423. c.Assert(worksheet.AutoFilter.Ref, Equals, "B2:C3")
  424. }