summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2021-09-29 23:08:17 +0800
committerxuri <xuri.me@gmail.com>2021-09-29 23:08:17 +0800
commite52e75528260745d87fb0962fe239f54c1b5b390 (patch)
tree1637aaa85d39150ed032770d0dee7b40a0d82a5e
parent2d8b5b1885b3d5cd14c974df61a3d0d757efd7bd (diff)
Now support set row style in the stream writer
-rw-r--r--stream.go17
-rw-r--r--stream_test.go2
2 files changed, 15 insertions, 4 deletions
diff --git a/stream.go b/stream.go
index 125b58c..65d6b72 100644
--- a/stream.go
+++ b/stream.go
@@ -84,6 +84,12 @@ type StreamWriter struct {
// excelize.Cell{Value: 2},
// excelize.Cell{Formula: "SUM(A1,B1)"}});
//
+// Set cell value and rows style for a worksheet with stream writer:
+//
+// err := streamWriter.SetRow("A1", []interface{}{
+// excelize.Cell{Value: 1}},
+// excelize.RowOpts{StyleID: styleID, Height: 20, Hidden: false});
+//
func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) {
sheetID := f.getSheetID(sheet)
if sheetID == -1 {
@@ -289,10 +295,12 @@ type Cell struct {
Value interface{}
}
-// RowOpts define the options for set row.
+// RowOpts define the options for the set row, it can be used directly in
+// StreamWriter.SetRow to specify the style and properties of the row.
type RowOpts struct {
- Height float64
- Hidden bool
+ Height float64
+ Hidden bool
+ StyleID int
}
// SetRow writes an array to stream rows by giving a worksheet name, starting
@@ -356,6 +364,9 @@ func marshalRowAttrs(opts ...RowOpts) (attrs string, err error) {
err = ErrMaxRowHeight
return
}
+ if opt.StyleID > 0 {
+ attrs += fmt.Sprintf(` s="%d" customFormat="true"`, opt.StyleID)
+ }
if opt.Height > 0 {
attrs += fmt.Sprintf(` ht="%v" customHeight="true"`, opt.Height)
}
diff --git a/stream_test.go b/stream_test.go
index 6cfed07..52cee4d 100644
--- a/stream_test.go
+++ b/stream_test.go
@@ -55,7 +55,7 @@ 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)"}}), RowOpts{Height: 45})
+ assert.NoError(t, streamWriter.SetRow("A4", []interface{}{Cell{StyleID: styleID}, Cell{Formula: "SUM(A10,B10)"}}), RowOpts{Height: 45, StyleID: styleID})
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}))