Skip to content

Commit

Permalink
Merge pull request #9 from tomtwinkle/fix/set-set-border-range-max-index
Browse files Browse the repository at this point in the history
fix: setborderRange maxindex, add test
  • Loading branch information
tomtwinkle authored Aug 22, 2022
2 parents ec465d9 + ccc57c5 commit 8c3b76e
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 13 deletions.
4 changes: 2 additions & 2 deletions excelizeam.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (e *excelizeam) SetBorderRange(startColIndex, startRowIndex, endColIndex, e
}

func (e *excelizeam) setBorderRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange, override bool) error {
e.checkMaxIndex(startColIndex, startRowIndex)
e.checkMaxIndex(endColIndex, endRowIndex)
for rowIdx := startRowIndex; rowIdx <= endRowIndex; rowIdx++ {
for colIdx := startColIndex; colIdx <= endColIndex; colIdx++ {
key := e.getCacheKey(colIdx, rowIdx)
Expand Down Expand Up @@ -522,7 +522,7 @@ func (e *excelizeam) setBorderRange(startColIndex, startRowIndex, endColIndex, e
if err != nil {
return err
}
e.cellStore.Store(colIdx, &Cell{
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
Expand Down
140 changes: 129 additions & 11 deletions excelizeam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ func TestExcelizeam_Sync(t *testing.T) {
return nil
},
},
"SetCellValue-with_not_style_multiple_rows_cols_no_sort_odd_override_cross": {
testFunc: func(w excelizeam.Excelizeam) error {
for rowIdx := 1; rowIdx <= 7; rowIdx++ {
if rowIdx%2 == 0 {
continue
}
for colIdx := 1; colIdx <= 7; colIdx++ {
if colIdx%2 == 0 {
continue
}
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
}, false, false); err != nil {
return err
}
}
}
for rowIdx := 5; rowIdx <= 10; rowIdx++ {
if rowIdx%2 == 0 {
continue
}
for colIdx := 5; colIdx <= 10; colIdx++ {
if colIdx%2 == 0 {
continue
}
if err := w.SetCellValue(colIdx, rowIdx, fmt.Sprintf("override-test%d-%d", rowIdx, colIdx), &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
}, true, true); err != nil {
return err
}
}
}
return nil
},
},
"SetCellValue-with_not_style_multiple_rows_cols_sort": {
testFunc: func(w excelizeam.Excelizeam) error {
for colIdx := 1; colIdx <= 10; colIdx++ {
Expand Down Expand Up @@ -251,6 +286,52 @@ func TestExcelizeam_Sync(t *testing.T) {
},
wantErr: excelizeam.ErrOverrideCellStyle,
},
"SetBorderRange-border_odd_override_cross": {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetBorderRange(1, 1, 7, 7, excelizeam.BorderRange{
Top: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Bottom: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Left: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Right: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Inside: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
}, false); err != nil {
return err
}
if err := w.SetBorderRange(5, 5, 10, 10, excelizeam.BorderRange{
Top: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Bottom: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Left: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Right: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Inside: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
}, true); err != nil {
return err
}
return nil
},
},
"SetStyleCellRange-border_odd_override_cross": {
testFunc: func(w excelizeam.Excelizeam) error {
if err := w.SetStyleCellRange(1, 1, 7, 7, excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
Fill: excelizestyle.Fill(excelizestyle.FillPatternSolid, "#BF00BF"),
}, false); err != nil {
return err
}
if err := w.SetStyleCellRange(5, 5, 10, 10, excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
Fill: excelizestyle.Fill(excelizestyle.FillPatternSolid, "#CFA0FF"),
}, true); err != nil {
return err
}
if err := w.SetStyleCellRange(6, 6, 7, 7, excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
Fill: excelizestyle.Fill(excelizestyle.FillPatternSolid, "#7FA04F"),
}, true); err != nil {
return err
}
return nil
},
},
}

