Browse Source

Deal with XLSX worksheets that only set the coords on *some* cells.

Geoffrey J. Teale 11 years ago
parent
commit
1af9906a50
3 changed files with 63 additions and 7 deletions
  1. 10 6
      lib.go
  2. 52 0
      lib_test.go
  3. 1 1
      xmlStyle.go

+ 10 - 6
lib.go

@@ -282,13 +282,17 @@ func makeRowFromRaw(rawrow xlsxRow) *Row {
 	upper = -1
 
 	for _, rawcell := range rawrow.C {
-		x, _, error := getCoordsFromCellIDString(rawcell.R)
-		if error != nil {
-			panic(fmt.Sprintf("Invalid Cell Coord, %s\n", rawcell.R))
-		}
-		if x > upper {
-			upper = x
+		if rawcell.R != "" {
+			x, _, error := getCoordsFromCellIDString(rawcell.R)
+			if error != nil {
+				panic(fmt.Sprintf("Invalid Cell Coord, %s\n", rawcell.R))
+			}
+			if x > upper {
+				upper = x
+			}
+			continue
 		}
+		upper++
 	}
 	upper++
 

+ 52 - 0
lib_test.go

@@ -701,3 +701,55 @@ func (l *LibSuite) TestReadRowsFromSheetWithMultipleTypes(c *C) {
 	c.Assert(cell6.Formula(), Equals, "10/0")
 	c.Assert(cell6.Value, Equals, "#DIV/0!")
 }
+
+// When converting the xlsxRow to a Row we create a as many cells as we find.
+func (l *LibSuite) TestReadRowFromRaw(c *C) {
+	var rawRow xlsxRow
+	var cell xlsxC
+	var row *Row
+
+	rawRow = xlsxRow{}
+	cell = xlsxC{R: "A1"}
+	cell = xlsxC{R: "A2"}
+	rawRow.C = append(rawRow.C, cell)
+	row = makeRowFromRaw(rawRow)
+	c.Assert(row, NotNil)
+	c.Assert(row.Cells, HasLen, 1)
+}
+
+// When a cell claims it is at a position greater than its ordinal
+// position in the file we make up the missing cells.
+func (l *LibSuite) TestReadRowFromRawWithMissingCells(c *C) {
+	var rawRow xlsxRow
+	var cell xlsxC
+	var row *Row
+
+	rawRow = xlsxRow{}
+	cell = xlsxC{R: "A1"}
+	rawRow.C = append(rawRow.C, cell)
+	cell = xlsxC{R: "E1"}
+	rawRow.C = append(rawRow.C, cell)
+	row = makeRowFromRaw(rawRow)
+	c.Assert(row, NotNil)
+	c.Assert(row.Cells, HasLen, 5)
+}
+
+// We can cope with missing coordinate references
+func (l *LibSuite) TestReadRowFromRawWithPartialCoordinates(c *C) {
+	var rawRow xlsxRow
+	var cell xlsxC
+	var row *Row
+
+	rawRow = xlsxRow{}
+	cell = xlsxC{R: "A1"}
+	rawRow.C = append(rawRow.C, cell)
+	cell = xlsxC{}
+	rawRow.C = append(rawRow.C, cell)
+	cell = xlsxC{R: "Z:1"}
+	rawRow.C = append(rawRow.C, cell)
+	cell = xlsxC{}
+	rawRow.C = append(rawRow.C, cell)
+	row = makeRowFromRaw(rawRow)
+	c.Assert(row, NotNil)
+	c.Assert(row.Cells, HasLen, 27)
+}

+ 1 - 1
xmlStyle.go

@@ -33,7 +33,7 @@ type xlsxStyleSheet struct {
 	CellXfs      xlsxCellXfs      `xml:"cellXfs,omitempty"`
 	NumFmts      xlsxNumFmts      `xml:"numFmts,omitempty"`
 
-	styleCache map[int]*Style `-`
+	styleCache map[int]*Style // `-`
 	lock       *sync.RWMutex
 }