diff --git a/rows.go b/rows.go index 853c8f7df3..9ef52cadd4 100644 --- a/rows.go +++ b/rows.go @@ -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. @@ -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: @@ -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 diff --git a/rows_test.go b/rows_test.go index 4fe28517cd..ec25bd938a 100644 --- a/rows_test.go +++ b/rows_test.go @@ -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) { diff --git a/sheet.go b/sheet.go index 01dd1672b9..e984dbd065 100644 --- a/sheet.go +++ b/sheet.go @@ -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. // diff --git a/test/Book1.xlsx b/test/Book1.xlsx index 6a497e33af..ed3e292954 100644 Binary files a/test/Book1.xlsx and b/test/Book1.xlsx differ diff --git a/xmlDrawing.go b/xmlDrawing.go index 3e54b7207f..04956d5745 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -107,6 +107,7 @@ const ( MaxFieldLength = 255 MaxColumnWidth = 255 MaxRowHeight = 409 + MaxCellStyles = 64000 MinFontSize = 1 TotalRows = 1048576 MinColumns = 1