diff --git a/cell_test.go b/cell_test.go index f7072560a2822d03c1b3553219a18c29a5f94750..c934876edb1eb0b2c2b0bbd81a60ec43450a8980 100644 --- a/cell_test.go +++ b/cell_test.go @@ -136,8 +136,9 @@ func TestSetCellBool(t *testing.T) { func TestGetCellValue(t *testing.T) { // Test get cell value without r attribute of the row. f := NewFile() + sheetData := `%s` delete(f.Sheet, "xl/worksheets/sheet1.xml") - f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`A3A4B4A7B7A8B8`) + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `A3A4B4A7B7A8B8`)) f.checked = nil cells := []string{"A3", "A4", "B4", "A7", "B7"} rows, err := f.GetRows("Sheet1") @@ -151,6 +152,24 @@ func TestGetCellValue(t *testing.T) { cols, err := f.GetCols("Sheet1") assert.Equal(t, [][]string{{"", "", "A3", "A4", "", "", "A7", "A8"}, {"", "", "", "B4", "", "", "B7", "B8"}}, cols) assert.NoError(t, err) + delete(f.Sheet, "xl/worksheets/sheet1.xml") + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `A2B2`)) + f.checked = nil + cell, err := f.GetCellValue("Sheet1", "A2") + assert.Equal(t, "A2", cell) + assert.NoError(t, err) + delete(f.Sheet, "xl/worksheets/sheet1.xml") + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `A2B2`)) + f.checked = nil + rows, err = f.GetRows("Sheet1") + assert.Equal(t, [][]string{nil, {"A2", "B2"}}, rows) + assert.NoError(t, err) + delete(f.Sheet, "xl/worksheets/sheet1.xml") + f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `A1B1`)) + f.checked = nil + rows, err = f.GetRows("Sheet1") + assert.Equal(t, [][]string{{"A1", "B1"}}, rows) + assert.NoError(t, err) } func TestGetCellFormula(t *testing.T) { diff --git a/col.go b/col.go index f3e502cf6297f2641fc83aa736c729940d38436f..5d9122910e9ae5199f6c35694a23557f99ce5257 100644 --- a/col.go +++ b/col.go @@ -103,10 +103,9 @@ func (cols *Cols) Rows() ([]string, error) { if inElement == "row" { cellCol = 0 cellRow++ - for _, attr := range startElement.Attr { - if attr.Name.Local == "r" { - cellRow, _ = strconv.Atoi(attr.Value) - } + attrR, _ := attrValToInt("r", startElement.Attr) + if attrR != 0 { + cellRow = attrR } } if inElement == "c" { diff --git a/excelize.go b/excelize.go index 5069756cf4ac1e968a77d82ef569f15618ad2a48..bcae48103b746f50e56ec9806a3f22255c9d7a80 100644 --- a/excelize.go +++ b/excelize.go @@ -219,11 +219,17 @@ func checkSheet(ws *xlsxWorksheet) { row = r.R continue } - row++ + if r.R != row { + row++ + } } sheetData := xlsxSheetData{Row: make([]xlsxRow, row)} row = 0 for _, r := range ws.SheetData.Row { + if r.R == row { + sheetData.Row[r.R-1].C = append(sheetData.Row[r.R-1].C, r.C...) + continue + } if r.R != 0 { sheetData.Row[r.R-1] = r row = r.R diff --git a/rows.go b/rows.go index 04a06b946dfacd2e72f129c55b6f4e2dc6ebb366..4f93ed1162672795d96039314ec3772e15bed06a 100644 --- a/rows.go +++ b/rows.go @@ -79,10 +79,10 @@ func (rows *Rows) Error() error { // Columns return the current row's column values. func (rows *Rows) Columns() ([]string, error) { var ( - err error - inElement string - row, cellCol int - columns []string + err error + inElement string + attrR, cellCol, row int + columns []string ) if rows.stashRow >= rows.curRow { @@ -99,17 +99,13 @@ func (rows *Rows) Columns() ([]string, error) { case xml.StartElement: inElement = startElement.Name.Local if inElement == "row" { - for _, attr := range startElement.Attr { - if attr.Name.Local == "r" { - row, err = strconv.Atoi(attr.Value) - if err != nil { - return columns, err - } - if row > rows.curRow { - rows.stashRow = row - 1 - return columns, err - } - } + row++ + if attrR, err = attrValToInt("r", startElement.Attr); attrR != 0 { + row = attrR + } + if row > rows.curRow { + rows.stashRow = row - 1 + return columns, err } } if inElement == "c" { @@ -117,8 +113,7 @@ func (rows *Rows) Columns() ([]string, error) { colCell := xlsxC{} _ = rows.decoder.DecodeElement(&colCell, &startElement) if colCell.R != "" { - cellCol, _, err = CellNameToCoordinates(colCell.R) - if err != nil { + if cellCol, _, err = CellNameToCoordinates(colCell.R); err != nil { return columns, err } } @@ -128,7 +123,10 @@ func (rows *Rows) Columns() ([]string, error) { } case xml.EndElement: inElement = startElement.Name.Local - if inElement == "row" { + if row == 0 { + row = rows.curRow + } + if inElement == "row" && row+1 < rows.curRow { return columns, err } }