sheet_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 := &xlsxStyles{}
  26. xSheet := sheet.makeXLSXSheet(refTable, styles)
  27. c.Assert(xSheet.Dimension.Ref, Equals, "A1: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. // When we create the xlsxSheet we also populate the xlsxStyles struct
  46. // with style information.
  47. func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
  48. file := NewFile()
  49. sheet := file.AddSheet("Sheet1")
  50. row := sheet.AddRow()
  51. cell1 := row.AddCell()
  52. cell1.Value = "A cell!"
  53. style1 := *NewStyle()
  54. style1.Font = *NewFont(10, "Verdana")
  55. style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  56. style1.Border = *NewBorder("none", "thin", "none", "thin")
  57. cell1.SetStyle(style1)
  58. // We need a second style to check that Xfs are populated correctly.
  59. cell2 := row.AddCell()
  60. cell2.Value = "Another cell!"
  61. style2 := *NewStyle()
  62. style2.Font = *NewFont(10, "Verdana")
  63. style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
  64. style2.Border = *NewBorder("none", "thin", "none", "thin")
  65. cell2.SetStyle(style2)
  66. refTable := NewSharedStringRefTable()
  67. styles := &xlsxStyles{}
  68. worksheet := sheet.makeXLSXSheet(refTable, styles)
  69. c.Assert(len(styles.Fonts), Equals, 2)
  70. c.Assert(styles.Fonts[0].Sz.Val, Equals, "10")
  71. c.Assert(styles.Fonts[0].Name.Val, Equals, "Verdana")
  72. c.Assert(len(styles.Fills), Equals, 2)
  73. c.Assert(styles.Fills[0].PatternFill.PatternType, Equals, "solid")
  74. c.Assert(styles.Fills[0].PatternFill.FgColor.RGB, Equals, "FFFFFFFF")
  75. c.Assert(styles.Fills[0].PatternFill.BgColor.RGB, Equals, "00000000")
  76. c.Assert(len(styles.Borders), Equals, 2)
  77. c.Assert(styles.Borders[0].Left.Style, Equals, "none")
  78. c.Assert(styles.Borders[0].Right.Style, Equals, "thin")
  79. c.Assert(styles.Borders[0].Top.Style, Equals, "none")
  80. c.Assert(styles.Borders[0].Bottom.Style, Equals, "thin")
  81. c.Assert(len(styles.CellStyleXfs), Equals, 2)
  82. // The 0th CellStyleXf could just be getting the zero values by default
  83. c.Assert(styles.CellStyleXfs[0].FontId, Equals, 0)
  84. c.Assert(styles.CellStyleXfs[0].FillId, Equals, 0)
  85. c.Assert(styles.CellStyleXfs[0].BorderId, Equals, 0)
  86. // The 1st element cannot get initialised this way by accident.
  87. c.Assert(styles.CellStyleXfs[1].FontId, Equals, 1)
  88. c.Assert(styles.CellStyleXfs[1].FillId, Equals, 1)
  89. c.Assert(styles.CellStyleXfs[1].BorderId, Equals, 1)
  90. c.Assert(len(styles.CellXfs), Equals, 2)
  91. c.Assert(styles.CellXfs[0].FontId, Equals, 0)
  92. c.Assert(styles.CellXfs[0].FillId, Equals, 0)
  93. c.Assert(styles.CellXfs[0].BorderId, Equals, 0)
  94. // As above, we need the 1st element to make the test fail
  95. // when it should.
  96. c.Assert(styles.CellXfs[1].FontId, Equals, 1)
  97. c.Assert(styles.CellXfs[1].FillId, Equals, 1)
  98. c.Assert(styles.CellXfs[1].BorderId, Equals, 1)
  99. // Finally we check that the cell points to the right CellXf /
  100. // CellStyleXf.
  101. c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
  102. c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
  103. }
  104. func (s *SheetSuite) TestMarshalSheet(c *C) {
  105. file := NewFile()
  106. sheet := file.AddSheet("Sheet1")
  107. row := sheet.AddRow()
  108. cell := row.AddCell()
  109. cell.Value = "A cell!"
  110. refTable := NewSharedStringRefTable()
  111. styles := &xlsxStyles{}
  112. xSheet := sheet.makeXLSXSheet(refTable, styles)
  113. output := bytes.NewBufferString(xml.Header)
  114. body, err := xml.MarshalIndent(xSheet, " ", " ")
  115. c.Assert(err, IsNil)
  116. c.Assert(body, NotNil)
  117. _, err = output.Write(body)
  118. c.Assert(err, IsNil)
  119. expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
  120. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  121. <dimension ref="A1:A1"></dimension>
  122. <cols>
  123. <col min="1" max="1"></col>
  124. </cols>
  125. <sheetData>
  126. <row r="1">
  127. <c r="A1" s="0" t="s">
  128. <v>0</v>
  129. </c>
  130. </row>
  131. </sheetData>
  132. </worksheet>`
  133. c.Assert(output.String(), Equals, expectedXLSXSheet)
  134. }