Skip to content

Commit 59260d9

Browse files
authored
Merge pull request #2 from tomtwinkle/feat/async-write
add async method
2 parents e64dc22 + 658e846 commit 59260d9

18 files changed

Lines changed: 384 additions & 216 deletions

excelizeam.go

Lines changed: 229 additions & 213 deletions
Large diffs are not rendered by default.

excelizeam_test.go

Lines changed: 155 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/xuri/excelize/v2"
1616
)
1717

18-
func TestExcelizeam_Write(t *testing.T) {
18+
func TestExcelizeam_Sync(t *testing.T) {
1919
tests := map[string]struct {
2020
testFunc func(w excelizeam.Excelizeam) error
2121
wantErr error
@@ -258,12 +258,164 @@ func TestExcelizeam_Write(t *testing.T) {
258258
return
259259
}
260260

261-
//f, err := os.Create("testdata/" + name + ".xlsx")
261+
//f, err := os.Create("testdata/sync/" + name + ".xlsx")
262262
//assert.NoError(t, err)
263263
//_, err = f.Write(buf.Bytes())
264264
//assert.NoError(t, err)
265265

266-
expected, err := excelize.OpenFile("testdata/" + name + ".xlsx")
266+
expected, err := excelize.OpenFile("testdata/sync/" + name + ".xlsx")
267+
if !assert.NoError(t, err) {
268+
return
269+
}
270+
actual, err := excelize.OpenReader(&buf)
271+
if !assert.NoError(t, err) {
272+
return
273+
}
274+
Assert(t, expected, actual)
275+
})
276+
}
277+
}
278+
279+
func TestExcelizeam_Async(t *testing.T) {
280+
tests := map[string]struct {
281+
testFunc func(w excelizeam.Excelizeam)
282+
wantErr error
283+
}{
284+
"SetCellValue-with_not_style": {
285+
testFunc: func(w excelizeam.Excelizeam) {
286+
w.SetCellValueAsync(1, 1, "test", nil)
287+
},
288+
},
289+
"SetCellValue-with_not_style_override_error": {
290+
testFunc: func(w excelizeam.Excelizeam) {
291+
w.SetCellValue(1, 1, "test1", nil, false)
292+
// can override value
293+
w.SetCellValueAsync(1, 1, "test2", nil)
294+
},
295+
wantErr: excelizeam.ErrOverrideCellValue,
296+
},
297+
"SetCellValue-with_not_style_multiple_rows_cols_no_sort": {
298+
testFunc: func(w excelizeam.Excelizeam) {
299+
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
300+
for colIdx := 1; colIdx <= 10; colIdx++ {
301+
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil)
302+
}
303+
}
304+
},
305+
},
306+
"SetCellValue-with_not_style_multiple_rows_cols_no_sort_odd": {
307+
testFunc: func(w excelizeam.Excelizeam) {
308+
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
309+
if rowIdx%2 == 0 {
310+
continue
311+
}
312+
for colIdx := 1; colIdx <= 10; colIdx++ {
313+
if colIdx%2 == 0 {
314+
continue
315+
}
316+
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil)
317+
}
318+
}
319+
},
320+
},
321+
"SetCellValue-with_not_style_multiple_rows_cols_sort": {
322+
testFunc: func(w excelizeam.Excelizeam) {
323+
for colIdx := 1; colIdx <= 10; colIdx++ {
324+
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
325+
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("test%d-%d", rowIdx, colIdx), nil)
326+
}
327+
}
328+
},
329+
},
330+
"SetCellValue-with_style_border_fill_font_alignment": {
331+
testFunc: func(w excelizeam.Excelizeam) {
332+
w.SetCellValueAsync(2, 2, "test", &excelize.Style{
333+
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
334+
Fill: excelizestyle.Fill(excelizestyle.FillPatternSolid, "#315D3C"),
335+
Font: &excelize.Font{
336+
Bold: true,
337+
Size: 8,
338+
Color: "#718DDC",
339+
},
340+
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
341+
})
342+
},
343+
},
344+
"SetCellValue-with_style_border_fill_font_alignment_odd_row": {
345+
testFunc: func(w excelizeam.Excelizeam) {
346+
for rowIdx := 1; rowIdx <= 10; rowIdx++ {
347+
if rowIdx%2 == 0 {
348+
continue
349+
}
350+
for colIdx := 1; colIdx <= 10; colIdx++ {
351+
if colIdx%2 == 0 {
352+
continue
353+
}
354+
w.SetCellValueAsync(colIdx, rowIdx, fmt.Sprintf("%d-%d", rowIdx, colIdx), &excelize.Style{
355+
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
356+
Fill: excelizestyle.Fill(excelizestyle.FillPatternSolid, "#315D3C"),
357+
Font: &excelize.Font{
358+
Bold: true,
359+
Size: 8,
360+
Color: "#718DDC",
361+
},
362+
Alignment: excelizestyle.Alignment(excelizestyle.AlignmentHorizontalCenter, excelizestyle.AlignmentVerticalCenter, true),
363+
})
364+
}
365+
}
366+
},
367+
},
368+
"SetCellValue-with_style_border_fill_font_alignment_override_value_error": {
369+
testFunc: func(w excelizeam.Excelizeam) {
370+
w.SetCellValue(2, 2, "test1", &excelize.Style{
371+
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
372+
}, false)
373+
w.SetCellValueAsync(2, 2, "", &excelize.Style{
374+
Border: []excelize.Border{
375+
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
376+
},
377+
})
378+
},
379+
wantErr: excelizeam.ErrOverrideCellValue,
380+
},
381+
"SetCellValue-with_style_border_fill_font_alignment_override_style_error": {
382+
testFunc: func(w excelizeam.Excelizeam) {
383+
w.SetCellValue(2, 2, "test1", &excelize.Style{
384+
Border: excelizestyle.BorderAround(excelizestyle.BorderStyleContinuous2, excelizestyle.BorderColorBlack),
385+
}, false)
386+
w.SetCellValueAsync(2, 2, nil, &excelize.Style{
387+
Border: []excelize.Border{
388+
excelizestyle.Border(excelizestyle.BorderPositionRight, excelizestyle.BorderStyleDash2, excelizestyle.BorderColorBlack),
389+
},
390+
})
391+
},
392+
wantErr: excelizeam.ErrOverrideCellStyle,
393+
},
394+
}
395+
396+
for n, v := range tests {
397+
name := n
398+
tt := v
399+
t.Run(name, func(t *testing.T) {
400+
w, err := excelizeam.New("test")
401+
assert.NoError(t, err)
402+
tt.testFunc(w)
403+
var buf bytes.Buffer
404+
err = w.Write(&buf)
405+
if tt.wantErr != nil {
406+
assert.ErrorIs(t, err, tt.wantErr)
407+
return
408+
}
409+
if !assert.NoError(t, err) {
410+
return
411+
}
412+
413+
//f, err := os.Create("testdata/async/" + name + ".xlsx")
414+
//assert.NoError(t, err)
415+
//_, err = f.Write(buf.Bytes())
416+
//assert.NoError(t, err)
417+
418+
expected, err := excelize.OpenFile("testdata/async/" + name + ".xlsx")
267419
if !assert.NoError(t, err) {
268420
return
269421
}
6.21 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.

testdata/SetCellValue-with_not_style_multiple_rows_cols_no_sort.xlsx renamed to testdata/sync/SetCellValue-with_not_style_multiple_rows_cols_no_sort.xlsx

File renamed without changes.

0 commit comments

Comments
 (0)