Skip to content

Commit c8e5b9c

Browse files
author
Thomas Charbonnel
committed
qax-os#1296 add GetStyleID to Rows
Signed-off-by: Thomas Charbonnel <[email protected]>
1 parent b133399 commit c8e5b9c

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

rows.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Rows struct {
8080
sst *xlsxSST
8181
decoder *xml.Decoder
8282
token xml.Token
83+
rowOpts RowOpts
8384
}
8485

8586
// Next will return true if find the next row element.
@@ -101,6 +102,17 @@ func (rows *Rows) Next() bool {
101102
rows.curRow = rowNum
102103
}
103104
rows.token = token
105+
ro := RowOpts{}
106+
if styleID, err := attrValToInt("s", xmlElement.Attr); err == nil && styleID > 0 && styleID < MaxCellStyles {
107+
ro.StyleID = styleID
108+
}
109+
if hidden, err := attrValToBool("hidden", xmlElement.Attr); err == nil {
110+
ro.Hidden = hidden
111+
}
112+
if height, err := attrValToFloat("ht", xmlElement.Attr); err == nil {
113+
ro.Height = height
114+
}
115+
rows.rowOpts = ro
104116
return true
105117
}
106118
case xml.EndElement:
@@ -111,6 +123,13 @@ func (rows *Rows) Next() bool {
111123
}
112124
}
113125

126+
// GetStyleID will return the RowOpts of the current row.
127+
//
128+
// rowOpts := rows.GetRowOpts()
129+
func (rows *Rows) GetRowOpts() RowOpts {
130+
return rows.rowOpts
131+
}
132+
114133
// Error will return the error when the error occurs.
115134
func (rows *Rows) Error() error {
116135
return rows.err

rows_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ func TestRowsIterator(t *testing.T) {
9696
assert.Equal(t, expectedNumRow, rowCount)
9797
}
9898

99+
func TestRowsGetRowOpts(t *testing.T) {
100+
sheetName := "Sheet2"
101+
expectedRowStyleID1 := RowOpts{Height: 17.0, Hidden: false, StyleID: 1}
102+
expectedRowStyleID2 := RowOpts{Height: 17.0, Hidden: false, StyleID: 0}
103+
expectedRowStyleID3 := RowOpts{Height: 17.0, Hidden: false, StyleID: 2}
104+
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
105+
require.NoError(t, err)
106+
107+
rows, err := f.Rows(sheetName)
108+
require.NoError(t, err)
109+
110+
rows.Next()
111+
got := rows.GetRowOpts()
112+
assert.Equal(t, expectedRowStyleID1, got)
113+
rows.Next()
114+
got = rows.GetRowOpts()
115+
assert.Equal(t, expectedRowStyleID2, got)
116+
rows.Next()
117+
got = rows.GetRowOpts()
118+
assert.Equal(t, expectedRowStyleID3, got)
119+
}
120+
99121
func TestRowsError(t *testing.T) {
100122
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
101123
if !assert.NoError(t, err) {

sheet.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,34 @@ func attrValToInt(name string, attrs []xml.Attr) (val int, err error) {
928928
return
929929
}
930930

931+
// attrValToFloat provides a function to convert the local names to a float64
932+
// by given XML attributes and specified names.
933+
func attrValToFloat(name string, attrs []xml.Attr) (val float64, err error) {
934+
for _, attr := range attrs {
935+
if attr.Name.Local == name {
936+
val, err = strconv.ParseFloat(attr.Value, 64)
937+
if err != nil {
938+
return
939+
}
940+
}
941+
}
942+
return
943+
}
944+
945+
// attrValToBool provides a function to convert the local names to a boot
946+
// by given XML attributes and specified names.
947+
func attrValToBool(name string, attrs []xml.Attr) (val bool, err error) {
948+
for _, attr := range attrs {
949+
if attr.Name.Local == name {
950+
val, err = strconv.ParseBool(attr.Value)
951+
if err != nil {
952+
return
953+
}
954+
}
955+
}
956+
return
957+
}
958+
931959
// SetHeaderFooter provides a function to set headers and footers by given
932960
// worksheet name and the control characters.
933961
//

test/Book1.xlsx

-287 Bytes
Binary file not shown.

xmlDrawing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const (
107107
MaxFieldLength = 255
108108
MaxColumnWidth = 255
109109
MaxRowHeight = 409
110+
MaxCellStyles = 64000
110111
MinFontSize = 1
111112
TotalRows = 1048576
112113
MinColumns = 1

0 commit comments

Comments
 (0)