{"record":{"id":"7b1c379d5b7e7358","repo":"qax-os/excelize","slug":"view-index-d-out-of-range","errorCode":null,"errorMessage":"view index %d out of range","messagePattern":"view index (.+?) out of range","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":423,"sourceCode":"\treturn fmt.Errorf(\"unsupported chart type %d\", chartType)\n}\n\n// newUnsupportedPivotCacheSourceType defined the error message on receiving the\n// source type of pivot table cache.\nfunc newUnsupportedPivotCacheSourceType(sourceType string) error {\n\treturn fmt.Errorf(\"unsupported pivot table cache source type: %s\", sourceType)\n}\n\n// newUnzipSizeLimitError defined the error message on unzip size exceeds the\n// limit.\nfunc newUnzipSizeLimitError(unzipSizeLimit int64) error {\n\treturn fmt.Errorf(\"unzip size exceeds the %d bytes limit\", unzipSizeLimit)\n}\n\n// newViewIdxError defined the error message on receiving a invalid sheet view\n// index.\nfunc newViewIdxError(viewIndex int) error {\n\treturn fmt.Errorf(\"view index %d out of range\", viewIndex)\n}\n","sourceCodeStart":405,"sourceCodeEnd":425,"githubUrl":"https://github.com/qax-os/excelize/blob/f2483381fbfbe432a6baa98d48a1c277183f3285/errors.go#L405-L425","documentation":"Excelize throws this when a sheet view index used with view-related APIs is outside the range of existing sheet views for that worksheet. Worksheets normally have one or a few views; referencing a view beyond that count is invalid.","triggerScenarios":"Calling f.SetSheetView / GetSheetView (and related view APIs) with a viewIndex parameter greater than or equal to the number of sheetView elements in the worksheet XML, e.g. viewIndex 1 when the sheet has only one view.","commonSituations":"Assuming views are 1-based and passing 1 on a single-view sheet; copying sample code that used a second view; loops over a hardcoded number of views.","solutions":["Use viewIndex 0 (the default, first sheet view) unless you explicitly created additional views.","Query the existing view count first with f.GetSheetViewOptions/GetSheetView to determine valid indices.","Create the extra view you intend to manipulate before referencing it.","Fix off-by-one loops so they iterate indices 0..n-1 over existing views."],"exampleFix":"// before\nf.SetSheetView(\"Sheet1\", 1, &excelize.ViewOptions{ZoomScale: 1.5}) // only one view exists\n// after\nf.SetSheetView(\"Sheet1\", 0, &excelize.ViewOptions{ZoomScale: 1.5})","handlingStrategy":"validation","validationCode":"func safeSetView(f *excelize.File, sheet string, viewIndex int, opts *excelize.ViewOptions) error {\n    if viewIndex < 0 {\n        return fmt.Errorf(\"viewIndex must be >= 0\")\n    }\n    // probe: reading view viewIndex fails if it does not exist\n    if _, err := f.GetSheetView(sheet, viewIndex); err != nil {\n        return fmt.Errorf(\"sheet %s has no view index %d\", sheet, viewIndex)\n    }\n    return f.SetSheetView(sheet, viewIndex, opts)\n}","typeGuard":null,"tryCatchPattern":"err := f.SetSheetView(sheet, idx, &excelize.ViewOptions{ZoomScale: 1.5})\nif err != nil {\n    if strings.Contains(err.Error(), \"view index\") && strings.Contains(err.Error(), \"out of range\") {\n        return f.SetSheetView(sheet, 0, &excelize.ViewOptions{ZoomScale: 1.5})\n    }\n    return err\n}","preventionTips":["Default to viewIndex 0; only use higher indices if you created extra views","Remember view indices are 0-based","Probe with GetSheetView before writing to an index","Avoid hardcoded view counts in loops"],"tags":["go","excelize","sheet-view","index-out-of-range"],"backgroundTag":"view-index-out-of-range","analyzedSha":"f2483381fbfbe432a6baa98d48a1c277183f3285","analyzedAt":"2026-09-02T01:26:19.299Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}