2019-03-24 13:08:32 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2022-07-16 12:50:13 +08:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2019-03-24 13:08:32 +08:00
|
|
|
"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-14 00:28:31 +08:00
|
|
|
label string
|
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
expectRect []int
|
2021-12-06 22:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// testing insert
|
|
|
|
cases = []struct {
|
2022-01-14 00:28:31 +08:00
|
|
|
label string
|
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
expectRect []int
|
2021-12-06 22:37:25 +08:00
|
|
|
}{
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: rows,
|
|
|
|
num: 2,
|
|
|
|
offset: 1,
|
|
|
|
expect: "A3:B4",
|
|
|
|
expectRect: []int{1, 3, 2, 4},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: rows,
|
|
|
|
num: 3,
|
|
|
|
offset: 1,
|
|
|
|
expect: "A2:B4",
|
|
|
|
expectRect: []int{1, 2, 2, 4},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: columns,
|
|
|
|
num: 1,
|
|
|
|
offset: 1,
|
|
|
|
expect: "B2:C3",
|
|
|
|
expectRect: []int{2, 2, 3, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2022-01-14 00:28:31 +08:00
|
|
|
assert.Equal(t, c.expectRect, c.ws.MergeCells.Cells[0].rect, c.label)
|
2021-12-06 22:37:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// testing delete
|
|
|
|
cases = []struct {
|
2022-01-14 00:28:31 +08:00
|
|
|
label string
|
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
expectRect []int
|
2021-12-06 22:37:25 +08:00
|
|
|
}{
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: rows,
|
|
|
|
num: 2,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:B2",
|
|
|
|
expectRect: []int{1, 2, 2, 2},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: rows,
|
|
|
|
num: 3,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:B2",
|
|
|
|
expectRect: []int{1, 2, 2, 2},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: columns,
|
|
|
|
num: 1,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:A3",
|
|
|
|
expectRect: []int{1, 2, 1, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A2:B3",
|
|
|
|
rect: []int{1, 2, 2, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-14 00:28:31 +08:00
|
|
|
dir: columns,
|
|
|
|
num: 2,
|
|
|
|
offset: -1,
|
|
|
|
expect: "A2:A3",
|
|
|
|
expectRect: []int{1, 2, 1, 3},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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-14 00:28:31 +08:00
|
|
|
label string
|
|
|
|
ws *xlsxWorksheet
|
|
|
|
dir adjustDirection
|
|
|
|
num int
|
|
|
|
offset int
|
|
|
|
expect string
|
|
|
|
expectRect []int
|
2021-12-06 22:37:25 +08:00
|
|
|
}{
|
|
|
|
{
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A1:B1",
|
|
|
|
rect: []int{1, 1, 2, 1},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
{
|
2022-01-14 00:28:31 +08:00
|
|
|
Ref: "A1:A2",
|
|
|
|
rect: []int{1, 1, 1, 2},
|
2021-12-06 22:37:25 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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))
|
2022-07-16 12:50:13 +08:00
|
|
|
// Test adjustAutoFilter with illegal cell coordinates.
|
2019-03-24 13:08:32 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-16 12:50:13 +08:00
|
|
|
func TestAdjustTable(t *testing.T) {
|
|
|
|
f, sheetName := NewFile(), "Sheet1"
|
|
|
|
for idx, tableRange := range [][]string{{"B2", "C3"}, {"E3", "F5"}, {"H5", "H8"}, {"J5", "K9"}} {
|
|
|
|
assert.NoError(t, f.AddTable(sheetName, tableRange[0], tableRange[1], fmt.Sprintf(`{
|
|
|
|
"table_name": "table%d",
|
|
|
|
"table_style": "TableStyleMedium2",
|
|
|
|
"show_first_column": true,
|
|
|
|
"show_last_column": true,
|
|
|
|
"show_row_stripes": false,
|
|
|
|
"show_column_stripes": true
|
|
|
|
}`, idx)))
|
|
|
|
}
|
|
|
|
assert.NoError(t, f.RemoveRow(sheetName, 2))
|
|
|
|
assert.NoError(t, f.RemoveRow(sheetName, 3))
|
|
|
|
assert.NoError(t, f.RemoveCol(sheetName, "H"))
|
|
|
|
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAdjustTable.xlsx")))
|
|
|
|
|
|
|
|
f = NewFile()
|
|
|
|
assert.NoError(t, f.AddTable(sheetName, "A1", "D5", ""))
|
|
|
|
// Test adjust table with non-table part
|
|
|
|
f.Pkg.Delete("xl/tables/table1.xml")
|
|
|
|
assert.NoError(t, f.RemoveRow(sheetName, 1))
|
|
|
|
// Test adjust table with unsupported charset
|
|
|
|
f.Pkg.Store("xl/tables/table1.xml", MacintoshCyrillicCharset)
|
|
|
|
assert.NoError(t, f.RemoveRow(sheetName, 1))
|
|
|
|
// Test adjust table with invalid table range reference
|
|
|
|
f.Pkg.Store("xl/tables/table1.xml", []byte(`<table ref="-" />`))
|
|
|
|
assert.NoError(t, f.RemoveRow(sheetName, 1))
|
|
|
|
}
|
|
|
|
|
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{
|
2022-01-23 00:32:34 +08:00
|
|
|
MergeCells: &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:B1"}}},
|
|
|
|
})
|
2021-07-05 00:03:56 +08:00
|
|
|
f.Sheet.Store("xl/worksheets/sheet2.xml", &xlsxWorksheet{
|
2022-01-23 00:32:34 +08:00
|
|
|
AutoFilter: &xlsxAutoFilter{Ref: "A1:B"},
|
|
|
|
})
|
2022-07-16 12:50:13 +08:00
|
|
|
// Test 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())
|
2022-07-16 12:50:13 +08:00
|
|
|
// Test adjustHelper on not exists worksheet.
|
2022-08-28 00:16:41 +08:00
|
|
|
assert.EqualError(t, f.adjustHelper("SheetN", rows, 0, 0), "sheet SheetN does 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"))
|
|
|
|
}
|