diff options
author | xuri <xuri.me@gmail.com> | 2019-10-26 20:55:24 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2019-10-26 20:55:24 +0800 |
commit | 5e418ebd665f38d1211b27d7157ec7e5868451bc (patch) | |
tree | a2e72dc8d5690856e8157e8208539c780ec631d6 /sheet.go | |
parent | 87390cdd99b3afbe07daeef9abe96f57d03cb352 (diff) |
Resolve #507, add the new function `DeleteDefinedName`
Diffstat (limited to 'sheet.go')
-rw-r--r-- | sheet.go | 28 |
1 files changed, 27 insertions, 1 deletions
@@ -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 { |