Co-authored-by: Tianzhi Jin <tianzhi.jin@iglooinsure.com>
This commit is contained in:
parent
3f702999e6
commit
74dad51cfc
63
stream.go
63
stream.go
|
@ -302,6 +302,37 @@ type RowOpts struct {
|
||||||
StyleID int
|
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
|
// 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
|
// coordinate and a pointer to an array of values. Note that you must call the
|
||||||
// 'Flush' method to end the streaming writing process.
|
// '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.rawData.WriteString(`<sheetData>`)
|
||||||
sw.sheetWritten = true
|
sw.sheetWritten = true
|
||||||
}
|
}
|
||||||
attrs, err := marshalRowAttrs(opts...)
|
options := parseRowOpts(opts...)
|
||||||
|
attrs, err := options.marshalAttrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
for i, val := range values {
|
||||||
if val == nil {
|
if val == nil {
|
||||||
continue
|
continue
|
||||||
|
@ -333,7 +365,7 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c := xlsxC{R: ref}
|
c := xlsxC{R: ref, S: options.StyleID}
|
||||||
if v, ok := val.(Cell); ok {
|
if v, ok := val.(Cell); ok {
|
||||||
c.S = v.StyleID
|
c.S = v.StyleID
|
||||||
val = v.Value
|
val = v.Value
|
||||||
|
@ -353,31 +385,6 @@ func (sw *StreamWriter) SetRow(cell string, values []interface{}, opts ...RowOpt
|
||||||
return sw.rawData.Sync()
|
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
|
// SetColWidth provides a function to set the width of a single column or
|
||||||
// multiple columns for the StreamWriter. Note that you must call
|
// multiple columns for the StreamWriter. Note that you must call
|
||||||
// the 'SetColWidth' function before the 'SetRow' function. For example set
|
// the 'SetColWidth' function before the 'SetRow' function. For example set
|
||||||
|
|
|
@ -55,7 +55,7 @@ func TestStreamWriter(t *testing.T) {
|
||||||
// Test set cell with style.
|
// Test set cell with style.
|
||||||
styleID, err := file.NewStyle(&Style{Font: &Font{Color: "#777777"}})
|
styleID, err := file.NewStyle(&Style{Font: &Font{Color: "#777777"}})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
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("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("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("A6", []interface{}{time.Now()}))
|
||||||
assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: 20, Hidden: true, StyleID: styleID}))
|
assert.NoError(t, streamWriter.SetRow("A7", nil, RowOpts{Height: 20, Hidden: true, StyleID: styleID}))
|
||||||
|
@ -201,7 +201,14 @@ func TestNewStreamWriter(t *testing.T) {
|
||||||
assert.EqualError(t, err, "sheet SheetN does not exist")
|
assert.EqualError(t, err, "sheet SheetN does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetRow(t *testing.T) {
|
func TestStreamMarshalAttrs(t *testing.T) {
|
||||||
|
var r *RowOpts
|
||||||
|
attrs, err := r.marshalAttrs()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Empty(t, attrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamSetRow(t *testing.T) {
|
||||||
// Test error exceptions
|
// Test error exceptions
|
||||||
file := NewFile()
|
file := NewFile()
|
||||||
streamWriter, err := file.NewStreamWriter("Sheet1")
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
||||||
|
@ -209,7 +216,7 @@ func TestSetRow(t *testing.T) {
|
||||||
assert.EqualError(t, streamWriter.SetRow("A", []interface{}{}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
assert.EqualError(t, streamWriter.SetRow("A", []interface{}{}), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetRowNilValues(t *testing.T) {
|
func TestStreamSetRowNilValues(t *testing.T) {
|
||||||
file := NewFile()
|
file := NewFile()
|
||||||
streamWriter, err := file.NewStreamWriter("Sheet1")
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@ -220,7 +227,36 @@ func TestSetRowNilValues(t *testing.T) {
|
||||||
assert.NotEqual(t, ws.SheetData.Row[0].C[0].XMLName.Local, "c")
|
assert.NotEqual(t, ws.SheetData.Row[0].C[0].XMLName.Local, "c")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetCellValFunc(t *testing.T) {
|
func TestStreamSetRowWithStyle(t *testing.T) {
|
||||||
|
file := NewFile()
|
||||||
|
zeroStyleID := 0
|
||||||
|
grayStyleID, err := file.NewStyle(&Style{Font: &Font{Color: "#777777"}})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
blueStyleID, err := file.NewStyle(&Style{Font: &Font{Color: "#0000FF"}})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NoError(t, streamWriter.SetRow("A1", []interface{}{
|
||||||
|
"value1",
|
||||||
|
Cell{Value: "value2"},
|
||||||
|
&Cell{Value: "value2"},
|
||||||
|
Cell{StyleID: blueStyleID, Value: "value3"},
|
||||||
|
&Cell{StyleID: blueStyleID, Value: "value3"},
|
||||||
|
}, RowOpts{StyleID: grayStyleID}))
|
||||||
|
err = streamWriter.Flush()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
ws, err := file.workSheetReader("Sheet1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, grayStyleID, ws.SheetData.Row[0].C[0].S)
|
||||||
|
assert.Equal(t, zeroStyleID, ws.SheetData.Row[0].C[1].S)
|
||||||
|
assert.Equal(t, zeroStyleID, ws.SheetData.Row[0].C[2].S)
|
||||||
|
assert.Equal(t, blueStyleID, ws.SheetData.Row[0].C[3].S)
|
||||||
|
assert.Equal(t, blueStyleID, ws.SheetData.Row[0].C[4].S)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamSetCellValFunc(t *testing.T) {
|
||||||
f := NewFile()
|
f := NewFile()
|
||||||
sw, err := f.NewStreamWriter("Sheet1")
|
sw, err := f.NewStreamWriter("Sheet1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
Loading…
Reference in New Issue