sheet_test.go 20 KB

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