| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package xlsx
- import (
- "bytes"
- "encoding/xml"
- . "gopkg.in/check.v1"
- )
- type SheetSuite struct{}
- var _ = Suite(&SheetSuite{})
- // Test we can add a Row to a Sheet
- func (s *SheetSuite) TestAddRow(c *C) {
- var f *File
- f = NewFile()
- sheet := f.AddSheet("MySheet")
- row := sheet.AddRow()
- c.Assert(row, NotNil)
- c.Assert(len(sheet.Rows), Equals, 1)
- }
- func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
- file := NewFile()
- sheet := file.AddSheet("Sheet1")
- row := sheet.AddRow()
- cell := row.AddCell()
- cell.Value = "A cell!"
- refTable := NewSharedStringRefTable()
- xSheet := sheet.makeXLSXSheet(refTable)
- c.Assert(xSheet.Dimension.Ref, Equals, "A1:A1")
- c.Assert(xSheet.SheetData.Row, HasLen, 1)
- xRow := xSheet.SheetData.Row[0]
- c.Assert(xRow.R, Equals, 1)
- c.Assert(xRow.Spans, Equals, "")
- c.Assert(xRow.C, HasLen, 1)
- xC := xRow.C[0]
- c.Assert(xC.R, Equals, "A1")
- c.Assert(xC.S, Equals, 0)
- c.Assert(xC.T, Equals, "s") // Shared string type
- c.Assert(xC.V, Equals, "0") // reference to shared string
- xSST := refTable.makeXLSXSST()
- c.Assert(xSST.Count, Equals, 1)
- c.Assert(xSST.UniqueCount, Equals, 1)
- c.Assert(xSST.SI, HasLen, 1)
- xSI := xSST.SI[0]
- c.Assert(xSI.T, Equals, "A cell!")
- }
- func (s *SheetSuite) TestMarshalSheet(c *C) {
- file := NewFile()
- sheet := file.AddSheet("Sheet1")
- row := sheet.AddRow()
- cell := row.AddCell()
- cell.Value = "A cell!"
- refTable := NewSharedStringRefTable()
- xSheet := sheet.makeXLSXSheet(refTable)
- output := bytes.NewBufferString(xml.Header)
- body, err := xml.MarshalIndent(xSheet, " ", " ")
- c.Assert(err, IsNil)
- c.Assert(body, NotNil)
- _, err = output.Write(body)
- c.Assert(err, IsNil)
- expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
- <dimension ref="A1:A1"></dimension>
- <cols>
- <col min="1" max="1" width="9.5"></col>
- </cols>
- <sheetData>
- <row r="1">
- <c r="A1" t="s">
- <v>0</v>
- </c>
- </row>
- </sheetData>
- </worksheet>`
- c.Assert(output.String(), Equals, expectedXLSXSheet)
- }
- func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
- file := NewFile()
- sheet := file.AddSheet("Sheet1")
- row := sheet.AddRow()
- cell := row.AddCell()
- cell.Value = "A cell (with value 1)!"
- cell = row.AddCell()
- cell.Value = "A cell (with value 2)!"
- refTable := NewSharedStringRefTable()
- xSheet := sheet.makeXLSXSheet(refTable)
- output := bytes.NewBufferString(xml.Header)
- body, err := xml.MarshalIndent(xSheet, " ", " ")
- c.Assert(err, IsNil)
- c.Assert(body, NotNil)
- _, err = output.Write(body)
- c.Assert(err, IsNil)
- expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
- <dimension ref="A1:B1"></dimension>
- <cols>
- <col min="1" max="1" width="9.5"></col>
- <col min="2" max="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>
- </worksheet>`
- c.Assert(output.String(), Equals, expectedXLSXSheet)
- }
|