Explorar el Código

Modify readRowsFromSheet to handle series of rows and cells omitted from the
XML because they're blank.

Geoffrey J. Teale hace 12 años
padre
commit
229e811767
Se han modificado 4 ficheros con 42 adiciones y 16 borrados
  1. 26 6
      lib.go
  2. 4 4
      lib_test.go
  3. BIN
      wpsBlankLineTest.xlsx
  4. 12 6
      wpsBlankLine_test.go

+ 26 - 6
lib.go

@@ -309,6 +309,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 	var minCol, maxCol, minRow, maxRow, colCount, rowCount int
 	var minCol, maxCol, minRow, maxRow, colCount, rowCount int
 	var reftable []string
 	var reftable []string
 	var err error
 	var err error
+	var insertRowIndex, insertColIndex int
 
 
 	if len(Worksheet.SheetData.Row) == 0 {
 	if len(Worksheet.SheetData.Row) == 0 {
 		return nil, 0, 0
 		return nil, 0, 0
@@ -321,23 +322,42 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 	rowCount = (maxRow - minRow) + 1
 	rowCount = (maxRow - minRow) + 1
 	colCount = (maxCol - minCol) + 1
 	colCount = (maxCol - minCol) + 1
 	rows = make([]*Row, rowCount)
 	rows = make([]*Row, rowCount)
-	for rowIndex := 0; rowIndex < rowCount; rowIndex++ {
+	insertRowIndex = minRow
+	for rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
 		rawrow := Worksheet.SheetData.Row[rowIndex]
 		rawrow := Worksheet.SheetData.Row[rowIndex]
+		// Some spreadsheets will omit blank rows from the
+		// stored data
+		for rawrow.R > (insertRowIndex + 1) {
+			// Put an empty Row into the array
+			rows[insertRowIndex-minRow] = new(Row)
+			insertRowIndex++
+		}
 		// range is not empty
 		// range is not empty
 		if len(rawrow.Spans) != 0 {
 		if len(rawrow.Spans) != 0 {
 			row = makeRowFromSpan(rawrow.Spans)
 			row = makeRowFromSpan(rawrow.Spans)
 		} else {
 		} else {
 			row = makeRowFromRaw(rawrow)
 			row = makeRowFromRaw(rawrow)
 		}
 		}
+
+		insertColIndex = minCol
 		for _, rawcell := range rawrow.C {
 		for _, rawcell := range rawrow.C {
 			x, _, _ := getCoordsFromCellIDString(rawcell.R)
 			x, _, _ := getCoordsFromCellIDString(rawcell.R)
-			if x < len(row.Cells) {
-				row.Cells[x].Value = getValueFromCellData(rawcell, reftable)
-				row.Cells[x].styleIndex = rawcell.S
-				row.Cells[x].styles = file.styles
+
+			// Some spreadsheets will omit blank cells
+			// from the data.
+			for x > (insertColIndex + 1) {
+				// Put an empty Cell into the array
+				row.Cells[insertColIndex-minCol] = new(Cell)
+				insertColIndex++
 			}
 			}
+			cellX := insertColIndex - minCol
+			row.Cells[cellX].Value = getValueFromCellData(rawcell, reftable)
+			row.Cells[cellX].styleIndex = rawcell.S
+			row.Cells[cellX].styles = file.styles
+			insertColIndex++
 		}
 		}
-		rows[rowIndex] = row
+		rows[insertRowIndex-minRow] = row
+		insertRowIndex++
 	}
 	}
 	return rows, colCount, rowCount
 	return rows, colCount, rowCount
 }
 }

+ 4 - 4
lib_test.go

@@ -731,12 +731,12 @@ func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
 		t.Error("Expected cell1.String() == 'No', got ", cell1.String())
 		t.Error("Expected cell1.String() == 'No', got ", cell1.String())
 	}
 	}
 	cell2 := row.Cells[1]
 	cell2 := row.Cells[1]
-	if cell2.String() != "" {
-		t.Error("Expected cell2.String() == '', got ", cell2.String())
+	if cell2.String() != "Yes" {
+		t.Error("Expected cell2.String() == 'Yes', got ", cell2.String())
 	}
 	}
 	cell3 := row.Cells[2]
 	cell3 := row.Cells[2]
-	if cell3.String() != "Yes" {
-		t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
+	if cell3.String() != "" {
+		t.Error("Expected cell3.String() == 'No', got ", cell3.String())
 	}
 	}
 
 
 }
 }

BIN
wpsBlankLineTest.xlsx


+ 12 - 6
wpsBlankLine_test.go

@@ -16,14 +16,20 @@ func TestWpsBlankLine(t *testing.T) {
 		t.Error("OpenFile returned nil FileInterface without generating an os.Error")
 		t.Error("OpenFile returned nil FileInterface without generating an os.Error")
 		return
 		return
 	}
 	}
-	s := xlsxFile.Sheets[0].Cell(0, 0).String()
-	if s != "编号" {
-		t.Errorf("[TestMacExcel] xlsxFile.Sheets[0].Cell(0,0).String():'%s'", s)
+	sheet := xlsxFile.Sheets[0]
+	row := sheet.Rows[0]
+	cell := row.Cells[0]
+	s := cell.String()
+	expected := "编号"
+	if s != expected {
+		t.Errorf("[TestMacExcel] expected cell A1 = '%s'', but got :'%s'", expected, s)
 		return
 		return
 	}
 	}
-	s = xlsxFile.Sheets[0].Cell(0, 2).String()
-	if s != "编号" {
-		t.Errorf("[TestMacExcel] xlsxFile.Sheets[0].Cell(0,0).String():'%s'", s)
+	row = sheet.Rows[2]
+	cell = row.Cells[0]
+	s = cell.String()
+	if s != expected {
+		t.Errorf("[TestMacExcel] expected cell A3 = '%s'', but got :'%s'", expected, s)
 		return
 		return
 	}
 	}
 }
 }