Преглед на файлове

Rework patch to set Row.Sheet at point of creation, and avoid additional loop. Fix call sites and unit tests, adding assertions as I went.

Geoffrey J. Teale преди 10 години
родител
ревизия
8b98a07e3f
променени са 2 файла, в които са добавени 57 реда и са изтрити 28 реда
  1. 13 12
      lib.go
  2. 44 16
      lib_test.go

+ 13 - 12
lib.go

@@ -251,13 +251,14 @@ func calculateMaxMinFromWorksheet(worksheet *xlsxWorksheet) (minx, miny, maxx, m
 // return an empty Row large enough to encompass that span and
 // populate it with empty cells.  All rows start from cell 1 -
 // regardless of the lower bound of the span.
-func makeRowFromSpan(spans string) *Row {
+func makeRowFromSpan(spans string, sheet *Sheet) *Row {
 	var error error
 	var upper int
 	var row *Row
 	var cell *Cell
 
 	row = new(Row)
+	row.Sheet = sheet
 	_, upper, error = getRangeFromString(spans)
 	if error != nil {
 		panic(error)
@@ -273,12 +274,13 @@ func makeRowFromSpan(spans string) *Row {
 }
 
 // makeRowFromRaw returns the Row representation of the xlsxRow.
-func makeRowFromRaw(rawrow xlsxRow) *Row {
+func makeRowFromRaw(rawrow xlsxRow, sheet *Sheet) *Row {
 	var upper int
 	var row *Row
 	var cell *Cell
 
 	row = new(Row)
+	row.Sheet = sheet
 	upper = -1
 
 	for _, rawcell := range rawrow.C {
@@ -305,9 +307,10 @@ func makeRowFromRaw(rawrow xlsxRow) *Row {
 	return row
 }
 
-func makeEmptyRow() *Row {
+func makeEmptyRow(sheet *Sheet) *Row {
 	row := new(Row)
 	row.Cells = make([]*Cell, 0)
+	row.Sheet = sheet
 	return row
 }
 
@@ -417,7 +420,7 @@ func fillCellData(rawcell xlsxC, reftable *RefTable, sharedFormulas map[int]shar
 // rows from a XSLXWorksheet, populates them with Cells and resolves
 // the value references from the reference table and stores them in
 // the rows and columns.
-func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, int, int) {
+func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File, sheet *Sheet) ([]*Row, []*Col, int, int) {
 	var rows []*Row
 	var cols []*Col
 	var row *Row
@@ -439,6 +442,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, in
 	if err != nil {
 		panic(err.Error())
 	}
+
 	rowCount = maxRow + 1
 	colCount = maxCol + 1
 	rows = make([]*Row, rowCount)
@@ -473,7 +477,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, in
 
 	// insert leading empty rows that is in front of minRow
 	for rowIndex := 0; rowIndex < minRow; rowIndex++ {
-		rows[rowIndex] = makeEmptyRow()
+		rows[rowIndex] = makeEmptyRow(sheet)
 	}
 
 	numRows := len(rows)
@@ -485,15 +489,15 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, []*Col, in
 			// Put an empty Row into the array
 			index := insertRowIndex - minRow
 			if index < numRows {
-				rows[index] = makeEmptyRow()
+				rows[index] = makeEmptyRow(sheet)
 			}
 			insertRowIndex++
 		}
 		// range is not empty and only one range exist
 		if len(rawrow.Spans) != 0 && strings.Count(rawrow.Spans, ":") == 1 {
-			row = makeRowFromSpan(rawrow.Spans)
+			row = makeRowFromSpan(rawrow.Spans, sheet)
 		} else {
-			row = makeRowFromRaw(rawrow)
+			row = makeRowFromRaw(rawrow, sheet)
 		}
 
 		row.Hidden = rawrow.Hidden
@@ -584,10 +588,7 @@ func readSheetFromFile(sc chan *indexedSheet, index int, rsheet xlsxSheet, fi *F
 	}
 	sheet := new(Sheet)
 	sheet.File = fi
-	sheet.Rows, sheet.Cols, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi)
-	for _, row := range sheet.Rows {
-		row.Sheet = sheet
-	}
+	sheet.Rows, sheet.Cols, sheet.MaxCol, sheet.MaxRow = readRowsFromSheet(worksheet, fi, sheet)
 	sheet.Hidden = rsheet.State == sheetStateHidden || rsheet.State == sheetStateVeryHidden
 	sheet.SheetViews = readSheetViews(worksheet.SheetViews)
 

+ 44 - 16
lib_test.go

@@ -239,18 +239,23 @@ func (l *LibSuite) TestMakeRowFromSpan(c *C) {
 	var rangeString string
 	var row *Row
 	var length int
+	var sheet *Sheet
+	sheet = new(Sheet)
 	rangeString = "1:3"
-	row = makeRowFromSpan(rangeString)
+	row = makeRowFromSpan(rangeString, sheet)
 	length = len(row.Cells)
 	c.Assert(length, Equals, 3)
+	c.Assert(row.Sheet, Equals, sheet)
 	rangeString = "5:7" // Note - we ignore lower bound!
-	row = makeRowFromSpan(rangeString)
+	row = makeRowFromSpan(rangeString, sheet)
 	length = len(row.Cells)
 	c.Assert(length, Equals, 7)
+	c.Assert(row.Sheet, Equals, sheet)
 	rangeString = "1:1"
-	row = makeRowFromSpan(rangeString)
+	row = makeRowFromSpan(rangeString, sheet)
 	length = len(row.Cells)
 	c.Assert(length, Equals, 1)
+	c.Assert(row.Sheet, Equals, sheet)
 }
 
 func (l *LibSuite) TestReadRowsFromSheet(c *C) {
@@ -314,10 +319,12 @@ func (l *LibSuite) TestReadRowsFromSheet(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 2)
 	c.Assert(maxCols, Equals, 2)
 	row := rows[0]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 2)
 	cell1 := row.Cells[0]
 	c.Assert(cell1.Value, Equals, "Foo")
@@ -402,9 +409,10 @@ func (l *LibSuite) TestReadRowsFromSheetBadR(c *C) {
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
 
+	sheet := new(Sheet)
 	// Discarding all return values; this test is a regression for
 	// a panic due to an "index out of range."
-	readRowsFromSheet(worksheet, file)
+	readRowsFromSheet(worksheet, file, sheet)
 }
 
 func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
@@ -449,7 +457,8 @@ func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
 
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 5)
 	c.Assert(maxCols, Equals, 1)
 
@@ -506,7 +515,8 @@ func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyCols(c *C) {
 
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 2)
 	c.Assert(maxCols, Equals, 4)
 
@@ -612,11 +622,13 @@ func (l *LibSuite) TestReadRowsFromSheetWithEmptyCells(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, cols, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 3)
 	c.Assert(maxCols, Equals, 3)
 
 	row := rows[2]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 3)
 
 	cell1 := row.Cells[0]
@@ -654,11 +666,13 @@ func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
 
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, _, maxCol, maxRow := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, _, maxCol, maxRow := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxCol, Equals, 4)
 	c.Assert(maxRow, Equals, 8)
 
 	row = rows[0]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 4)
 
 	cell1 = row.Cells[0]
@@ -674,6 +688,7 @@ func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
 	c.Assert(cell4.Value, Equals, "D")
 
 	row = rows[1]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 4)
 
 	cell1 = row.Cells[0]
@@ -761,10 +776,12 @@ func (l *LibSuite) TestReadRowsFromSheetWithMultipleSpans(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 2)
 	c.Assert(maxCols, Equals, 4)
 	row := rows[0]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 4)
 	cell1 := row.Cells[0]
 	c.Assert(cell1.Value, Equals, "Foo")
@@ -834,10 +851,12 @@ func (l *LibSuite) TestReadRowsFromSheetWithMultipleTypes(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 1)
 	c.Assert(maxCols, Equals, 6)
 	row := rows[0]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 6)
 
 	cell1 := row.Cells[0]
