Skip to content

Commit

Permalink
qax-os#1296 add GetStyleID to Rows
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Charbonnel <github@charbonnel.email>
  • Loading branch information
Thomas Charbonnel committed Aug 4, 2022
1 parent b133399 commit c8e5b9c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Rows struct {
sst *xlsxSST
decoder *xml.Decoder
token xml.Token
rowOpts RowOpts
}

// Next will return true if find the next row element.
Expand All @@ -101,6 +102,17 @@ func (rows *Rows) Next() bool {
rows.curRow = rowNum
}
rows.token = token
ro := RowOpts{}
if styleID, err := attrValToInt("s", xmlElement.Attr); err == nil && styleID > 0 && styleID < MaxCellStyles {
ro.StyleID = styleID
}
if hidden, err := attrValToBool("hidden", xmlElement.Attr); err == nil {
ro.Hidden = hidden
}
if height, err := attrValToFloat("ht", xmlElement.Attr); err == nil {
ro.Height = height
}
rows.rowOpts = ro
return true
}
case xml.EndElement:
Expand All @@ -111,6 +123,13 @@ func (rows *Rows) Next() bool {
}
}

// GetStyleID will return the RowOpts of the current row.
//
// rowOpts := rows.GetRowOpts()
func (rows *Rows) GetRowOpts() RowOpts {
return rows.rowOpts
}

// Error will return the error when the error occurs.
func (rows *Rows) Error() error {
return rows.err
Expand Down
22 changes: 22 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ func TestRowsIterator(t *testing.T) {
assert.Equal(t, expectedNumRow, rowCount)
}

func TestRowsGetRowOpts(t *testing.T) {
sheetName := "Sheet2"
expectedRowStyleID1 := RowOpts{Height: 17.0, Hidden: false, StyleID: 1}
expectedRowStyleID2 := RowOpts{Height: 17.0, Hidden: false, StyleID: 0}
expectedRowStyleID3 := RowOpts{Height: 17.0, Hidden: false, StyleID: 2}
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
require.NoError(t, err)

rows, err := f.Rows(sheetName)
require.NoError(t, err)

rows.Next()
got := rows.GetRowOpts()
assert.Equal(t, expectedRowStyleID1, got)
rows.Next()
got = rows.GetRowOpts()
assert.Equal(t, expectedRowStyleID2, got)
rows.Next()
got = rows.GetRowOpts()
assert.Equal(t, expectedRowStyleID3, got)
}

func TestRowsError(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {
Expand Down
28 changes: 28 additions & 0 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,34 @@ func attrValToInt(name string, attrs []xml.Attr) (val int, err error) {
return
}

// attrValToFloat provides a function to convert the local names to a float64
// by given XML attributes and specified names.
func attrValToFloat(name string, attrs []xml.Attr) (val float64, err error) {
for _, attr := range attrs {
if attr.Name.Local == name {
val, err = strconv.ParseFloat(attr.Value, 64)
if err != nil {
return
}
}
}
return
}

// attrValToBool provides a function to convert the local names to a boot
// by given XML attributes and specified names.
func attrValToBool(name string, attrs []xml.Attr) (val bool, err error) {
for _, attr := range attrs {
if attr.Name.Local == name {
val, err = strconv.ParseBool(attr.Value)
if err != nil {
return
}
}
}
return
}

// SetHeaderFooter provides a function to set headers and footers by given
// worksheet name and the control characters.
//
Expand Down
Binary file modified test/Book1.xlsx
Binary file not shown.
1 change: 1 addition & 0 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const (
MaxFieldLength = 255
MaxColumnWidth = 255
MaxRowHeight = 409
MaxCellStyles = 64000
MinFontSize = 1
TotalRows = 1048576
MinColumns = 1
Expand Down

0 comments on commit c8e5b9c

Please sign in to comment.