Skip to content

Commit

Permalink
Merge pull request #4 from tomtwinkle/feat/set-cell-value-async-overr…
Browse files Browse the repository at this point in the history
…ide-style

SetCellVaueAsync override style, SetCellValue override style
  • Loading branch information
tomtwinkle authored Aug 22, 2022
2 parents c0a929c + 6b89887 commit 796eff4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 46 deletions.
26 changes: 17 additions & 9 deletions excelizeam.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type Excelizeam interface {
MergeCell(startColIndex, startRowIndex, endColIndex, endRowIndex int) error

// SetCellValue Set value and style to cell
SetCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, override bool) error
SetCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideValue, overrideStyle bool) error
// SetCellValueAsync Set value and style to cell asynchronously
SetCellValueAsync(colIndex, rowIndex int, value interface{}, style *excelize.Style)
SetCellValueAsync(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideStyle bool)

// SetStyleCell Set style to cell
SetStyleCell(colIndex, rowIndex int, style excelize.Style, override bool) error
Expand All @@ -52,6 +52,10 @@ type Excelizeam interface {
// SetBorderRangeAsync Set border around cell range asynchronously
SetBorderRangeAsync(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange)

// Wait
// Wait for all running asynchronous operations to finish
Wait() error

// Write StreamWriter
Write(w io.Writer) error
}
Expand Down Expand Up @@ -162,26 +166,26 @@ func (e *excelizeam) MergeCell(startColIndex, startRowIndex, endColIndex, endRow
return e.sw.MergeCell(startCell, endCell)
}

func (e *excelizeam) SetCellValueAsync(colIndex, rowIndex int, value interface{}, style *excelize.Style) {
func (e *excelizeam) SetCellValueAsync(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideStyle bool) {
e.eg.Go(func() error {
return e.setCellValue(colIndex, rowIndex, value, style, false)
return e.setCellValue(colIndex, rowIndex, value, style, false, overrideStyle)
})
}

func (e *excelizeam) SetCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, override bool) error {
func (e *excelizeam) SetCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideValue bool, overrideStyle bool) error {
if err := e.eg.Wait(); err != nil {
return err
}
return e.setCellValue(colIndex, rowIndex, value, style, override)
return e.setCellValue(colIndex, rowIndex, value, style, overrideValue, overrideStyle)
}

func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, override bool) error {
func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideValue bool, overrideStyle bool) error {
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)