for n, v := range tests {
Expand Down Expand Up @@ -294,35 +375,35 @@ func TestExcelizeam_Async(t *testing.T) {
testFunc func(w excelizeam.Excelizeam)
wantErr error
}{
"SetCellValue-with_not_style": {
"SetCellValueAsync-with_not_style": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValueAsync(1, 1, "test", nil, false)
},
},
"SetCellValue-with_not_style_override_style": {
"SetCellValueAsync-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_value_error": {
"SetCellValueAsync-with_not_style_override_value_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, "test2", &excelize.Style{Font: &excelize.Font{Size: 13}}, false)
},
wantErr: excelizeam.ErrOverrideCellValue,
},
"SetCellValue-with_not_style_override_style_error": {
"SetCellValueAsync-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": {
"SetCellValueAsync-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++ {
Expand All @@ -331,7 +412,7 @@ func TestExcelizeam_Async(t *testing.T) {
}
},
},
"SetCellValue-with_not_style_multiple_rows_cols_no_sort_odd": {
"SetCellValueAsync-with_not_style_multiple_rows_cols_no_sort_odd": {
testFunc: func(w excelizeam.Excelizeam) {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
if rowIdx%2 == 0 {
Expand All @@ -346,7 +427,7 @@ func TestExcelizeam_Async(t *testing.T) {
}
},
},
"SetCellValue-with_not_style_multiple_rows_cols_sort": {
"SetCellValueAsync-with_not_style_multiple_rows_cols_sort": {
testFunc: func(w excelizeam.Excelizeam) {
for colIdx := 1; colIdx <= 10; colIdx++ {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
Expand All @@ -355,7 +436,7 @@ func TestExcelizeam_Async(t *testing.T) {
}
},
},
"SetCellValue-with_style_border_fill_font_alignment": {
"SetCellValueAsync-with_style_border_fill_font_alignment": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValueAsync(2, 2, "test", &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
Expand All @@ -369,7 +450,7 @@ func TestExcelizeam_Async(t *testing.T) {
}, false)
},
},
"SetCellValue-with_style_border_fill_font_alignment_odd_row": {
"SetCellValueAsync-with_style_border_fill_font_alignment_odd_row": {
testFunc: func(w excelizeam.Excelizeam) {
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
if rowIdx%2 == 0 {
Expand All @@ -393,7 +474,7 @@ func TestExcelizeam_Async(t *testing.T) {
}
},
},
"SetCellValue-with_style_border_fill_font_alignment_override_value_error": {
"SetCellValueAsync-with_style_border_fill_font_alignment_override_value_error": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetCellValue(2, 2, "test1", &excelize.Style{
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
Expand All @@ -406,7 +487,7 @@ func TestExcelizeam_Async(t *testing.T) {
},
wantErr: excelizeam.ErrOverrideCellValue,
},
"SetCellValue-with_style_border_fill_font_alignment_override_style_error": {
"SetCellValueAsync-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),
Expand All @@ -419,6 +500,43 @@ func TestExcelizeam_Async(t *testing.T) {
},
wantErr: excelizeam.ErrOverrideCellStyle,
},
"SetBorderRangeAsync-not_override_style": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetBorderRangeAsync(1, 1, 5, 5, excelizeam.BorderRange{
Top: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Bottom: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Left: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Right: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Inside: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
})
w.SetBorderRangeAsync(6, 6, 10, 10, excelizeam.BorderRange{
Top: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Bottom: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Left: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Right: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Inside: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
})
},
},
"SetBorderRangeAsync-override_error": {
testFunc: func(w excelizeam.Excelizeam) {
w.SetBorderRange(1, 1, 7, 7, excelizeam.BorderRange{
Top: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Bottom: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Left: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Right: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
Inside: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleDash2, Color: excelizestyle.BorderColorBlack},
}, false)
w.SetBorderRangeAsync(5, 5, 10, 10, excelizeam.BorderRange{
Top: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Bottom: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Left: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Right: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
Inside: &excelizeam.BorderItem{Style: excelizestyle.BorderStyleContinuous2, Color: excelizestyle.BorderColorBlack},
})
},
wantErr: excelizeam.ErrOverrideCellStyle,
},
}

for n, v := range tests {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 8c3b76e

Please sign in to comment.