qax-os/excelize · error

view index %d out of range

Error message

view index %d out of range

What it means

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.

Source

Thrown at errors.go:423

	return fmt.Errorf("unsupported chart type %d", chartType)
}

// newUnsupportedPivotCacheSourceType defined the error message on receiving the
// source type of pivot table cache.
func newUnsupportedPivotCacheSourceType(sourceType string) error {
	return fmt.Errorf("unsupported pivot table cache source type: %s", sourceType)
}

// newUnzipSizeLimitError defined the error message on unzip size exceeds the
// limit.
func newUnzipSizeLimitError(unzipSizeLimit int64) error {
	return fmt.Errorf("unzip size exceeds the %d bytes limit", unzipSizeLimit)
}

// newViewIdxError defined the error message on receiving a invalid sheet view
// index.
func newViewIdxError(viewIndex int) error {
	return fmt.Errorf("view index %d out of range", viewIndex)
}

View on GitHub (pinned to f2483381fb)

Solutions

  1. Use viewIndex 0 (the default, first sheet view) unless you explicitly created additional views.
  2. Query the existing view count first with f.GetSheetViewOptions/GetSheetView to determine valid indices.
  3. Create the extra view you intend to manipulate before referencing it.
  4. Fix off-by-one loops so they iterate indices 0..n-1 over existing views.

Example fix

// before
f.SetSheetView("Sheet1", 1, &excelize.ViewOptions{ZoomScale: 1.5}) // only one view exists
// after
f.SetSheetView("Sheet1", 0, &excelize.ViewOptions{ZoomScale: 1.5})
Defensive patterns

Strategy: validation

Validate before calling

func safeSetView(f *excelize.File, sheet string, viewIndex int, opts *excelize.ViewOptions) error {
    if viewIndex < 0 {
        return fmt.Errorf("viewIndex must be >= 0")
    }
    // probe: reading view viewIndex fails if it does not exist
    if _, err := f.GetSheetView(sheet, viewIndex); err != nil {
        return fmt.Errorf("sheet %s has no view index %d", sheet, viewIndex)
    }
    return f.SetSheetView(sheet, viewIndex, opts)
}

Try / catch

err := f.SetSheetView(sheet, idx, &excelize.ViewOptions{ZoomScale: 1.5})
if err != nil {
    if strings.Contains(err.Error(), "view index") && strings.Contains(err.Error(), "out of range") {
        return f.SetSheetView(sheet, 0, &excelize.ViewOptions{ZoomScale: 1.5})
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of qax-os/excelize@f2483381fb (2026-09-02). Data as JSON: /api/errors/7b1c379d5b7e7358. Report an issue: GitHub.