@@ -901,10 +920,12 @@ func (l *LibSuite) TestReadRowsFromSheetWithHiddenColumn(c *C) {
 	c.Assert(err, IsNil)
 	file := new(File)
 	file.referenceTable = MakeSharedStringRefTable(sst)
-	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxRows, Equals, 1)
 	c.Assert(maxCols, Equals, 2)
 	row := rows[0]
+	c.Assert(row.Sheet, Equals, sheet)
 	c.Assert(len(row.Cells), Equals, 2)
 
 	cell1 := row.Cells[0]
@@ -928,9 +949,11 @@ func (l *LibSuite) TestReadRowFromRaw(c *C) {
 	cell = xlsxC{R: "A1"}
 	cell = xlsxC{R: "A2"}
 	rawRow.C = append(rawRow.C, cell)
-	row = makeRowFromRaw(rawRow)
+	sheet := new(Sheet)
+	row = makeRowFromRaw(rawRow, sheet)
 	c.Assert(row, NotNil)
 	c.Assert(row.Cells, HasLen, 1)
+	c.Assert(row.Sheet, Equals, sheet)
 }
 
 // When a cell claims it is at a position greater than its ordinal
@@ -945,9 +968,11 @@ func (l *LibSuite) TestReadRowFromRawWithMissingCells(c *C) {
 	rawRow.C = append(rawRow.C, cell)
 	cell = xlsxC{R: "E1"}
 	rawRow.C = append(rawRow.C, cell)
-	row = makeRowFromRaw(rawRow)
+	sheet := new(Sheet)
+	row = makeRowFromRaw(rawRow, sheet)
 	c.Assert(row, NotNil)
 	c.Assert(row.Cells, HasLen, 5)
+	c.Assert(row.Sheet, Equals, sheet)
 }
 
 // We can cope with missing coordinate references
@@ -965,9 +990,11 @@ func (l *LibSuite) TestReadRowFromRawWithPartialCoordinates(c *C) {
 	rawRow.C = append(rawRow.C, cell)
 	cell = xlsxC{}
 	rawRow.C = append(rawRow.C, cell)
-	row = makeRowFromRaw(rawRow)
+	sheet := new(Sheet)
+	row = makeRowFromRaw(rawRow, sheet)
 	c.Assert(row, NotNil)
 	c.Assert(row.Cells, HasLen, 27)
+	c.Assert(row.Sheet, Equals, sheet)
 }
 
 func (l *LibSuite) TestSharedFormulas(c *C) {
@@ -1021,7 +1048,8 @@ func (l *LibSuite) TestSharedFormulas(c *C) {
 	c.Assert(err, IsNil)
 
 	file := new(File)
-	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
+	sheet := new(Sheet)
+	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
 	c.Assert(maxCols, Equals, 3)
 	c.Assert(maxRows, Equals, 2)