diff options
Diffstat (limited to 'excelize_test.go')
| -rw-r--r-- | excelize_test.go | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/excelize_test.go b/excelize_test.go index 7b6b674..95d63fd 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -290,6 +290,12 @@ func TestSetCellHyperLink(t *testing.T) { assert.NoError(t, file.SetCellHyperLink("Sheet1", cell, "https://github.com/360EntSecGroup-Skylar/excelize", "External")) } assert.EqualError(t, file.SetCellHyperLink("Sheet1", "A65531", "https://github.com/360EntSecGroup-Skylar/excelize", "External"), "over maximum limit hyperlinks in a worksheet") + + f = NewFile() + f.workSheetReader("Sheet1") + f.Sheet["xl/worksheets/sheet1.xml"].MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:A"}}} + err = f.SetCellHyperLink("Sheet1", "A1", "https://github.com/360EntSecGroup-Skylar/excelize", "External") + assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) } func TestGetCellHyperLink(t *testing.T) { @@ -310,6 +316,23 @@ func TestGetCellHyperLink(t *testing.T) { link, target, err = f.GetCellHyperLink("Sheet3", "H3") assert.EqualError(t, err, "sheet Sheet3 is not exist") t.Log(link, target) + + f = NewFile() + f.workSheetReader("Sheet1") + f.Sheet["xl/worksheets/sheet1.xml"].Hyperlinks = &xlsxHyperlinks{ + Hyperlink: []xlsxHyperlink{{Ref: "A1"}}, + } + link, target, err = f.GetCellHyperLink("Sheet1", "A1") + assert.NoError(t, err) + assert.Equal(t, link, true) + assert.Equal(t, target, "") + + f.Sheet["xl/worksheets/sheet1.xml"].MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: "A:A"}}} + link, target, err = f.GetCellHyperLink("Sheet1", "A1") + assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`) + assert.Equal(t, link, false) + assert.Equal(t, target, "") + } func TestSetCellFormula(t *testing.T) { @@ -372,32 +395,6 @@ func TestSetSheetBackgroundErrors(t *testing.T) { assert.EqualError(t, err, "unsupported image extension") } -func TestMergeCell(t *testing.T) { - f, err := OpenFile(filepath.Join("test", "Book1.xlsx")) - if !assert.NoError(t, err) { - t.FailNow() - } - - f.MergeCell("Sheet1", "D9", "D9") - f.MergeCell("Sheet1", "D9", "E9") - f.MergeCell("Sheet1", "H14", "G13") - f.MergeCell("Sheet1", "C9", "D8") - f.MergeCell("Sheet1", "F11", "G13") - f.MergeCell("Sheet1", "H7", "B15") - f.MergeCell("Sheet1", "D11", "F13") - f.MergeCell("Sheet1", "G10", "K12") - f.SetCellValue("Sheet1", "G11", "set value in merged cell") - f.SetCellInt("Sheet1", "H11", 100) - f.SetCellValue("Sheet1", "I11", float64(0.5)) - f.SetCellHyperLink("Sheet1", "J11", "https://github.com/360EntSecGroup-Skylar/excelize", "External") - f.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)") - f.GetCellValue("Sheet1", "H11") - f.GetCellValue("Sheet2", "A6") // Merged cell ref is single coordinate. - f.GetCellFormula("Sheet1", "G12") - - assert.NoError(t, f.SaveAs(filepath.Join("test", "TestMergeCell.xlsx"))) -} - // TestWriteArrayFormula tests the extended options of SetCellFormula by writing an array function // to a workbook. In the resulting file, the lines 2 and 3 as well as 4 and 5 should have matching // contents. @@ -913,13 +910,18 @@ func TestAddShape(t *testing.T) { t.FailNow() } - f.AddShape("Sheet1", "A30", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`) - f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`) - f.AddShape("Sheet1", "C30", `{"type":"rect","paragraph":[]}`) - f.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "paragraph":[{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777","underline":"single"}}], "height": 90}`) - f.AddShape("Sheet3", "H1", "") + assert.NoError(t, f.AddShape("Sheet1", "A30", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`)) + assert.NoError(t, f.AddShape("Sheet1", "B30", `{"type":"rect","paragraph":[{"text":"Rectangle"},{}]}`)) + assert.NoError(t, f.AddShape("Sheet1", "C30", `{"type":"rect","paragraph":[]}`)) + assert.EqualError(t, f.AddShape("Sheet3", "H1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "paragraph":[{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777","underline":"single"}}], "height": 90}`), "sheet Sheet3 is not exist") + assert.EqualError(t, f.AddShape("Sheet3", "H1", ""), "unexpected end of JSON input") + assert.EqualError(t, f.AddShape("Sheet1", "A", `{"type":"rect","paragraph":[{"text":"Rectangle","font":{"color":"CD5C5C"}},{"text":"Shape","font":{"bold":true,"color":"2980B9"}}]}`), `cannot convert cell "A" to coordinates: invalid cell name "A"`) + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape1.xlsx"))) - assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape.xlsx"))) + // Test add first shape for given sheet. + f = NewFile() + assert.NoError(t, f.AddShape("Sheet1", "A1", `{"type":"ellipseRibbon", "color":{"line":"#4286f4","fill":"#8eb9ff"}, "paragraph":[{"font":{"bold":true,"italic":true,"family":"Times New Roman","size":36,"color":"#777777","underline":"single"}}], "height": 90}`)) + assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddShape2.xlsx"))) } func TestAddComments(t *testing.T) { @@ -1278,3 +1280,9 @@ func fillCells(f *File, sheet string, colCount, rowCount int) { } } } + +func BenchmarkOpenFile(b *testing.B) { + for i := 0; i < b.N; i++ { + OpenFile(filepath.Join("test", "Book1.xlsx")) + } +} |
