summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--stream.go39
-rw-r--r--stream_test.go4
2 files changed, 39 insertions, 4 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
diff --git a/stream_test.go b/stream_test.go
index f911ccc..fda44fb 100644
--- a/stream_test.go
+++ b/stream_test.go
@@ -55,9 +55,11 @@ func TestStreamWriter(t *testing.T) {
// Test set cell with style.
styleID, err := file.NewStyle(`{"font":{"color":"#777777"}}`)
assert.NoError(t, err)
- assert.NoError(t, streamWriter.SetRow("A4", []interface{}{Cell{StyleID: styleID}, Cell{Formula: "SUM(A10,B10)"}}))
+ assert.NoError(t, streamWriter.SetRow("A4", []interface{}{Cell{StyleID: styleID}, Cell{Formula: "SUM(A10,B10)"}}), RowOpts{Height: 45})
assert.NoError(t, streamWriter.SetRow("A5", []interface{}{&Cell{StyleID: styleID, Value: "cell"}, &Cell{Formula: "SUM(A10,B10)"}}))
assert.NoError(t, streamWriter.SetRow("A6", []interface{}{time.Now()}))
+ assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Hidden: true}))
+ assert.EqualError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: MaxRowHeight + 1}), ErrMaxRowHeight.Error())
for rowID := 10; rowID <= 51200; rowID++ {
row := make([]interface{}, 50)