if cached, ok := e.cellStore.Load(key); ok {
cell := cached.(*Cell)
if cell.Value != nil && value != nil && !override {
if cell.Value != nil && value != nil && !overrideValue {
return ErrOverrideCellValue
}
if value != nil {
Expand All @@ -190,7 +194,7 @@ func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, sty

if style != nil {
if cell.StyleID > 0 {
if !override {
if !overrideStyle {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(cell.StyleID, *style)
Expand Down Expand Up @@ -669,6 +673,10 @@ func (e *excelizeam) checkMaxIndex(colIndex, rowIndex int) {
}
}

func (e *excelizeam) Wait() error {
return e.eg.Wait()
}

func (e *excelizeam) Write(w io.Writer) error {
if err := e.writeStream(); err != nil {
return err
Expand Down
102 changes: 65 additions & 37 deletions excelizeam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,41 @@ func TestExcelizeam_Sync(t *testing.T) {
}{
"SetCellValue-with_not_style": {
testFunc: func(w excelizeam.Excelizeam) error {
return w.SetCellValue(1, 1, "test", nil, false)
return w.SetCellValue(1, 1, "test", nil, false, false)
},
},
"SetCellValue-with_not_style_override": {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetCellValue(1, 1, "test1", nil, false); err != nil {
if err := w.SetCellValue(1, 1, "test1", nil, false, false); err != nil {
return err
}
// can override value
if err := w.SetCellValue(1, 1, "test2", nil, true); err != nil {
if err := w.SetCellValue(1, 1, "test2", nil, true, true); err != nil {
return err
}
return nil
},
},
"SetCellValue-with_not_style_override_error": {
"SetCellValue-with_not_style_override_style_error": {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetCellValue(1, 1, "test1", nil, false); err != nil {
if err := w.SetCellValue(1, 1, "test1", &excelize.Style{Font: &excelize.Font{Size: 12}}, false, false); err != nil {
return err
}
// can override value
if err := w.SetCellValue(1, 1, "test2", nil, false); err != nil {
if err := w.SetCellValue(1, 1, "test2", &excelize.Style{Font: &excelize.Font{Size: 13}}, true, false); err != nil {
return err
}
return nil
},
wantErr: excelizeam.ErrOverrideCellStyle,
},
"SetCellValue-with_not_style_override_value_error": {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetCellValue(1, 1, "test1", &excelize.Style{Font: &excelize.Font{Size: 12}}, false, false); err != nil {
return err
}
// can override value
if err := w.SetCellValue(1, 1, "test2", &excelize.Style{Font: &excelize.Font{Size: 13}}, false, true); err != nil {
return err
}
return nil
Expand All @@ -54,7 +67,7 @@ func TestExcelizeam_Sync(t *testing.T) {
testFunc: func(w excelizeam.Excelizeam) error {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
for colIdx := 1; colIdx <= 10; colIdx++ {
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false); err != nil {
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false, false); err != nil {
return err
}
}
Expand All @@ -72,7 +85,7 @@ func TestExcelizeam_Sync(t *testing.T) {
if colIdx%2 == 0 {
continue
}
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false); err != nil {
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false, false); err != nil {
return err
}
}
Expand All @@ -84,7 +97,7 @@ func TestExcelizeam_Sync(t *testing.T) {
testFunc: func(w excelizeam.Excelizeam) error {
for colIdx := 1; colIdx <= 10; colIdx++ {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false); err != nil {
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false, false); err != nil {
return err
}
}
Expand All @@ -103,7 +116,7 @@ func TestExcelizeam_Sync(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
}, false)
}, false, false)
},
},
"SetCellValue-with_style_border_fill_font_alignment_override_border_top": {
Expand All @@ -117,14 +130,14 @@ func TestExcelizeam_Sync(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
}, false); err != nil {
}, false, false); err != nil {
return err
}
if err := w.SetCellValue(2, 2, "test2", &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionTop, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
}, true); err != nil {
}, true, true); err != nil {
return err
}
return nil
Expand All @@ -141,14 +154,14 @@ func TestExcelizeam_Sync(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
}, false); err != nil {
}, false, false); err != nil {
return err
}
if err := w.SetCellValue(2, 2, "test2", &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionBottom, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
}, true); err != nil {
}, true, true); err != nil {
return err
}
return nil
Expand All @@ -165,14 +178,14 @@ func TestExcelizeam_Sync(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
}, false); err != nil {
}, false, false); err != nil {
return err
}
if err := w.SetCellValue(2, 2, "test2", &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionLeft, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
}, true); err != nil {
}, true, true); err != nil {
return err
}
return nil
Expand All @@ -189,14 +202,14 @@ func TestExcelizeam_Sync(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
}, false); err != nil {
}, false, false); err != nil {
return err
}
if err := w.SetCellValue(2, 2, "test2", &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
}, true); err != nil {
}, true, true); err != nil {
return err
}
return nil
Expand All @@ -206,14 +219,14 @@ func TestExcelizeam_Sync(t *testing.T) {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetCellValue(2, 2, "test1", &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
}, false); err != nil {
}, false, false); err != nil {
return err
}
if err := w.SetCellValue(2, 2, "", &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
}, false); err != nil {
}, false, false); err != nil {
return err
}
return nil
Expand All @@ -224,14 +237,14 @@ func TestExcelizeam_Sync(t *testing.T) {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetCellValue(2, 2, "test1", &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
}, false); err != nil {
}, false, false); err != nil {
return err
}
if err := w.SetCellValue(2, 2, nil, &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
}, false); err != nil {
}, false, false); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -283,22 +296,37 @@ func TestExcelizeam_Async(t *testing.T) {
}{
"SetCellValue-with_not_style": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValueAsync(1, 1, "test", nil)
w.SetCellValueAsync(1, 1, "test", nil, false)
},
},
"SetCellValue-with_not_style_override_style": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValue(1, 1, "test1", &excelize.Style{Font: &excelize.Font{Size: 12}}, false, false)
// can override value
w.SetCellValueAsync(1, 1, nil, &excelize.Style{Font: &excelize.Font{Size: 13}}, true)
},
},
"SetCellValue-with_not_style_override_error": {
"SetCellValue-with_not_style_override_value_error": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValue(1, 1, "test1", nil, false)
w.SetCellValue(1, 1, "test1", &excelize.Style{Font: &excelize.Font{Size: 12}}, false, false)
// can override value
w.SetCellValueAsync(1, 1, "test2", nil)
w.SetCellValueAsync(1, 1, "test2", &excelize.Style{Font: &excelize.Font{Size: 13}}, false)
},
wantErr: excelizeam.ErrOverrideCellValue,
},
"SetCellValue-with_not_style_override_style_error": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValue(1, 1, "test1", &excelize.Style{Font: &excelize.Font{Size: 12}}, false, false)
// can override value
w.SetCellValueAsync(1, 1, nil, &excelize.Style{Font: &excelize.Font{Size: 13}}, false)
},
wantErr: excelizeam.ErrOverrideCellStyle,
},
"SetCellValue-with_not_style_multiple_rows_cols_no_sort": {
testFunc: func(w excelizeam.Excelizeam) {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
for colIdx := 1; colIdx <= 10; colIdx++ {
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil)
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false)
}
}
},
Expand All @@ -313,7 +341,7 @@ func TestExcelizeam_Async(t *testing.T) {
if colIdx%2 == 0 {
continue
}
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil)
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false)
}
}
},
Expand All @@ -322,7 +350,7 @@ func TestExcelizeam_Async(t *testing.T) {
testFunc: func(w excelizeam.Excelizeam) {
for colIdx := 1; colIdx <= 10; colIdx++ {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil)
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil, false)
}
}
},
Expand All @@ -338,7 +366,7 @@ func TestExcelizeam_Async(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
})
}, false)
},
},
"SetCellValue-with_style_border_fill_font_alignment_odd_row": {
Expand All @@ -360,7 +388,7 @@ func TestExcelizeam_Async(t *testing.T) {
Color: "#718DDC",
},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
})
}, false)
}
}
},
Expand All @@ -369,25 +397,25 @@ func TestExcelizeam_Async(t *testing.T) {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValue(2, 2, "test1", &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
}, false)
}, false, false)
w.SetCellValueAsync(2, 2, "", &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
})
}, false)
},
wantErr: excelizeam.ErrOverrideCellValue,
},
"SetCellValue-with_style_border_fill_font_alignment_override_style_error": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValue(2, 2, "test1", &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
}, false)
}, false, false)
w.SetCellValueAsync(2, 2, nil, &excelize.Style{
Border: []excelize.Border{
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
},
})
}, false)
},
wantErr: excelizeam.ErrOverrideCellStyle,
},
Expand Down Expand Up @@ -602,7 +630,7 @@ func benchExcelizeam(w io.Writer) error {
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
Font: &excelize.Font{Size: 12, Bold: true},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
}, false); err != nil {
}, false, false); err != nil {
return err
}
}
Expand All @@ -622,7 +650,7 @@ func benchExcelizeamAsync(w io.Writer) error {
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
Font: &excelize.Font{Size: 12, Bold: true},
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
})
}, false)
}
}

Expand Down
Binary file not shown.

0 comments on commit 796eff4

Please sign in to comment.