forked from p30928647/excelize
Resolve #507, add the new function `DeleteDefinedName`
This commit is contained in:
parent
87390cdd99
commit
5e418ebd66
45
cell_test.go
45
cell_test.go
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -73,6 +74,50 @@ func TestSetCellFloat(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, "123.42", val, "A1 should be 123.42")
|
||||
})
|
||||
f := NewFile()
|
||||
assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
}
|
||||
|
||||
func TestSetCellValue(t *testing.T) {
|
||||
f := NewFile()
|
||||
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
}
|
||||
|
||||
func TestSetCellBool(t *testing.T) {
|
||||
f := NewFile()
|
||||
assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
}
|
||||
|
||||
func TestGetCellFormula(t *testing.T) {
|
||||
f := NewFile()
|
||||
f.GetCellFormula("Sheet", "A1")
|
||||
}
|
||||
|
||||
func TestMergeCell(t *testing.T) {
|
||||
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.EqualError(t, f.MergeCell("Sheet1", "A", "B"), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
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")))
|
||||
}
|
||||
|
||||
func ExampleFile_SetCellFloat() {
|
||||
|
|
|
@ -53,10 +53,15 @@ func TestOutlineLevel(t *testing.T) {
|
|||
assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
|
||||
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
|
||||
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
|
||||
// Test set row outline level on not exists worksheet.
|
||||
assert.EqualError(t, f.SetRowOutlineLevel("SheetN", 1, 4), "sheet SheetN is not exist")
|
||||
// Test get row outline level on not exists worksheet.
|
||||
_, err := f.GetRowOutlineLevel("SheetN", 1)
|
||||
assert.EqualError(t, err, "sheet SheetN is not exist")
|
||||
|
||||
// Test set and get column outline level with illegal cell coordinates.
|
||||
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
|
||||
_, err := f.GetColOutlineLevel("Sheet1", "*")
|
||||
_, err = f.GetColOutlineLevel("Sheet1", "*")
|
||||
assert.EqualError(t, err, `invalid column name "*"`)
|
||||
|
||||
// Test set column outline level on not exists worksheet.
|
||||
|
|
|
@ -372,32 +372,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 +887,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) {
|
||||
|
|
26
rows_test.go
26
rows_test.go
|
@ -42,7 +42,6 @@ func TestRows(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// test bug https://github.com/360EntSecGroup-Skylar/excelize/issues/502
|
||||
func TestRowsIterator(t *testing.T) {
|
||||
const (
|
||||
sheet2 = "Sheet2"
|
||||
|
@ -59,6 +58,10 @@ func TestRowsIterator(t *testing.T) {
|
|||
require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected")
|
||||
}
|
||||
assert.Equal(t, expectedNumRow, rowCount)
|
||||
|
||||
rows = &Rows{f: xlsx, rows: []xlsxRow{{C: []xlsxC{{R: "A"}}}}, curRow: 1}
|
||||
_, err = rows.Columns()
|
||||
assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
|
||||
}
|
||||
|
||||
func TestRowsError(t *testing.T) {
|
||||
|
@ -113,22 +116,25 @@ func TestRowHeight(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestRowVisibility(t *testing.T) {
|
||||
xlsx, err := prepareTestBook1()
|
||||
f, err := prepareTestBook1()
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
xlsx.NewSheet("Sheet3")
|
||||
assert.NoError(t, xlsx.SetRowVisible("Sheet3", 2, false))
|
||||
assert.NoError(t, xlsx.SetRowVisible("Sheet3", 2, true))
|
||||
xlsx.GetRowVisible("Sheet3", 2)
|
||||
xlsx.GetRowVisible("Sheet3", 25)
|
||||
assert.EqualError(t, xlsx.SetRowVisible("Sheet3", 0, true), "invalid row number 0")
|
||||
f.NewSheet("Sheet3")
|
||||
assert.NoError(t, f.SetRowVisible("Sheet3", 2, false))
|
||||
assert.NoError(t, f.SetRowVisible("Sheet3", 2, true))
|
||||
f.GetRowVisible("Sheet3", 2)
|
||||
f.GetRowVisible("Sheet3", 25)
|
||||
assert.EqualError(t, f.SetRowVisible("Sheet3", 0, true), "invalid row number 0")
|
||||
assert.EqualError(t, f.SetRowVisible("SheetN", 2, false), "sheet SheetN is not exist")
|
||||
|
||||
visible, err := xlsx.GetRowVisible("Sheet3", 0)
|
||||
visible, err := f.GetRowVisible("Sheet3", 0)
|
||||
assert.Equal(t, false, visible)
|
||||
assert.EqualError(t, err, "invalid row number 0")
|
||||
_, err = f.GetRowVisible("SheetN", 1)
|
||||
assert.EqualError(t, err, "sheet SheetN is not exist")
|
||||
|
||||
assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx")))
|
||||
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx")))
|
||||
}
|
||||
|
||||
func TestRemoveRow(t *testing.T) {
|
||||
|
|
28
sheet.go
28
sheet.go
|
@ -1271,7 +1271,7 @@ func (f *File) SetDefinedName(definedName *DefinedName) error {
|
|||
scope = f.GetSheetName(*dn.LocalSheetID + 1)
|
||||
}
|
||||
if scope == definedName.Scope && dn.Name == definedName.Name {
|
||||
return errors.New("the same name already exists on scope")
|
||||
return errors.New("the same name already exists on the scope")
|
||||
}
|
||||
}
|
||||
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName, d)
|
||||
|
@ -1283,6 +1283,32 @@ func (f *File) SetDefinedName(definedName *DefinedName) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// DeleteDefinedName provides a function to delete the defined names of the
|
||||
// workbook or worksheet. If not specified scope, the default scope is
|
||||
// workbook. For example:
|
||||
//
|
||||
// f.DeleteDefinedName(&excelize.DefinedName{
|
||||
// Name: "Amount",
|
||||
// Scope: "Sheet2",
|
||||
// })
|
||||
//
|
||||
func (f *File) DeleteDefinedName(definedName *DefinedName) error {
|
||||
wb := f.workbookReader()
|
||||
if wb.DefinedNames != nil {
|
||||
for idx, dn := range wb.DefinedNames.DefinedName {
|
||||
var scope string
|
||||
if dn.LocalSheetID != nil {
|
||||
scope = f.GetSheetName(*dn.LocalSheetID + 1)
|
||||
}
|
||||
if scope == definedName.Scope && dn.Name == definedName.Name {
|
||||
wb.DefinedNames.DefinedName = append(wb.DefinedNames.DefinedName[:idx], wb.DefinedNames.DefinedName[idx+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return errors.New("no defined name on the scope")
|
||||
}
|
||||
|
||||
// GetDefinedName provides a function to get the defined names of the workbook
|
||||
// or worksheet.
|
||||
func (f *File) GetDefinedName() []DefinedName {
|
||||
|
|
|
@ -210,8 +210,16 @@ func TestDefinedName(t *testing.T) {
|
|||
Name: "Amount",
|
||||
RefersTo: "Sheet1!$A$2:$D$5",
|
||||
Comment: "defined name comment",
|
||||
}), "the same name already exists on scope")
|
||||
}), "the same name already exists on the scope")
|
||||
assert.EqualError(t, f.DeleteDefinedName(&excelize.DefinedName{
|
||||
Name: "No Exist Defined Name",
|
||||
}), "no defined name on the scope")
|
||||
assert.Exactly(t, "Sheet1!$A$2:$D$5", f.GetDefinedName()[1].RefersTo)
|
||||
assert.NoError(t, f.DeleteDefinedName(&excelize.DefinedName{
|
||||
Name: "Amount",
|
||||
}))
|
||||
assert.Exactly(t, "Sheet1!$A$2:$D$5", f.GetDefinedName()[0].RefersTo)
|
||||
assert.Exactly(t, 1, len(f.GetDefinedName()))
|
||||
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestDefinedName.xlsx")))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue