已验证 提交 92c8626f 编写于 作者: xurime's avatar xurime

Fixed #732, support single line with repeated row element in the sheet data

上级 2be4bfd4
...@@ -136,8 +136,9 @@ func TestSetCellBool(t *testing.T) { ...@@ -136,8 +136,9 @@ func TestSetCellBool(t *testing.T) {
func TestGetCellValue(t *testing.T) { func TestGetCellValue(t *testing.T) {
// Test get cell value without r attribute of the row. // Test get cell value without r attribute of the row.
f := NewFile() f := NewFile()
sheetData := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>%s</sheetData></worksheet>`
delete(f.Sheet, "xl/worksheets/sheet1.xml") delete(f.Sheet, "xl/worksheets/sheet1.xml")
f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData><row r="3"><c t="str"><v>A3</v></c></row><row><c t="str"><v>A4</v></c><c t="str"><v>B4</v></c></row><row r="7"><c t="str"><v>A7</v></c><c t="str"><v>B7</v></c></row><row><c t="str"><v>A8</v></c><c t="str"><v>B8</v></c></row></sheetData></worksheet>`) f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `<row r="3"><c t="str"><v>A3</v></c></row><row><c t="str"><v>A4</v></c><c t="str"><v>B4</v></c></row><row r="7"><c t="str"><v>A7</v></c><c t="str"><v>B7</v></c></row><row><c t="str"><v>A8</v></c><c t="str"><v>B8</v></c></row>`))
f.checked = nil f.checked = nil
cells := []string{"A3", "A4", "B4", "A7", "B7"} cells := []string{"A3", "A4", "B4", "A7", "B7"}
rows, err := f.GetRows("Sheet1") rows, err := f.GetRows("Sheet1")
...@@ -151,6 +152,24 @@ func TestGetCellValue(t *testing.T) { ...@@ -151,6 +152,24 @@ func TestGetCellValue(t *testing.T) {
cols, err := f.GetCols("Sheet1") cols, err := f.GetCols("Sheet1")
assert.Equal(t, [][]string{{"", "", "A3", "A4", "", "", "A7", "A8"}, {"", "", "", "B4", "", "", "B7", "B8"}}, cols) assert.Equal(t, [][]string{{"", "", "A3", "A4", "", "", "A7", "A8"}, {"", "", "", "B4", "", "", "B7", "B8"}}, cols)
assert.NoError(t, err) assert.NoError(t, err)
delete(f.Sheet, "xl/worksheets/sheet1.xml")
f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `<row r="2"><c r="A2" t="str"><v>A2</v></c></row><row r="2"><c r="B2" t="str"><v>B2</v></c></row>`))
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, `<row r="2"><c r="A2" t="str"><v>A2</v></c></row><row r="2"><c r="B2" t="str"><v>B2</v></c></row>`))
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, `<row r="1"><c r="A1" t="str"><v>A1</v></c></row><row r="1"><c r="B1" t="str"><v>B1</v></c></row>`))
f.checked = nil
rows, err = f.GetRows("Sheet1")
assert.Equal(t, [][]string{{"A1", "B1"}}, rows)
assert.NoError(t, err)
} }
func TestGetCellFormula(t *testing.T) { func TestGetCellFormula(t *testing.T) {
......
...@@ -103,10 +103,9 @@ func (cols *Cols) Rows() ([]string, error) { ...@@ -103,10 +103,9 @@ func (cols *Cols) Rows() ([]string, error) {
if inElement == "row" { if inElement == "row" {
cellCol = 0 cellCol = 0
cellRow++ cellRow++
for _, attr := range startElement.Attr { attrR, _ := attrValToInt("r", startElement.Attr)
if attr.Name.Local == "r" { if attrR != 0 {
cellRow, _ = strconv.Atoi(attr.Value) cellRow = attrR
}
} }
} }
if inElement == "c" { if inElement == "c" {
......
...@@ -219,11 +219,17 @@ func checkSheet(ws *xlsxWorksheet) { ...@@ -219,11 +219,17 @@ func checkSheet(ws *xlsxWorksheet) {
row = r.R row = r.R
continue continue
} }
if r.R != row {
row++ row++
} }
}
sheetData := xlsxSheetData{Row: make([]xlsxRow, row)} sheetData := xlsxSheetData{Row: make([]xlsxRow, row)}
row = 0 row = 0
for _, r := range ws.SheetData.Row { 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 { if r.R != 0 {
sheetData.Row[r.R-1] = r sheetData.Row[r.R-1] = r
row = r.R row = r.R
......
...@@ -81,7 +81,7 @@ func (rows *Rows) Columns() ([]string, error) { ...@@ -81,7 +81,7 @@ func (rows *Rows) Columns() ([]string, error) {
var ( var (
err error err error
inElement string inElement string
row, cellCol int attrR, cellCol, row int
columns []string columns []string
) )
...@@ -99,26 +99,21 @@ func (rows *Rows) Columns() ([]string, error) { ...@@ -99,26 +99,21 @@ func (rows *Rows) Columns() ([]string, error) {
case xml.StartElement: case xml.StartElement:
inElement = startElement.Name.Local inElement = startElement.Name.Local
if inElement == "row" { if inElement == "row" {
for _, attr := range startElement.Attr { row++
if attr.Name.Local == "r" { if attrR, err = attrValToInt("r", startElement.Attr); attrR != 0 {
row, err = strconv.Atoi(attr.Value) row = attrR
if err != nil {
return columns, err
} }
if row > rows.curRow { if row > rows.curRow {
rows.stashRow = row - 1 rows.stashRow = row - 1
return columns, err return columns, err
} }
} }
}
}
if inElement == "c" { if inElement == "c" {
cellCol++ cellCol++
colCell := xlsxC{} colCell := xlsxC{}
_ = rows.decoder.DecodeElement(&colCell, &startElement) _ = rows.decoder.DecodeElement(&colCell, &startElement)
if colCell.R != "" { if colCell.R != "" {
cellCol, _, err = CellNameToCoordinates(colCell.R) if cellCol, _, err = CellNameToCoordinates(colCell.R); err != nil {
if err != nil {
return columns, err return columns, err
} }
} }
...@@ -128,7 +123,10 @@ func (rows *Rows) Columns() ([]string, error) { ...@@ -128,7 +123,10 @@ func (rows *Rows) Columns() ([]string, error) {
} }
case xml.EndElement: case xml.EndElement:
inElement = startElement.Name.Local inElement = startElement.Name.Local
if inElement == "row" { if row == 0 {
row = rows.curRow
}
if inElement == "row" && row+1 < rows.curRow {
return columns, err return columns, err
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册