Jelajahi Sumber

Fix an issue with empty rows (I forgot that these are emitted from the XML).

Geoffrey J. Teale 12 tahun lalu
induk
melakukan
196249312c
3 mengubah file dengan 5 tambahan dan 5 penghapusan
  1. 2 2
      lib.go
  2. 1 1
      worksheet.go
  3. 2 2
      worksheet_test.go

+ 2 - 2
lib.go

@@ -321,7 +321,7 @@ 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++ {
+	for rowIndex := 0; rowIndex < len(Worksheet.SheetData.Row); rowIndex++ {
 		rawrow := Worksheet.SheetData.Row[rowIndex]
 		// range is not empty
 		if len(rawrow.Spans) != 0 {
@@ -337,7 +337,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 				row.Cells[x].styles = file.styles
 			}
 		}
-		rows[rowIndex] = row
+		rows[rawrow.R - 1] = row
 	}
 	return rows, colCount, rowCount
 }

+ 1 - 1
worksheet.go

@@ -30,7 +30,7 @@ type xlsxSheetData struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type xlsxRow struct {
-	R     string  `xml:"r,attr"`
+	R     int  `xml:"r,attr"`
 	Spans string  `xml:"spans,attr"`
 	C     []xlsxC `xml:"c"`
 }

+ 2 - 2
worksheet_test.go

@@ -67,8 +67,8 @@ func TestUnmarshallWorksheet(t *testing.T) {
 		t.Error(fmt.Sprintf("Expected len(worksheet.SheetData.Row) == '2', got %d", worksheet.SheetData.Row))
 	}
 	row := worksheet.SheetData.Row[0]
-	if row.R != "1" {
-		t.Error(fmt.Sprintf("Expected row.r == '1', got %s", row.R))
+	if row.R != 1 {
+		t.Error(fmt.Sprintf("Expected row.r == 1, got %d", row.R))
 	}
 	if len(row.C) != 2 {
 		t.Error(fmt.Sprintf("Expected len(row.C) == 2, got %s", row.C))