diff options
author | invzhi <invzhi@protonmail.com> | 2022-09-21 00:29:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-21 00:29:34 +0800 |
commit | 74dad51cfce19c2f67a0ed9fe1479b6d21d767e9 (patch) | |
tree | 1eb1d3e076f6bf4e9f9c2c6753e8120f74105990 /stream.go | |
parent | 3f702999e6bba26afbd2a259f6849e536042ec2e (diff) |
This closes #1354, stream writer will apply style in `RowOpts` for each cell (#1355)
Co-authored-by: Tianzhi Jin <tianzhi.jin@iglooinsure.com>
Diffstat (limited to 'stream.go')
-rw-r--r-- | stream.go | 63 |
1 files changed, 35 insertions, 28 deletions
@@ -302,6 +302,37 @@ type RowOpts struct { StyleID int } +// marshalAttrs prepare attributes of the row. +func (r *RowOpts) marshalAttrs() (attrs string, err error) { + if r == nil { + return + } + if r.Height > MaxRowHeight { + err = ErrMaxRowHeight + return + } + if r.StyleID > 0 { + attrs += fmt.Sprintf(` s="%d" customFormat="true"`, r.StyleID) + } + if r.Height > 0 { + attrs += fmt.Sprintf(` ht="%v" customHeight="true"`, r.Height) + } + if r.Hidden { + attrs += ` hidden="true"` + } + return +} + +// parseRowOpts provides a function to parse the optional settings for +// *StreamWriter.SetRow. +func parseRowOpts(opts ...RowOpts) *RowOpts { + options := &RowOpts{} + for _, opt := range opts { + options = &opt + } + return options +} + // 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. @@ -320,11 +351,12 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt _, _ = sw.rawData.WriteString(`<sheetData>`) sw.sheetWritten = true } - attrs, err := marshalRowAttrs(opts...) + options := parseRowOpts(opts...) + attrs, err := options.marshalAttrs() if err != nil { return err } - fmt.Fprintf(&sw.rawData, `<row r="%d"%s>`, row, attrs) + _, _ = fmt.Fprintf(&sw.rawData, `<row r="%d"%s>`, row, attrs) for i, val := range values { if val == nil { continue @@ -333,7 +365,7 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt if err != nil { return err } - c := xlsxC{R: ref} + c := xlsxC{R: ref, S: options.StyleID} if v, ok := val.(Cell); ok { c.S = v.StyleID val = v.Value @@ -353,31 +385,6 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt return sw.rawData.Sync() } -// marshalRowAttrs prepare attributes of the row by given options. -func marshalRowAttrs(opts ...RowOpts) (attrs string, err error) { - var options *RowOpts - for i := range opts { - options = &opts[i] - } - if options == nil { - return - } - if options.Height > MaxRowHeight { - err = ErrMaxRowHeight - return - } - if options.StyleID > 0 { - attrs += fmt.Sprintf(` s="%d" customFormat="true"`, options.StyleID) - } - if options.Height > 0 { - attrs += fmt.Sprintf(` ht="%v" customHeight="true"`, options.Height) - } - if options.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 |