From f9e9e5d2e07b087e2d4fb2487193aea8c240ab0e Mon Sep 17 00:00:00 2001 From: xuri Date: Sun, 25 Jul 2021 00:43:07 +0800 Subject: This closes #882, support set rows height and hidden row by stream writer --- stream.go | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'stream.go') 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(``) sw.sheetWritten = true } - fmt.Fprintf(&sw.rawData, ``, row) + attrs, err := marshalRowAttrs(opts...) + if err != nil { + return err + } + fmt.Fprintf(&sw.rawData, ``, 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 -- cgit v1.2.1