forked from p30928647/excelize
Fix adjustMergeCells not modifies cell rect (#1118)
This commit is contained in:
parent
b96329cc88
commit
67127883dd
|
@ -226,6 +226,7 @@ func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, off
|
||||||
i--
|
i--
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
areaData.rect = []int{x1, y1, x2, y2}
|
||||||
if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
|
if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num int
|
num int
|
||||||
offset int
|
offset int
|
||||||
expect string
|
expect string
|
||||||
|
expectRect []int
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing insert
|
// testing insert
|
||||||
|
@ -64,6 +65,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num int
|
num int
|
||||||
offset int
|
offset int
|
||||||
expect string
|
expect string
|
||||||
|
expectRect []int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
label: "insert row on ref",
|
label: "insert row on ref",
|
||||||
|
@ -72,6 +74,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -80,6 +83,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 2,
|
num: 2,
|
||||||
offset: 1,
|
offset: 1,
|
||||||
expect: "A3:B4",
|
expect: "A3:B4",
|
||||||
|
expectRect: []int{1, 3, 2, 4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "insert row on bottom of ref",
|
label: "insert row on bottom of ref",
|
||||||
|
@ -88,6 +92,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -96,6 +101,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 3,
|
num: 3,
|
||||||
offset: 1,
|
offset: 1,
|
||||||
expect: "A2:B4",
|
expect: "A2:B4",
|
||||||
|
expectRect: []int{1, 2, 2, 4},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "insert column on the left",
|
label: "insert column on the left",
|
||||||
|
@ -104,6 +110,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -112,11 +119,13 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 1,
|
num: 1,
|
||||||
offset: 1,
|
offset: 1,
|
||||||
expect: "B2:C3",
|
expect: "B2:C3",
|
||||||
|
expectRect: []int{2, 2, 3, 3},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
assert.NoError(t, f.adjustMergeCells(c.ws, c.dir, c.num, 1))
|
assert.NoError(t, f.adjustMergeCells(c.ws, c.dir, c.num, 1))
|
||||||
assert.Equal(t, c.expect, c.ws.MergeCells.Cells[0].Ref, c.label)
|
assert.Equal(t, c.expect, c.ws.MergeCells.Cells[0].Ref, c.label)
|
||||||
|
assert.Equal(t, c.expectRect, c.ws.MergeCells.Cells[0].rect, c.label)
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing delete
|
// testing delete
|
||||||
|
@ -127,6 +136,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num int
|
num int
|
||||||
offset int
|
offset int
|
||||||
expect string
|
expect string
|
||||||
|
expectRect []int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
label: "delete row on top of ref",
|
label: "delete row on top of ref",
|
||||||
|
@ -135,6 +145,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -143,6 +154,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 2,
|
num: 2,
|
||||||
offset: -1,
|
offset: -1,
|
||||||
expect: "A2:B2",
|
expect: "A2:B2",
|
||||||
|
expectRect: []int{1, 2, 2, 2},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "delete row on bottom of ref",
|
label: "delete row on bottom of ref",
|
||||||
|
@ -151,6 +163,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -159,6 +172,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 3,
|
num: 3,
|
||||||
offset: -1,
|
offset: -1,
|
||||||
expect: "A2:B2",
|
expect: "A2:B2",
|
||||||
|
expectRect: []int{1, 2, 2, 2},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "delete column on the ref left",
|
label: "delete column on the ref left",
|
||||||
|
@ -167,6 +181,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -175,6 +190,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 1,
|
num: 1,
|
||||||
offset: -1,
|
offset: -1,
|
||||||
expect: "A2:A3",
|
expect: "A2:A3",
|
||||||
|
expectRect: []int{1, 2, 1, 3},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "delete column on the ref right",
|
label: "delete column on the ref right",
|
||||||
|
@ -183,6 +199,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A2:B3",
|
Ref: "A2:B3",
|
||||||
|
rect: []int{1, 2, 2, 3},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -191,6 +208,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num: 2,
|
num: 2,
|
||||||
offset: -1,
|
offset: -1,
|
||||||
expect: "A2:A3",
|
expect: "A2:A3",
|
||||||
|
expectRect: []int{1, 2, 1, 3},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
@ -206,6 +224,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
num int
|
num int
|
||||||
offset int
|
offset int
|
||||||
expect string
|
expect string
|
||||||
|
expectRect []int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
label: "delete one row ref",
|
label: "delete one row ref",
|
||||||
|
@ -214,6 +233,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A1:B1",
|
Ref: "A1:B1",
|
||||||
|
rect: []int{1, 1, 2, 1},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -229,6 +249,7 @@ func TestAdjustMergeCells(t *testing.T) {
|
||||||
Cells: []*xlsxMergeCell{
|
Cells: []*xlsxMergeCell{
|
||||||
{
|
{
|
||||||
Ref: "A1:A2",
|
Ref: "A1:A2",
|
||||||
|
rect: []int{1, 1, 1, 2},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
10
rows.go
10
rows.go
|
@ -661,7 +661,8 @@ func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if row > len(ws.SheetData.Row) || row2 < 1 || row == row2 {
|
|
||||||
|
if row2 < 1 || row == row2 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -675,14 +676,15 @@ func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := f.adjustHelper(sheet, rows, row2, 1); err != nil {
|
if err := f.adjustHelper(sheet, rows, row2, 1); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
idx2 := -1
|
idx2 := -1
|
||||||
for i, r := range ws.SheetData.Row {
|
for i, r := range ws.SheetData.Row {
|
||||||
if r.R == row2 {
|
if r.R == row2 {
|
||||||
|
|
16
rows_test.go
16
rows_test.go
|
@ -669,6 +669,7 @@ func TestDuplicateRowInsertBefore(t *testing.T) {
|
||||||
f := newFileWithDefaults()
|
f := newFileWithDefaults()
|
||||||
|
|
||||||
assert.NoError(t, f.DuplicateRowTo(sheet, 2, 1))
|
assert.NoError(t, f.DuplicateRowTo(sheet, 2, 1))
|
||||||
|
assert.NoError(t, f.DuplicateRowTo(sheet, 10, 4))
|
||||||
|
|
||||||
if !assert.NoError(t, f.SaveAs(fmt.Sprintf(outFile, "InsertBefore"))) {
|
if !assert.NoError(t, f.SaveAs(fmt.Sprintf(outFile, "InsertBefore"))) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
|
@ -678,7 +679,7 @@ func TestDuplicateRowInsertBefore(t *testing.T) {
|
||||||
"A1": cells["A2"], "B1": cells["B2"],
|
"A1": cells["A2"], "B1": cells["B2"],
|
||||||
"A2": cells["A1"], "B2": cells["B1"],
|
"A2": cells["A1"], "B2": cells["B1"],
|
||||||
"A3": cells["A2"], "B3": cells["B2"],
|
"A3": cells["A2"], "B3": cells["B2"],
|
||||||
"A4": cells["A3"], "B4": cells["B3"],
|
"A5": cells["A3"], "B5": cells["B3"],
|
||||||
}
|
}
|
||||||
for cell, val := range expect {
|
for cell, val := range expect {
|
||||||
v, err := f.GetCellValue(sheet, cell)
|
v, err := f.GetCellValue(sheet, cell)
|
||||||
|
@ -846,7 +847,18 @@ func TestDuplicateRowInvalidRowNum(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDuplicateRowTo(t *testing.T) {
|
func TestDuplicateRowTo(t *testing.T) {
|
||||||
f := File{}
|
f, sheetName := NewFile(), "Sheet1"
|
||||||
|
// Test duplicate row with invalid target row number
|
||||||
|
assert.Equal(t, nil, f.DuplicateRowTo(sheetName, 1, 0))
|
||||||
|
// Test duplicate row with equal source and target row number
|
||||||
|
assert.Equal(t, nil, f.DuplicateRowTo(sheetName, 1, 1))
|
||||||
|
// Test duplicate row on the blank worksheet
|
||||||
|
assert.Equal(t, nil, f.DuplicateRowTo(sheetName, 1, 2))
|
||||||
|
// Test duplicate row on the worksheet with illegal cell coordinates
|
||||||
|
f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
|
||||||
|
MergeCells: &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:B1"}}}})
|
||||||
|
assert.EqualError(t, f.DuplicateRowTo(sheetName, 1, 2), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
||||||
|
// Test duplicate row on not exists worksheet
|
||||||
assert.EqualError(t, f.DuplicateRowTo("SheetN", 1, 2), "sheet SheetN is not exist")
|
assert.EqualError(t, f.DuplicateRowTo("SheetN", 1, 2), "sheet SheetN is not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue