Skip to content

Commit

Permalink
#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 1, 2022
1 parent fd0eb2b commit 45afab5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rows.go
Expand Up @@ -80,6 +80,7 @@ type Rows struct {
sst *xlsxSST
decoder *xml.Decoder
token xml.Token
styleID int
}

// Next will return true if find the next row element.
Expand All @@ -101,6 +102,9 @@ func (rows *Rows) Next() bool {
rows.curRow = rowNum
}
rows.token = token
if styleID, _ := attrValToInt("s", xmlElement.Attr); styleID != 0 {
rows.styleID = styleID
}
return true
}
case xml.EndElement:
Expand All @@ -111,6 +115,14 @@ func (rows *Rows) Next() bool {
}
}

// GetStyleID provise a function to get the current Rows' style ID. It will
// return 0 when no style has been set.
//
// styleID := rows.GetStyleID()
func (rows *Rows) GetStyleID() int {
return rows.styleID
}

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

func TestRowsGetStyleID(t *testing.T) {
sheetName, expectedRowStyleID := "Sheet2", 1
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
require.NoError(t, err)

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

assert.Equal(t, expectedRowStyleID, rowStyleID)
}

func TestRowsError(t *testing.T) {
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) {
Expand Down
Binary file modified test/Book1.xlsx
Binary file not shown.

0 comments on commit 45afab5

Please sign in to comment.