GetCols support the row element without r attribute in the worksheet
This commit is contained in:
parent
48f19f60aa
commit
1cbb05d497
|
@ -110,6 +110,9 @@ func TestGetCellValue(t *testing.T) {
|
||||||
assert.Equal(t, cell, value)
|
assert.Equal(t, cell, value)
|
||||||
assert.NoError(t, err)
|
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) {
|
func TestGetCellFormula(t *testing.T) {
|
||||||
|
|
31
col.go
31
col.go
|
@ -48,8 +48,8 @@ type Cols struct {
|
||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
// for _, col := range cols {
|
// for _, col := range cols {
|
||||||
// for _, colCell := range col {
|
// for _, rowCell := range col {
|
||||||
// fmt.Println(colCell, "\t")
|
// fmt.Print(rowCell, "\t")
|
||||||
// }
|
// }
|
||||||
// fmt.Println()
|
// fmt.Println()
|
||||||
// }
|
// }
|
||||||
|
@ -99,12 +99,24 @@ func (cols *Cols) Rows() ([]string, error) {
|
||||||
switch startElement := token.(type) {
|
switch startElement := token.(type) {
|
||||||
case xml.StartElement:
|
case xml.StartElement:
|
||||||
inElement = startElement.Name.Local
|
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" {
|
if inElement == "c" {
|
||||||
|
cellCol++
|
||||||
for _, attr := range startElement.Attr {
|
for _, attr := range startElement.Attr {
|
||||||
if attr.Name.Local == "r" {
|
if attr.Name.Local == "r" {
|
||||||
if cellCol, cellRow, err = CellNameToCoordinates(attr.Value); err != nil {
|
if cellCol, cellRow, err = CellNameToCoordinates(attr.Value); err != nil {
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
blank := cellRow - len(rows)
|
blank := cellRow - len(rows)
|
||||||
for i := 1; i < blank; i++ {
|
for i := 1; i < blank; i++ {
|
||||||
rows = append(rows, "")
|
rows = append(rows, "")
|
||||||
|
@ -118,8 +130,6 @@ func (cols *Cols) Rows() ([]string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return rows, nil
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,7 +164,7 @@ func (f *File) Cols(sheet string) (*Cols, error) {
|
||||||
var (
|
var (
|
||||||
inElement string
|
inElement string
|
||||||
cols Cols
|
cols Cols
|
||||||
cellCol int
|
cellCol, curRow, row int
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
cols.sheetXML = f.readXML(name)
|
cols.sheetXML = f.readXML(name)
|
||||||
|
@ -168,28 +178,33 @@ func (f *File) Cols(sheet string) (*Cols, error) {
|
||||||
case xml.StartElement:
|
case xml.StartElement:
|
||||||
inElement = startElement.Name.Local
|
inElement = startElement.Name.Local
|
||||||
if inElement == "row" {
|
if inElement == "row" {
|
||||||
|
row++
|
||||||
for _, attr := range startElement.Attr {
|
for _, attr := range startElement.Attr {
|
||||||
if attr.Name.Local == "r" {
|
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
|
return &cols, err
|
||||||
}
|
}
|
||||||
|
row = curRow
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cols.totalRow = row
|
||||||
|
cellCol = 0
|
||||||
}
|
}
|
||||||
if inElement == "c" {
|
if inElement == "c" {
|
||||||
|
cellCol++
|
||||||
for _, attr := range startElement.Attr {
|
for _, attr := range startElement.Attr {
|
||||||
if attr.Name.Local == "r" {
|
if attr.Name.Local == "r" {
|
||||||
if cellCol, _, err = CellNameToCoordinates(attr.Value); err != nil {
|
if cellCol, _, err = CellNameToCoordinates(attr.Value); err != nil {
|
||||||
return &cols, err
|
return &cols, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if cellCol > cols.totalCol {
|
if cellCol > cols.totalCol {
|
||||||
cols.totalCol = cellCol
|
cols.totalCol = cellCol
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
cols.f = f
|
cols.f = f
|
||||||
cols.sheet = trimSheetName(sheet)
|
cols.sheet = trimSheetName(sheet)
|
||||||
return &cols, nil
|
return &cols, nil
|
||||||
|
|
14
rows.go
14
rows.go
|
@ -32,7 +32,7 @@ import (
|
||||||
// }
|
// }
|
||||||
// for _, row := range rows {
|
// for _, row := range rows {
|
||||||
// for _, colCell := range row {
|
// for _, colCell := range row {
|
||||||
// fmt.Println(colCell, "\t")
|
// fmt.Print(colCell, "\t")
|
||||||
// }
|
// }
|
||||||
// fmt.Println()
|
// fmt.Println()
|
||||||
// }
|
// }
|
||||||
|
@ -111,6 +111,7 @@ func (rows *Rows) Columns() ([]string, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if inElement == "c" {
|
if inElement == "c" {
|
||||||
|
cellCol++
|
||||||
colCell := xlsxC{}
|
colCell := xlsxC{}
|
||||||
_ = rows.decoder.DecodeElement(&colCell, &startElement)
|
_ = rows.decoder.DecodeElement(&colCell, &startElement)
|
||||||
if colCell.R != "" {
|
if colCell.R != "" {
|
||||||
|
@ -118,8 +119,6 @@ func (rows *Rows) Columns() ([]string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return columns, err
|
return columns, err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
cellCol++
|
|
||||||
}
|
}
|
||||||
blank := cellCol - len(columns)
|
blank := cellCol - len(columns)
|
||||||
for i := 1; i < blank; i++ {
|
for i := 1; i < blank; i++ {
|
||||||
|
@ -179,7 +178,7 @@ func (f *File) Rows(sheet string) (*Rows, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
inElement string
|
inElement string
|
||||||
row, curRow int
|
row int
|
||||||
rows Rows
|
rows Rows
|
||||||
)
|
)
|
||||||
decoder := f.xmlNewDecoder(bytes.NewReader(f.readXML(name)))
|
decoder := f.xmlNewDecoder(bytes.NewReader(f.readXML(name)))
|
||||||
|
@ -192,18 +191,15 @@ func (f *File) Rows(sheet string) (*Rows, error) {
|
||||||
case xml.StartElement:
|
case xml.StartElement:
|
||||||
inElement = startElement.Name.Local
|
inElement = startElement.Name.Local
|
||||||
if inElement == "row" {
|
if inElement == "row" {
|
||||||
|
row++
|
||||||
for _, attr := range startElement.Attr {
|
for _, attr := range startElement.Attr {
|
||||||
if attr.Name.Local == "r" {
|
if attr.Name.Local == "r" {
|
||||||
curRow, err = strconv.Atoi(attr.Value)
|
row, err = strconv.Atoi(attr.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &rows, err
|
return &rows, err
|
||||||
}
|
}
|
||||||
row = curRow
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(startElement.Attr) == 0 {
|
|
||||||
row++
|
|
||||||
}
|
|
||||||
rows.totalRow = row
|
rows.totalRow = row
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
Loading…
Reference in New Issue