Просмотр исходного кода

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

Geoffrey J. Teale 12 лет назад
Родитель
Сommit
229e811767
4 измененных файлов с 42 добавлено и 16 удалено
  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 reftable []string
 	var err error
+	var insertRowIndex, insertColIndex int
 
 	if len(Worksheet.SheetData.Row) == 0 {
 		return nil, 0, 0
@@ -321,23 +322,42 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 	rowCount = (maxRow - minRow) + 1
 	colCount = (maxCol - minCol) + 1
 	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]
+		// 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
 		if len(rawrow.Spans) != 0 {
 			row = makeRowFromSpan(rawrow.Spans)
 		} else {
 			row = makeRowFromRaw(rawrow)
 		}
+
+		insertColIndex = minCol
 		for _, rawcell := range rawrow.C {
 			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
 }

+ 4 - 4
lib_test.go

@@ -731,12 +731,12 @@ func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
 		t.Error("Expected cell1.String() == 'No', got ", cell1.String())
 	}
 	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]
-	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")
 		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
 	}
-	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
 	}
 }