From 1cbb05d4977fc1c03fa37d704118fd9c722e487d Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 28 Jun 2020 00:02:32 +0800 Subject: [PATCH] GetCols support the row element without r attribute in the worksheet --- cell_test.go | 3 +++ col.go | 55 +++++++++++++++++++++++++++++++++------------------- rows.go | 20 ++++++++----------- 3 files changed, 46 insertions(+), 32 deletions(-) diff --git a/cell_test.go b/cell_test.go index b2b1d54..fb30596 100644 --- a/cell_test.go +++ b/cell_test.go @@ -110,6 +110,9 @@ func TestGetCellValue(t *testing.T) { assert.Equal(t, cell, value) assert.NoError(t, err) } + cols, err := f.GetCols("Sheet1") + assert.Equal(t, [][]string{{"", "", "A3", "A4", "", "", "A7", "A8"}, {"", "", "", "B4", "", "", "B7", "B8"}}, cols) + assert.NoError(t, err) } func TestGetCellFormula(t *testing.T) { diff --git a/col.go b/col.go index 0baa2e4..472106f 100644 --- a/col.go +++ b/col.go @@ -48,8 +48,8 @@ type Cols struct { // return // } // for _, col := range cols { -// for _, colCell := range col { -// fmt.Println(colCell, "\t") +// for _, rowCell := range col { +// fmt.Print(rowCell, "\t") // } // fmt.Println() // } @@ -99,24 +99,34 @@ func (cols *Cols) Rows() ([]string, error) { switch startElement := token.(type) { case xml.StartElement: inElement = startElement.Name.Local + if inElement == "row" { + cellCol = 0 + cellRow++ + for _, attr := range startElement.Attr { + if attr.Name.Local == "r" { + cellRow, _ = strconv.Atoi(attr.Value) + } + } + } if inElement == "c" { + cellCol++ for _, attr := range startElement.Attr { if attr.Name.Local == "r" { if cellCol, cellRow, err = CellNameToCoordinates(attr.Value); err != nil { return rows, err } - blank := cellRow - len(rows) - for i := 1; i < blank; i++ { - rows = append(rows, "") - } - if cellCol == cols.curCol { - colCell := xlsxC{} - _ = decoder.DecodeElement(&colCell, &startElement) - val, _ := colCell.getValueFrom(cols.f, d) - rows = append(rows, val) - } } } + blank := cellRow - len(rows) + for i := 1; i < blank; i++ { + rows = append(rows, "") + } + if cellCol == cols.curCol { + colCell := xlsxC{} + _ = decoder.DecodeElement(&colCell, &startElement) + val, _ := colCell.getValueFrom(cols.f, d) + rows = append(rows, val) + } } } } @@ -152,10 +162,10 @@ func (f *File) Cols(sheet string) (*Cols, error) { f.saveFileList(name, replaceRelationshipsNameSpaceBytes(output)) } var ( - inElement string - cols Cols - cellCol int - err error + inElement string + cols Cols + cellCol, curRow, row int + err error ) cols.sheetXML = f.readXML(name) decoder := f.xmlNewDecoder(bytes.NewReader(cols.sheetXML)) @@ -168,25 +178,30 @@ func (f *File) Cols(sheet string) (*Cols, error) { case xml.StartElement: inElement = startElement.Name.Local if inElement == "row" { + row++ for _, attr := range startElement.Attr { if attr.Name.Local == "r" { - if cols.totalRow, err = strconv.Atoi(attr.Value); err != nil { + if curRow, err = strconv.Atoi(attr.Value); err != nil { return &cols, err } + row = curRow } } + cols.totalRow = row + cellCol = 0 } if inElement == "c" { + cellCol++ for _, attr := range startElement.Attr { if attr.Name.Local == "r" { if cellCol, _, err = CellNameToCoordinates(attr.Value); err != nil { return &cols, err } - if cellCol > cols.totalCol { - cols.totalCol = cellCol - } } } + if cellCol > cols.totalCol { + cols.totalCol = cellCol + } } } } diff --git a/rows.go b/rows.go index 38c1ecc..b89d916 100644 --- a/rows.go +++ b/rows.go @@ -32,7 +32,7 @@ import ( // } // for _, row := range rows { // for _, colCell := range row { -// fmt.Println(colCell, "\t") +// fmt.Print(colCell, "\t") // } // fmt.Println() // } @@ -111,6 +111,7 @@ func (rows *Rows) Columns() ([]string, error) { } } if inElement == "c" { + cellCol++ colCell := xlsxC{} _ = rows.decoder.DecodeElement(&colCell, &startElement) if colCell.R != "" { @@ -118,8 +119,6 @@ func (rows *Rows) Columns() ([]string, error) { if err != nil { return columns, err } - } else { - cellCol++ } blank := cellCol - len(columns) for i := 1; i < blank; i++ { @@ -177,10 +176,10 @@ func (f *File) Rows(sheet string) (*Rows, error) { f.saveFileList(name, replaceRelationshipsNameSpaceBytes(output)) } var ( - err error - inElement string - row, curRow int - rows Rows + err error + inElement string + row int + rows Rows ) decoder := f.xmlNewDecoder(bytes.NewReader(f.readXML(name))) for { @@ -192,18 +191,15 @@ func (f *File) Rows(sheet string) (*Rows, error) { case xml.StartElement: inElement = startElement.Name.Local if inElement == "row" { + row++ for _, attr := range startElement.Attr { if attr.Name.Local == "r" { - curRow, err = strconv.Atoi(attr.Value) + row, err = strconv.Atoi(attr.Value) if err != nil { return &rows, err } - row = curRow } } - if len(startElement.Attr) == 0 { - row++ - } rows.totalRow = row } default: