summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2019-10-26 20:55:24 +0800
committerxuri <xuri.me@gmail.com>2019-10-26 20:55:24 +0800
commit5e418ebd665f38d1211b27d7157ec7e5868451bc (patch)
treea2e72dc8d5690856e8157e8208539c780ec631d6 /sheet.go
parent87390cdd99b3afbe07daeef9abe96f57d03cb352 (diff)
Resolve #507, add the new function `DeleteDefinedName`
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/sheet.go b/sheet.go
index 9e8d504..335c4fc 100644
--- a/sheet.go
+++ b/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 {