summaryrefslogtreecommitdiff
path: root/stream.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2021-07-25 00:43:07 +0800
committerxuri <xuri.me@gmail.com>2021-07-25 00:43:07 +0800
commitf9e9e5d2e07b087e2d4fb2487193aea8c240ab0e (patch)
tree34a83a6be65c8c6e3d2621a74fbc9e553f249567 /stream.go
parent5ce3fe8cb89f5f278b3857bab8e69c117c2a6027 (diff)
This closes #882, support set rows height and hidden row by stream writer
Diffstat (limited to 'stream.go')
-rw-r--r--stream.go39
1 files changed, 36 insertions, 3 deletions
diff --git a/stream.go b/stream.go
index 4a77b56..9939016 100644
--- a/stream.go
+++ b/stream.go
@@ -56,7 +56,8 @@ type StreamWriter struct {
// if err != nil {
// fmt.Println(err)
// }
-// if err := streamWriter.SetRow("A1", []interface{}{excelize.Cell{StyleID: styleID, Value: "Data"}}); err != nil {
+// if err := streamWriter.SetRow("A1", []interface{}{excelize.Cell{StyleID: styleID, Value: "Data"}},
+// excelize.RowOpts{Height: 45, Hidden: false}); err != nil {
// fmt.Println(err)
// }
// for rowID := 2; rowID <= 102400; rowID++ {
@@ -288,13 +289,19 @@ type Cell struct {
Value interface{}
}
+// RowOpts define the options for set row.
+type RowOpts struct {
+ Height float64
+ Hidden bool
+}
+
// SetRow writes an array to stream rows by giving a worksheet name, starting
// coordinate and a pointer to an array of values. Note that you must call the
// 'Flush' method to end the streaming writing process.
//
// As a special case, if Cell is used as a value, then the Cell.StyleID will be
// applied to that cell.
-func (sw *StreamWriter) SetRow(axis string, values []interface{}) error {
+func (sw *StreamWriter) SetRow(axis string, values []interface{}, opts ...RowOpts) error {
col, row, err := CellNameToCoordinates(axis)
if err != nil {
return err
@@ -306,7 +313,11 @@ func (sw *StreamWriter) SetRow(axis string, values []interface{}) error {
_, _ = sw.rawData.WriteString(`<sheetData>`)
sw.sheetWritten = true
}
- fmt.Fprintf(&sw.rawData, `<row r="%d">`, row)
+ attrs, err := marshalRowAttrs(opts...)
+ if err != nil {
+ return err
+ }
+ fmt.Fprintf(&sw.rawData, `<row r="%d"%s>`, row, attrs)
for i, val := range values {
axis, err := CoordinatesToCellName(col+i, row)
if err != nil {
@@ -332,6 +343,28 @@ func (sw *StreamWriter) SetRow(axis string, values []interface{}) error {
return sw.rawData.Sync()
}
+// marshalRowAttrs prepare attributes of the row by given options.
+func marshalRowAttrs(opts ...RowOpts) (attrs string, err error) {
+ var opt *RowOpts
+ for _, o := range opts {
+ opt = &o
+ }
+ if opt == nil {
+ return
+ }
+ if opt.Height > MaxRowHeight {
+ err = ErrMaxRowHeight
+ return
+ }
+ if opt.Height > 0 {
+ attrs += fmt.Sprintf(` ht="%v" customHeight="true"`, opt.Height)
+ }
+ if opt.Hidden {
+ attrs += ` hidden="true"`
+ }
+ return
+}
+
// SetColWidth provides a function to set the width of a single column or
// multiple columns for the StreamWriter. Note that you must call
// the 'SetColWidth' function before the 'SetRow' function. For example set