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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
|
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))
|
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",
|
|
|
|
},
|
|
|
|
}, rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
assert.EqualError(t, f.adjustAutoFilter(&xlsxWorksheet{
|
|
|
|
AutoFilter: &xlsxAutoFilter{
|
|
|
|
Ref: "A1:B",
|
|
|
|
},
|
|
|
|
}, rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdjustHelper(t *testing.T) {
|
|
|
|
f := NewFile()
|
2019-04-15 11:22:57 +08:00
|
|
|
f.NewSheet("Sheet2")
|
2019-03-24 13:08:32 +08:00
|
|
|
f.Sheet["xl/worksheets/sheet1.xml"] = &xlsxWorksheet{
|
|
|
|
MergeCells: &xlsxMergeCells{
|
|
|
|
Cells: []*xlsxMergeCell{
|
2019-04-16 10:57:21 +08:00
|
|
|
{
|
2019-03-24 13:08:32 +08:00
|
|
|
Ref: "A:B1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f.Sheet["xl/worksheets/sheet2.xml"] = &xlsxWorksheet{
|
|
|
|
AutoFilter: &xlsxAutoFilter{
|
|
|
|
Ref: "A1:B",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// testing adjustHelper with illegal cell coordinates.
|
2019-04-15 11:22:57 +08:00
|
|
|
assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
|
|
|
assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
|
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"
|
2019-06-08 00:00:55 +08:00
|
|
|
assert.EqualError(t, f.InsertCol("Sheet1", "A"), `cannot convert cell "invalid coordinates" to coordinates: invalid cell name "invalid coordinates"`)
|
|
|
|
f.CalcChain = nil
|
|
|
|
assert.NoError(t, f.InsertCol("Sheet1", "A"))
|
|
|
|
}
|
2019-06-12 08:10:33 +08:00
|
|
|
|
|
|
|
func TestCoordinatesToAreaRef(t *testing.T) {
|
|
|
|
f := NewFile()
|
2019-06-20 00:00:40 +08:00
|
|
|
_, err := f.coordinatesToAreaRef([]int{})
|
2021-05-10 00:09:24 +08:00
|
|
|
assert.EqualError(t, err, ErrCoordinates.Error())
|
2019-06-20 00:00:40 +08:00
|
|
|
_, err = f.coordinatesToAreaRef([]int{1, -1, 1, 1})
|
2019-06-12 08:10:33 +08:00
|
|
|
assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
|
2019-06-20 00:00:40 +08:00
|
|
|
_, err = f.coordinatesToAreaRef([]int{1, 1, 1, -1})
|
2019-06-12 08:10:33 +08:00
|
|
|
assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
|
2019-06-20 00:00:40 +08:00
|
|
|
ref, err := f.coordinatesToAreaRef([]int{1, 1, 1, 1})
|
2019-06-12 08:10:33 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, ref, "A1:A1")
|
|
|
|
}
|
2019-12-29 16:02:31 +08:00
|
|
|
|
|
|
|
func TestSortCoordinates(t *testing.T) {
|
2021-05-10 00:09:24 +08:00
|
|
|
assert.EqualError(t, sortCoordinates(make([]int, 3)), ErrCoordinates.Error())
|
2019-12-29 16:02:31 +08:00
|
|
|
}
|