{"record":{"id":"973df0c55443b266","repo":"qax-os/excelize","slug":"errsheetidx","errorCode":"ErrSheetIdx","errorMessage":"invalid worksheet index","messagePattern":"invalid worksheet index","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":143,"sourceCode":"\t// parameter.\n\tErrParameterRequired = errors.New(\"parameter is required\")\n\t// ErrPasswordLengthInvalid defined the error message on invalid password\n\t// length.\n\tErrPasswordLengthInvalid = errors.New(\"password length invalid\")\n\t// ErrPivotTableShowValuesAsBaseField defined the error message on enable\n\t// this kind of \"show values as\" type requires a base field.\n\tErrPivotTableShowValuesAsBaseField = errors.New(\"this kind of show values as type requires a base field\")\n\t// ErrPivotTableShowValuesAsBaseItem defined the error message on enable\n\t// this kind of \"show values as\" type and base field requires a base item.\n\tErrPivotTableShowValuesAsBaseItem = errors.New(\"this kind of show values as type and base field requires a base item\")\n\t// ErrPivotTableClassicLayout defined the error message on enable\n\t// ClassicLayout and CompactData in the same time.\n\tErrPivotTableClassicLayout = errors.New(\"cannot enable ClassicLayout and CompactData in the same time\")\n\t// ErrSave defined the error message for saving file.\n\tErrSave = errors.New(\"no path defined for file, consider File.WriteTo or File.Write\")\n\t// ErrSheetIdx defined the error message on receive the invalid worksheet\n\t// index.\n\tErrSheetIdx = errors.New(\"invalid worksheet index\")\n\t// ErrSheetNameBlank defined the error message on receive the blank sheet\n\t// name.\n\tErrSheetNameBlank = errors.New(\"the sheet name can not be blank\")\n\t// ErrSheetNameInvalid defined the error message on receive the sheet name\n\t// contains invalid characters.\n\tErrSheetNameInvalid = errors.New(\"the sheet can not contain any of the characters :\\\\/?*[or]\")\n\t// ErrSheetNameLength defined the error message on receiving the sheet\n\t// name length exceeds the limit.\n\tErrSheetNameLength = fmt.Errorf(\"the sheet name length exceeds the %d characters limit\", MaxSheetNameLength)\n\t// ErrSheetNameSingleQuote defined the error message on the first or last\n\t// character of the sheet name was a single quote.\n\tErrSheetNameSingleQuote = errors.New(\"the first or last character of the sheet name can not be a single quote\")\n\t// ErrSparkline defined the error message on receive the invalid sparkline\n\t// parameters.\n\tErrSparkline = errors.New(\"must have the same number of 'Location' and 'Range' parameters\")\n\t// ErrSparklineLocation defined the error message on missing Location\n\t// parameters\n\tErrSparklineLocation = errors.New(\"parameter 'Location' is required\")","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/qax-os/excelize/blob/f2483381fbfbe432a6baa98d48a1c277183f3285/errors.go#L125-L161","documentation":"ErrSheetIdx is returned by CopySheet when the from or to worksheet index is invalid: negative, equal to each other, or not naming an existing sheet. The public CopySheet validates indexes; the private copySheet validates sheet names and can return ErrSheetNameBlank instead.","triggerScenarios":"f.CopySheet(-1, -2) (excelize_test.go:1090); CopySheet with from == to; CopySheet with index values for sheets that do not exist, e.g. after deleting sheets and reusing stale indexes.","commonSituations":"Storing sheet indexes from earlier code that are stale after sheet deletion/reordering; assuming 1-based vs 0-based indexing; computing target index dynamically and hitting an unused slot.","solutions":["Look up valid indexes with f.GetSheetIndex(sheetName) before copying","Ensure from and to are distinct and both exist (GetSheetName(from) != \"\" and GetSheetName(to) != \"\")","Use a new unique index for the destination copy"],"exampleFix":"// before\nerr := f.CopySheet(-1, -2)\n// after\nfrom := f.GetSheetIndex(\"Sheet1\")\nto := f.NewSheet(\"Sheet2Copy\")\nif from < 1 || to < 1 || from == to { return errors.New(\"invalid sheet index\") }\nerr := f.CopySheet(from, to)","handlingStrategy":"validation","validationCode":"from := f.GetSheetIndex(fromName)\nto := f.GetSheetIndex(toName)\nif from < 1 || to < 1 || from == to {\n    return errors.New(\"invalid worksheet index\")\n}\nerr := f.CopySheet(from, to)","typeGuard":"func copyableIndex(f *excelize.File, idx int) bool { return idx >= 1 && f.GetSheetName(idx) != \"\" }","tryCatchPattern":"if err := f.CopySheet(from, to); errors.Is(err, excelize.ErrSheetIdx) { // re-resolve indexes via GetSheetIndex and retry }","preventionTips":["Resolve sheet names to fresh indexes with GetSheetIndex instead of caching indexes","Ensure from != to and both sheets exist before copying","Create the destination sheet first with NewSheet, then copy"],"tags":["go","excelize","worksheet","index-validation"],"backgroundTag":"invalid-worksheet-index","analyzedSha":"f2483381fbfbe432a6baa98d48a1c277183f3285","analyzedAt":"2026-09-02T01:26:19.299Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}