{"record":{"id":"28a22a6f1b498d37","repo":"qax-os/excelize","slug":"the-same-name-already-exists-on-the-scope","errorCode":null,"errorMessage":"the same name already exists on the scope","messagePattern":"the same name already exists on the scope","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":55,"sourceCode":"\tErrColumnNumber = fmt.Errorf(\"the column number must be greater than or equal to %d and less than or equal to %d\", MinColumns, MaxColumns)\n\t// ErrColumnWidth defined the error message on receive an invalid column\n\t// width.\n\tErrColumnWidth = fmt.Errorf(\"the width of the column must be less than or equal to %d characters\", MaxColumnWidth)\n\t// ErrCoordinates defined the error message on invalid coordinates tuples\n\t// length.\n\tErrCoordinates = errors.New(\"coordinates length must be 4\")\n\t// ErrCustomNumFmt defined the error message on receive the empty custom\n\t// number format.\n\tErrCustomNumFmt = errors.New(\"custom number format can not be empty\")\n\t// ErrDataValidationFormulaLength defined the error message for receiving a\n\t// data validation formula length that exceeds the limit.\n\tErrDataValidationFormulaLength = fmt.Errorf(\"data validation must be 0-%d characters\", MaxFieldLength)\n\t// ErrDataValidationRange defined the error message on set decimal range\n\t// exceeds limit.\n\tErrDataValidationRange = errors.New(\"data validation range exceeds limit\")\n\t// ErrDefinedNameDuplicate defined the error message on the same name\n\t// already exists on the scope.\n\tErrDefinedNameDuplicate = errors.New(\"the same name already exists on the scope\")\n\t// ErrDefinedNameScope defined the error message on not found defined name\n\t// in the given scope.\n\tErrDefinedNameScope = errors.New(\"no defined name on the scope\")\n\t// ErrExistsSheet defined the error message on given sheet already exists.\n\tErrExistsSheet = errors.New(\"the same name sheet already exists\")\n\t// ErrExistsTableName defined the error message on given table already\n\t// exists.\n\tErrExistsTableName = errors.New(\"the same name table already exists\")\n\t// ErrFillType defined the error message on receive an invalid fill type.\n\tErrFillType = errors.New(\"fill type value must be one of 'gradient' or 'pattern'\")\n\t// ErrFillGradientColor defined the error message on receive an invalid fill\n\t// color for 'gradient' type.\n\tErrFillGradientColor = errors.New(\"fill color value must be an array of two colors for 'gradient' type\")\n\t// ErrFillGradientShading defined the error message on receive an invalid\n\t// fill shading for 'gradient' type.\n\tErrFillGradientShading = errors.New(\"fill shading value must be between 0 and 16 for 'gradient' type\")\n\t// ErrFillPatternColor defined the error message on receive an invalid fill\n\t// color for 'pattern' type.","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/qax-os/excelize/blob/f2483381fbfbe432a6baa98d48a1c277183f3285/errors.go#L37-L73","documentation":"ErrDefinedNameDuplicate is returned by SetDefinedName when a defined name with the same Name already exists on the same scope (workbook-wide or a specific sheet). Excel requires defined names to be unique within their scope, so Excelize rejects the duplicate at sheet.go:1819 rather than overwriting silently.","triggerScenarios":"Calling f.SetDefinedName(&DefinedName{Name: X, RefersTo: ..., Scope: s}) twice with the same X and same scope; e.g. re-running setup code that defines \"name1\" -> \"Sheet1!$A$2:$D$5\" without first deleting the existing definition.","commonSituations":"Idempotent workbook-generation scripts run twice against the same file; copying template macros that already contain defined names; updating a name's RefersTo without deleting the old one first.","solutions":["Call f.DeleteDefinedName(&DefinedName{Name: X, Scope: s}) before re-setting the name.","Track already-created defined names in your generation code so SetDefinedName runs only once.","If the goal is to update, delete-then-set in one helper function."],"exampleFix":"// before\nf.SetDefinedName(&DefinedName{Name: \"name1\", RefersTo: \"Sheet1!$A$2:$D$5\"})\nf.SetDefinedName(&DefinedName{Name: \"name1\", RefersTo: \"Sheet1!$B$2:$D$5\"}) // ErrDefinedNameDuplicate\n// after\nf.DeleteDefinedName(&DefinedName{Name: \"name1\"})\nf.SetDefinedName(&DefinedName{Name: \"name1\", RefersTo: \"Sheet1!$B$2:$D$5\"})","handlingStrategy":"validation","validationCode":"// Excelize exposes no public name-lookup helper; track names yourself:\ncreated := map[string]bool{}\nkey := fmt.Sprintf(\"%s:%d\", name, scope)\nif created[key] { f.DeleteDefinedName(&excelize.DefinedName{Name: name, Scope: scope}) }\nf.SetDefinedName(&excelize.DefinedName{Name: name, RefersTo: ref, Scope: scope})\ncreated[key] = true","typeGuard":"func hasDefinedName(f *excelize.File, name string, scope int) bool {\n\terr := f.DeleteDefinedName(&excelize.DefinedName{Name: name, Scope: scope})\n\treturn err != excelize.ErrDefinedNameScope\n}","tryCatchPattern":"err := f.SetDefinedName(dn)\nif errors.Is(err, excelize.ErrDefinedNameDuplicate) {\n\t_ = f.DeleteDefinedName(&excelize.DefinedName{Name: dn.Name, Scope: dn.Scope})\n\terr = f.SetDefinedName(dn)\n}","preventionTips":["Delete the defined name before setting it when your code may run more than once","Keep a registry of names/scopes your generator has created","Make setup functions idempotent by treating ErrDefinedNameDuplicate as delete-and-retry"],"tags":["excelize","defined-names","duplicate-name"],"backgroundTag":"duplicate-identifier","analyzedSha":"f2483381fbfbe432a6baa98d48a1c277183f3285","analyzedAt":"2026-09-02T01:26:19.299Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}