| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- package xlsx
- import (
- "bytes"
- "encoding/xml"
- // "strconv"
- . "gopkg.in/check.v1"
- "strings"
- )
- type LibSuite struct{}
- var _ = Suite(&LibSuite{})
- // Test that we can correctly extract a reference table from the
- // sharedStrings.xml file embedded in the XLSX file and return a
- // reference table of string values from it.
- func (l *LibSuite) TestReadSharedStringsFromZipFile(c *C) {
- var xlsxFile *File
- var err error
- xlsxFile, err = OpenFile("testfile.xlsx")
- c.Assert(err, IsNil)
- c.Assert(xlsxFile.referenceTable, NotNil)
- }
- // Helper function used to test contents of a given xlsxXf against
- // expectations.
- func testXf(c *C, result, expected *xlsxXf) {
- c.Assert(result.ApplyAlignment, Equals, expected.ApplyAlignment)
- c.Assert(result.ApplyBorder, Equals, expected.ApplyBorder)
- c.Assert(result.ApplyFont, Equals, expected.ApplyFont)
- c.Assert(result.ApplyFill, Equals, expected.ApplyFill)
- c.Assert(result.ApplyProtection, Equals, expected.ApplyProtection)
- c.Assert(result.BorderId, Equals, expected.BorderId)
- c.Assert(result.FillId, Equals, expected.FillId)
- c.Assert(result.FontId, Equals, expected.FontId)
- c.Assert(result.NumFmtId, Equals, expected.NumFmtId)
- }
- // We can correctly extract a style table from the style.xml file
- // embedded in the XLSX file and return a styles struct from it.
- func (l *LibSuite) TestReadStylesFromZipFile(c *C) {
- var xlsxFile *File
- var err error
- var fontCount, fillCount, borderCount, cellStyleXfCount, cellXfCount int
- var font xlsxFont
- var fill xlsxFill
- var border xlsxBorder
- var xf xlsxXf
- xlsxFile, err = OpenFile("testfile.xlsx")
- c.Assert(err, IsNil)
- c.Assert(xlsxFile.styles, NotNil)
- fontCount = len(xlsxFile.styles.Fonts)
- c.Assert(fontCount, Equals, 4)
- font = xlsxFile.styles.Fonts[0]
- c.Assert(font.Sz.Val, Equals, "11")
- c.Assert(font.Name.Val, Equals, "Calibri")
- fillCount = len(xlsxFile.styles.Fills)
- c.Assert(fillCount, Equals, 3)
- fill = xlsxFile.styles.Fills[2]
- c.Assert(fill.PatternFill.PatternType, Equals, "solid")
- borderCount = len(xlsxFile.styles.Borders)
- c.Assert(borderCount, Equals, 2)
- border = xlsxFile.styles.Borders[1]
- c.Assert(border.Left.Style, Equals, "thin")
- c.Assert(border.Right.Style, Equals, "thin")
- c.Assert(border.Top.Style, Equals, "thin")
- c.Assert(border.Bottom.Style, Equals, "thin")
- cellStyleXfCount = len(xlsxFile.styles.CellStyleXfs)
- c.Assert(cellStyleXfCount, Equals, 20)
- xf = xlsxFile.styles.CellStyleXfs[0]
- expectedXf := &xlsxXf{
- ApplyAlignment: true,
- ApplyBorder: true,
- ApplyFont: true,
- ApplyFill: false,
- ApplyProtection: true,
- BorderId: 0,
- FillId: 0,
- FontId: 0,
- NumFmtId: 164}
- testXf(c, &xf, expectedXf)
- cellXfCount = len(xlsxFile.styles.CellXfs)
- c.Assert(cellXfCount, Equals, 3)
- xf = xlsxFile.styles.CellXfs[0]
- expectedXf = &xlsxXf{
- ApplyAlignment: false,
- ApplyBorder: false,
- ApplyFont: false,
- ApplyFill: false,
- ApplyProtection: false,
- BorderId: 0,
- FillId: 0,
- FontId: 0,
- NumFmtId: 164}
- testXf(c, &xf, expectedXf)
- }
- // We can correctly extract a map of relationship Ids to the worksheet files in
- // which they are contained from the XLSX file.
- func (l *LibSuite) TestReadWorkbookRelationsFromZipFile(c *C) {
- var xlsxFile *File
- var err error
- xlsxFile, err = OpenFile("testfile.xlsx")
- c.Assert(err, IsNil)
- sheetCount := len(xlsxFile.Sheet)
- c.Assert(sheetCount, Equals, 3)
- }
- // which they are contained from the XLSX file, even when the
- // worksheet files have arbitrary, non-numeric names.
- func (l *LibSuite) TestReadWorkbookRelationsFromZipFileWithFunnyNames(c *C) {
- var xlsxFile *File
- var err error
- xlsxFile, err = OpenFile("testrels.xlsx")
- c.Assert(err, IsNil)
- sheetCount := len(xlsxFile.Sheet)
- c.Assert(sheetCount, Equals, 2)
- bob := xlsxFile.Sheet["Bob"]
- row1 := bob.Rows[0]
- cell1 := row1.Cells[0]
- c.Assert(cell1.String(), Equals, "I am Bob")
- }
- // Excel column codes are a special form of base26 that doesn't allow
- // zeros, except in the least significant part of the code. Test we
- // can smoosh the numbers in a normal base26 representation (presented
- // as a slice of integers) down to this form.
- func (l *LibSuite) TestSmooshBase26Slice(c *C) {
- input := []int{20, 0, 1}
- expected := []int{19, 26, 1}
- c.Assert(smooshBase26Slice(input), DeepEquals, expected)
- }
- // formatColumnName converts slices of base26 integers to alphabetical
- // column names. Note that the least signifcant character has a
- // different numeric offset (Yuck!)
- func (l *LibSuite) TestFormatColumnName(c *C) {
- c.Assert(formatColumnName([]int{0}), Equals, "A")
- c.Assert(formatColumnName([]int{25}), Equals, "Z")
- c.Assert(formatColumnName([]int{1, 25}), Equals, "AZ")
- c.Assert(formatColumnName([]int{26, 25}), Equals, "ZZ")
- c.Assert(formatColumnName([]int{26, 26, 25}), Equals, "ZZZ")
- }
- // getLargestDenominator returns the largest power of a provided value
- // that can fit within a given value.
- func (l *LibSuite) TestGetLargestDenominator(c *C) {
- d, p := getLargestDenominator(0, 1, 2, 0)
- c.Assert(d, Equals, 1)
- c.Assert(p, Equals, 0)
- d, p = getLargestDenominator(1, 1, 2, 0)
- c.Assert(d, Equals, 1)
- c.Assert(p, Equals, 0)
- d, p = getLargestDenominator(2, 1, 2, 0)
- c.Assert(d, Equals, 2)
- c.Assert(p, Equals, 1)
- d, p = getLargestDenominator(4, 1, 2, 0)
- c.Assert(d, Equals, 4)
- c.Assert(p, Equals, 2)
- d, p = getLargestDenominator(8, 1, 2, 0)
- c.Assert(d, Equals, 8)
- c.Assert(p, Equals, 3)
- d, p = getLargestDenominator(9, 1, 2, 0)
- c.Assert(d, Equals, 8)
- c.Assert(p, Equals, 3)
- d, p = getLargestDenominator(15,1, 2, 0)
- c.Assert(d, Equals, 8)
- c.Assert(p, Equals, 3)
- d, p = getLargestDenominator(16,1, 2, 0)
- c.Assert(d, Equals, 16)
- c.Assert(p, Equals, 4)
- }
- func (l *LibSuite) TestLettersToNumeric(c *C) {
- cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
- "BA": 52, "BZ": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
- "AAA": 26*26 + 26 + 0, "AMI": 1022}
- for input, ans := range cases {
- output := lettersToNumeric(input)
- c.Assert(output, Equals, ans)
- }
- }
- func (l *LibSuite) TestNumericToLetters(c *C) {
- cases := map[string]int{
- "A": 0,
- "G": 6,
- "Z": 25,
- "AA": 26,
- "AZ": 51,
- "BA": 52,
- "BZ": 77, "ZA": 26*26, "ZB": 26*26 + 1,
- "ZZ": 26*26 + 25,
- "AAA": 26*26 + 26 + 0, "AMI": 1022}
- for ans, input := range cases {
- output := numericToLetters(input)
- c.Assert(output, Equals, ans)
- }
- }
- func (l *LibSuite) TestLetterOnlyMapFunction(c *C) {
- var input string = "ABC123"
- var output string = strings.Map(letterOnlyMapF, input)
- c.Assert(output, Equals, "ABC")
- input = "abc123"
- output = strings.Map(letterOnlyMapF, input)
- c.Assert(output, Equals, "ABC")
- }
- func (l *LibSuite) TestIntOnlyMapFunction(c *C) {
- var input string = "ABC123"
- var output string = strings.Map(intOnlyMapF, input)
- c.Assert(output, Equals, "123")
- }
- func (l *LibSuite) TestGetCoordsFromCellIDString(c *C) {
- var cellIDString string = "A3"
- var x, y int
- var err error
- x, y, err = getCoordsFromCellIDString(cellIDString)
- c.Assert(err, IsNil)
- c.Assert(x, Equals, 0)
- c.Assert(y, Equals, 2)
- }
- func (l *LibSuite) TestGetCellIDStringFromCoords(c *C){
- c.Assert(getCellIDStringFromCoords(0, 0), Equals, "A1")
- c.Assert(getCellIDStringFromCoords(2, 2), Equals, "C3")
- }
- func (l *LibSuite) TestGetMaxMinFromDimensionRef(c *C) {
- var dimensionRef string = "A1:B2"
- var minx, miny, maxx, maxy int
- var err error
- minx, miny, maxx, maxy, err = getMaxMinFromDimensionRef(dimensionRef)
- c.Assert(err, IsNil)
- c.Assert(minx, Equals, 0)
- c.Assert(miny, Equals, 0)
- c.Assert(maxx, Equals, 1)
- c.Assert(maxy, Equals, 1)
- }
- func (l *LibSuite) TestGetRangeFromString(c *C) {
- var rangeString string
- var lower, upper int
- var err error
- rangeString = "1:3"
- lower, upper, err = getRangeFromString(rangeString)
- c.Assert(err, IsNil)
- c.Assert(lower, Equals, 1)
- c.Assert(upper, Equals, 3)
- }
- func (l *LibSuite) TestMakeRowFromSpan(c *C) {
- var rangeString string
- var row *Row
- var length int
- rangeString = "1:3"
- row = makeRowFromSpan(rangeString)
- length = len(row.Cells)
- c.Assert(length, Equals, 3)
- rangeString = "5:7" // Note - we ignore lower bound!
- row = makeRowFromSpan(rangeString)
- length = len(row.Cells)
- c.Assert(length, Equals, 7)
- rangeString = "1:1"
- row = makeRowFromSpan(rangeString)
- length = len(row.Cells)
- c.Assert(length, Equals, 1)
- }
- func (l *LibSuite) TestReadRowsFromSheet(c *C) {
- var sharedstringsXML = bytes.NewBufferString(`
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
- <si>
- <t>Foo</t>
- </si>
- <si>
- <t>Bar</t>
- </si>
- <si>
- <t xml:space="preserve">Baz </t>
- </si>
- <si>
- <t>Quuk</t>
- </si>
- </sst>`)
- var sheetxml = bytes.NewBufferString(`
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
- xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
- <dimension ref="A1:B2"/>
- <sheetViews>
- <sheetView tabSelected="1" workbookViewId="0">
- <selection activeCell="C2" sqref="C2"/>
- </sheetView>
- </sheetViews>
- <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
- <sheetData>
- <row r="1" spans="1:2">
- <c r="A1" t="s">
- <v>0</v>
- </c>
- <c r="B1" t="s">
- <v>1</v>
- </c>
- </row>
- <row r="2" spans="1:2">
- <c r="A2" t="s">
- <v>2</v>
- </c>
- <c r="B2" t="s">
- <v>3</v>
- </c>
- </row>
- </sheetData>
- <pageMargins left="0.7" right="0.7"
- top="0.78740157499999996"
- bottom="0.78740157499999996"
- header="0.3"
- footer="0.3"/>
- </worksheet>`)
- worksheet := new(xlsxWorksheet)
- err := xml.NewDecoder(sheetxml).Decode(worksheet)
- c.Assert(err, IsNil)
- sst := new(xlsxSST)
- err = xml.NewDecoder(sharedstringsXML).Decode(sst)
- c.Assert(err, IsNil)
- file := new(File)
- file.referenceTable = MakeSharedStringRefTable(sst)
- rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
- c.Assert(maxRows, Equals, 2)
- c.Assert(maxCols, Equals, 2)
- row := rows[0]
- c.Assert(len(row.Cells), Equals, 2)
- cell1 := row.Cells[0]
- c.Assert(cell1.String(), Equals, "Foo")
- cell2 := row.Cells[1]
- c.Assert(cell2.String(), Equals, "Bar")
- }
- func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
- var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2" uniqueCount="2"><si><t>ABC</t></si><si><t>DEF</t></si></sst>`)
- var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
- <dimension ref="A4:A5"/>
- <sheetViews>
- <sheetView tabSelected="1" workbookViewId="0">
- <selection activeCell="A2" sqref="A2"/>
- </sheetView>
- </sheetViews>
- <sheetFormatPr baseColWidth="10" defaultRowHeight="15" x14ac:dyDescent="0"/>
- <sheetData>
- <row r="4" spans="1:1">
- <c r="A4" t="s">
- <v>0</v>
- </c>
- </row>
- <row r="5" spans="1:1">
- <c r="A5" t="s">
- <v>1</v>
- </c>
- </row>
- </sheetData>
- <pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
- <pageSetup paperSize="9" orientation="portrait" horizontalDpi="4294967292" verticalDpi="4294967292"/>
- <extLst>
- <ext uri="{64002731-A6B0-56B0-2670-7721B7C09600}" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main">
- <mx:PLV Mode="0" OnePage="0" WScale="0"/>
- </ext>
- </extLst>
- </worksheet>
- `)
- worksheet := new(xlsxWorksheet)
- err := xml.NewDecoder(sheetxml).Decode(worksheet)
- c.Assert(err, IsNil)
- sst := new(xlsxSST)
- err = xml.NewDecoder(sharedstringsXML).Decode(sst)
- c.Assert(err, IsNil)
- file := new(File)
- file.referenceTable = MakeSharedStringRefTable(sst)
- _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
- c.Assert(maxRows, Equals, 2)
- c.Assert(maxCols, Equals, 1)
- }
- func (l *LibSuite) TestReadRowsFromSheetWithEmptyCells(c *C) {
- var sharedstringsXML = bytes.NewBufferString(`
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5">
- <si>
- <t>Bob</t>
- </si>
- <si>
- <t>Alice</t>
- </si>
- <si>
- <t>Sue</t>
- </si>
- <si>
- <t>Yes</t>
- </si>
- <si>
- <t>No</t>
- </si>
- </sst>
- `)
- var sheetxml = bytes.NewBufferString(`
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C3"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="D3" sqref="D3"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
- <sheetData>
- <row r="1" spans="1:3">
- <c r="A1" t="s">
- <v>
- 0
- </v>
- </c>
- <c r="B1" t="s">
- <v>
- 1
- </v>
- </c>
- <c r="C1" t="s">
- <v>
- 2
- </v>
- </c>
- </row>
- <row r="2" spans="1:3">
- <c r="A2" t="s">
- <v>
- 3
- </v>
- </c>
- <c r="B2" t="s">
- <v>
- 4
- </v>
- </c>
- <c r="C2" t="s">
- <v>
- 3
- </v>
- </c>
- </row>
- <row r="3" spans="1:3">
- <c r="A3" t="s">
- <v>
- 4
- </v>
- </c>
- <c r="C3" t="s">
- <v>
- 3
- </v>
- </c>
- </row>
- </sheetData>
- <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
- </worksheet>
- `)
- worksheet := new(xlsxWorksheet)
- err := xml.NewDecoder(sheetxml).Decode(worksheet)
- c.Assert(err, IsNil)
- sst := new(xlsxSST)
- err = xml.NewDecoder(sharedstringsXML).Decode(sst)
- c.Assert(err, IsNil)
- file := new(File)
- file.referenceTable = MakeSharedStringRefTable(sst)
- rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
- c.Assert(maxRows, Equals, 3)
- c.Assert(maxCols, Equals, 3)
- row := rows[2]
- c.Assert(len(row.Cells), Equals, 3)
- cell1 := row.Cells[0]
- c.Assert(cell1.String(), Equals, "No")
- cell2 := row.Cells[1]
- c.Assert(cell2.String(), Equals, "")
- cell3 := row.Cells[2]
- c.Assert(cell3.String(), Equals, "Yes")
- }
- func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
- var row *Row
- var cell1, cell2, cell3, cell4 *Cell
- var sharedstringsXML = bytes.NewBufferString(`
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>A</t></si><si><t>B</t></si><si><t>C</t></si><si><t>D</t></si></sst>`)
- var sheetxml = bytes.NewBufferString(`
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:D8"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A7" sqref="A7"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:4"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c><c r="D1" t="s"><v>3</v></c></row><row r="2" spans="1:4"><c r="A2"><v>1</v></c></row><row r="3" spans="1:4"><c r="B3"><v>1</v></c></row><row r="4" spans="1:4"><c r="C4"><v>1</v></c></row><row r="5" spans="1:4"><c r="D5"><v>1</v></c></row><row r="6" spans="1:4"><c r="C6"><v>1</v></c></row><row r="7" spans="1:4"><c r="B7"><v>1</v></c></row><row r="8" spans="1:4"><c r="A8"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>
- `)
- worksheet := new(xlsxWorksheet)
- err := xml.NewDecoder(sheetxml).Decode(worksheet)
- c.Assert(err, IsNil)
- sst := new(xlsxSST)
- err = xml.NewDecoder(sharedstringsXML).Decode(sst)
- c.Assert(err, IsNil)
- file := new(File)
- file.referenceTable = MakeSharedStringRefTable(sst)
- rows, maxCol, maxRow := readRowsFromSheet(worksheet, file)
- c.Assert(maxCol, Equals, 4)
- c.Assert(maxRow, Equals, 8)
- row = rows[0]
- c.Assert(len(row.Cells), Equals, 4)
- cell1 = row.Cells[0]
- c.Assert(cell1.String(), Equals, "A")
- cell2 = row.Cells[1]
- c.Assert(cell2.String(), Equals, "B")
- cell3 = row.Cells[2]
- c.Assert(cell3.String(), Equals, "C")
- cell4 = row.Cells[3]
- c.Assert(cell4.String(), Equals, "D")
- row = rows[1]
- c.Assert(len(row.Cells), Equals, 4)
- cell1 = row.Cells[0]
- c.Assert(cell1.String(), Equals, "1")
- cell2 = row.Cells[1]
- c.Assert(cell2.String(), Equals, "")
- cell3 = row.Cells[2]
- c.Assert(cell3.String(), Equals, "")
- cell4 = row.Cells[3]
- c.Assert(cell4.String(), Equals, "")
- }
|