2019-03-24 13:08:32 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdjustMergeCells(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
// testing adjustAutoFilter with illegal cell coordinates.
|
|
|
|
assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
2019-04-16 10:57:21 +08:00
|
|
|
{
|
2019-03-24 13:08:32 +08:00
|
|
|
Ref: "A:B1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-07 00:26:53 +08:00
|
|
|
}, rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2019-03-24 13:08:32 +08:00
|
|
|
assert.EqualError(t, f.adjustMergeCells(&xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
2019-04-16 10:57:21 +08:00
|
|
|
{
|
2019-03-24 13:08:32 +08:00
|
|
|
Ref: "A1:B",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-07 00:26:53 +08:00
|
|
|
}, rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
|
2019-06-12 08:10:33 +08:00
|
|
|
assert.NoError(t, f.adjustMergeCells(&xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A1:B1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, rows, 1, -1))
|
|
|
|
assert.NoError(t, f.adjustMergeCells(&xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A1:A2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, columns, 1, -1))
|
2021-12-06 22:37:25 +08:00
|
|
|
|
|
|
|
// testing adjustMergeCells
|
|
|
|
var cases []struct {
|
2022-01-09 00:20:42 +08:00
|
|
|
label string
|
2021-12-06 22:37:25 +08:00
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
}
|
|
|
|
|
|
|
|
// testing insert
|
|
|
|
cases = []struct {
|
2022-01-09 00:20:42 +08:00
|
|
|
label string
|
2021-12-06 22:37:25 +08:00
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "insert row on ref",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: rows,
|
|
|
|
num: 2,
|
|
|
|
offset: 1,
|
|
|
|
expect: "A3:B4",
|
|
|
|
},
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "insert row on bottom of ref",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: rows,
|
|
|
|
num: 3,
|
|
|
|
offset: 1,
|
|
|
|
expect: "A2:B4",
|
|
|
|
},
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "insert column on the left",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: columns,
|
|
|
|
num: 1,
|
|
|
|
offset: 1,
|
|
|
|
expect: "B2:C3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
assert.NoError(t, f.adjustMergeCells(c.ws, c.dir, c.num, 1))
|
2022-01-09 00:20:42 +08:00
|
|
|
assert.Equal(t, c.expect, c.ws.MergeCells.Cells[0].Ref, c.label)
|
2021-12-06 22:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// testing delete
|
|
|
|
cases = []struct {
|
2022-01-09 00:20:42 +08:00
|
|
|
label string
|
2021-12-06 22:37:25 +08:00
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "delete row on top of ref",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: rows,
|
|
|
|
num: 2,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:B2",
|
|
|
|
},
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "delete row on bottom of ref",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: rows,
|
|
|
|
num: 3,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:B2",
|
|
|
|
},
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "delete column on the ref left",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: columns,
|
|
|
|
num: 1,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:A3",
|
|
|
|
},
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "delete column on the ref right",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A2:B3",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: columns,
|
|
|
|
num: 2,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:A3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
assert.NoError(t, f.adjustMergeCells(c.ws, c.dir, c.num, -1))
|
2022-01-09 00:20:42 +08:00
|
|
|
assert.Equal(t, c.expect, c.ws.MergeCells.Cells[0].Ref, c.label)
|
2021-12-06 22:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// testing delete one row/column
|
|
|
|
cases = []struct {
|
2022-01-09 00:20:42 +08:00
|
|
|
label string
|
2021-12-06 22:37:25 +08:00
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "delete one row ref",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A1:B1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: rows,
|
|
|
|
num: 1,
|
|
|
|
offset: -1,
|
|
|
|
},
|
|
|
|
{
|
2022-01-09 00:20:42 +08:00
|
|
|
label: "delete one column ref",
|
2021-12-06 22:37:25 +08:00
|
|
|
ws: &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
|
|
|
{
|
|
|
|
Ref: "A1:A2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
dir: columns,
|
|
|
|
num: 1,
|
|
|
|
offset: -1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
assert.NoError(t, f.adjustMergeCells(c.ws, c.dir, c.num, -1))
|
2022-01-09 00:20:42 +08:00
|
|
|
assert.Equal(t, 0, len(c.ws.MergeCells.Cells), c.label)
|
2021-12-06 22:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
f = NewFile()
|
|
|
|
p1, p2 := f.adjustMergeCellsHelper(2, 1, 0, 0)
|
|
|
|
assert.Equal(t, 1, p1)
|
|
|
|
assert.Equal(t, 2, p2)
|
|
|
|
f.deleteMergeCell(nil, -1)
|
2019-03-24 13:08:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdjustAutoFilter(t *testing.T) {
|
|
|
|
f := NewFile()
|
2020-10-11 00:15:04 +08:00
|
|
|
assert.NoError(t, f.adjustAutoFilter(&xlsxWorksheet{
|
|
|
|
SheetData: xlsxSheetData{
|
|
|
|
Row: []xlsxRow{{Hidden: true, R: 2}},
|
|
|
|
},
|
|
|
|
AutoFilter: &xlsxAutoFilter{
|
|
|
|
Ref: "A1:A3",
|
|
|
|
},
|
|
|
|
}, rows, 1, -1))
|
2019-03-24 13:08:32 +08:00
|
|
|
// testing adjustAutoFilter with illegal cell coordinates.
|
|
|
|
assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
|
|
|
|
AutoFilter: &xlsxAutoFilter{
|
|
|
|
Ref: "A:B1",
|
|
|
|
},
|
2021-12-07 00:26:53 +08:00
|
|
|
}, rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
2019-03-24 13:08:32 +08:00
|
|
|
assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
|
|
|
|
AutoFilter: &xlsxAutoFilter{
|
|
|
|
Ref: "A1:B",
|
|
|
|
},
|
2021-12-07 00:26:53 +08:00
|
|
|
}, rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
|
2019-03-24 13:08:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdjustHelper(t *testing.T) {
|
|
|
|
f := NewFile()
|
2019-04-15 11:22:57 +08:00
|
|
|
f.NewSheet("Sheet2")
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:B1"}}}})
|
|
|
|
f.Sheet.Store("xl/worksheets/sheet2.xml", &xlsxWorksheet{
|
|
|
|
AutoFilter: &xlsxAutoFilter{Ref: "A1:B"}})
|
2019-03-24 13:08:32 +08:00
|
|
|
// testing adjustHelper with illegal cell coordinates.
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), newCellNameToCoordinatesError("A", newInvalidCellNameError("A")).Error())
|
|
|
|
assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), newCellNameToCoordinatesError("B", newInvalidCellNameError("B")).Error())
|
2019-04-16 10:57:21 +08:00
|
|
|
// testing adjustHelper on not exists worksheet.
|
|
|
|
assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN is not exist")
|
2019-03-24 13:08:32 +08:00
|
|
|
}
|
2019-06-08 00:00:55 +08:00
|
|
|
|
|
|
|
func TestAdjustCalcChain(t *testing.T) {
|
|
|
|
f := NewFile()
|
|
|
|
f.CalcChain = &xlsxCalcChain{
|
|
|
|
C: []xlsxCalcChainC{
|
2021-02-02 22:23:16 +08:00
|
|
|
{R: "B2", I: 2}, {R: "B2", I: 1},
|
2019-06-08 00:00:55 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.InsertCol("Sheet1", "A"))
|
|
|
|
assert.NoError(t, f.InsertRow("Sheet1", 1))
|
|
|
|
|
2021-02-02 22:23:16 +08:00
|
|
|
f.CalcChain.C[1].R = "invalid coordinates"
|
2021-12-07 00:26:53 +08:00
|
|
|
assert.EqualError(t, f.InsertCol("Sheet1", "A"), newCellNameToCoordinatesError("invalid coordinates", newInvalidCellNameError("invalid coordinates")).Error())
|
2019-06-08 00:00:55 +08:00
|
|
|
f.CalcChain = nil
|
|
|
|
assert.NoError(t, f.InsertCol("Sheet1", "A"))
|
|
|
|
}
|