Skip to content

Commit cd030d4

Browse files
committed
Improve compatibility with row element with r="0" attribute
1 parent a2d4497 commit cd030d4

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

cell_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,23 @@ func TestGetCellValue(t *testing.T) {
225225
rows, err = f.GetRows("Sheet1")
226226
assert.Equal(t, [][]string{{"A3"}, {"A4", "B4"}, nil, nil, nil, nil, {"A7", "B7"}, {"A8", "B8"}}, rows)
227227
assert.NoError(t, err)
228+
229+
f.Sheet.Delete("xl/worksheets/sheet1.xml")
230+
f.Pkg.Store("xl/worksheets/sheet1.xml", []byte(fmt.Sprintf(sheetData, `<row r="0"><c r="H6" t="str"><v>H6</v></c><c r="A1" t="str"><v>r0A6</v></c><c r="F4" t="str"><v>F4</v></c></row><row><c r="A1" t="str"><v>A6</v></c><c r="B1" t="str"><v>B6</v></c><c r="C1" t="str"><v>C6</v></c></row><row r="3"><c r="A3"><v>100</v></c><c r="B3" t="str"><v>B3</v></c></row>`)))
231+
f.checked = nil
232+
cell, err = f.GetCellValue("Sheet1", "H6")
233+
assert.Equal(t, "H6", cell)
234+
assert.NoError(t, err)
235+
rows, err = f.GetRows("Sheet1")
236+
assert.Equal(t, [][]string{
237+
{"A6", "B6", "C6"},
238+
nil,
239+
{"100", "B3"},
240+
{"", "", "", "", "", "F4"},
241+
nil,
242+
{"", "", "", "", "", "", "", "H6"},
243+
}, rows)
244+
assert.NoError(t, err)
228245
}
229246

230247
func TestGetCellFormula(t *testing.T) {

excelize.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,13 @@ func (f *File) workSheetReader(sheet string) (ws *xlsxWorksheet, err error) {
226226
// continuous in a worksheet of XML.
227227
func checkSheet(ws *xlsxWorksheet) {
228228
var row int
229-
for _, r := range ws.SheetData.Row {
229+
var r0 xlsxRow
230+
for i, r := range ws.SheetData.Row {
231+
if i == 0 && r.R == 0 {
232+
r0 = r
233+
ws.SheetData.Row = ws.SheetData.Row[1:]
234+
continue
235+
}
230236
if r.R != 0 && r.R > row {
231237
row = r.R
232238
continue
@@ -254,7 +260,29 @@ func checkSheet(ws *xlsxWorksheet) {
254260
for i := 1; i <= row; i++ {
255261
sheetData.Row[i-1].R = i
256262
}
257-
ws.SheetData = sheetData
263+
checkSheetR0(ws, &sheetData, &r0)
264+
}
265+
266+
// checkSheetR0 handle the row element with r="0" attribute, cells in this row
267+
// could be disorderly, the cell in this row can be used as the value of
268+
// which cell is empty in the normal rows.
269+
func checkSheetR0(ws *xlsxWorksheet, sheetData *xlsxSheetData, r0 *xlsxRow) {
270+
for _, cell := range r0.C {
271+
if col, row, err := CellNameToCoordinates(cell.R); err == nil {
272+
rows, rowIdx := len(sheetData.Row), row-1
273+
for r := rows; r < row; r++ {
274+
sheetData.Row = append(sheetData.Row, xlsxRow{R: r + 1})
275+
}
276+
columns, colIdx := len(sheetData.Row[rowIdx].C), col-1
277+
for c := columns; c < col; c++ {
278+
sheetData.Row[rowIdx].C = append(sheetData.Row[rowIdx].C, xlsxC{})
279+
}
280+
if !sheetData.Row[rowIdx].C[colIdx].hasValue() {
281+
sheetData.Row[rowIdx].C[colIdx] = cell
282+
}
283+
}
284+
}
285+
ws.SheetData = *sheetData
258286
}
259287

260288
// addRels provides a function to add relationships by given XML path,

0 commit comments

Comments
 (0)