qax-os/excelize · error

ErrSheetIdx

ErrSheetIdx

Error message

invalid worksheet index

What it means

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.

Source

Thrown at errors.go:143

	// parameter.
	ErrParameterRequired = errors.New("parameter is required")
	// ErrPasswordLengthInvalid defined the error message on invalid password
	// length.
	ErrPasswordLengthInvalid = errors.New("password length invalid")
	// ErrPivotTableShowValuesAsBaseField defined the error message on enable
	// this kind of "show values as" type requires a base field.
	ErrPivotTableShowValuesAsBaseField = errors.New("this kind of show values as type requires a base field")
	// ErrPivotTableShowValuesAsBaseItem defined the error message on enable
	// this kind of "show values as" type and base field requires a base item.
	ErrPivotTableShowValuesAsBaseItem = errors.New("this kind of show values as type and base field requires a base item")
	// ErrPivotTableClassicLayout defined the error message on enable
	// ClassicLayout and CompactData in the same time.
	ErrPivotTableClassicLayout = errors.New("cannot enable ClassicLayout and CompactData in the same time")
	// ErrSave defined the error message for saving file.
	ErrSave = errors.New("no path defined for file, consider File.WriteTo or File.Write")
	// ErrSheetIdx defined the error message on receive the invalid worksheet
	// index.
	ErrSheetIdx = errors.New("invalid worksheet index")
	// ErrSheetNameBlank defined the error message on receive the blank sheet
	// name.
	ErrSheetNameBlank = errors.New("the sheet name can not be blank")
	// ErrSheetNameInvalid defined the error message on receive the sheet name
	// contains invalid characters.
	ErrSheetNameInvalid = errors.New("the sheet can not contain any of the characters :\\/?*[or]")
	// ErrSheetNameLength defined the error message on receiving the sheet
	// name length exceeds the limit.
	ErrSheetNameLength = fmt.Errorf("the sheet name length exceeds the %d characters limit", MaxSheetNameLength)
	// ErrSheetNameSingleQuote defined the error message on the first or last
	// character of the sheet name was a single quote.
	ErrSheetNameSingleQuote = errors.New("the first or last character of the sheet name can not be a single quote")
	// ErrSparkline defined the error message on receive the invalid sparkline
	// parameters.
	ErrSparkline = errors.New("must have the same number of 'Location' and 'Range' parameters")
	// ErrSparklineLocation defined the error message on missing Location
	// parameters
	ErrSparklineLocation = errors.New("parameter 'Location' is required")

View on GitHub (pinned to f2483381fb)

Solutions

  1. Look up valid indexes with f.GetSheetIndex(sheetName) before copying
  2. Ensure from and to are distinct and both exist (GetSheetName(from) != "" and GetSheetName(to) != "")
  3. Use a new unique index for the destination copy

Example fix

// before
err := f.CopySheet(-1, -2)
// after
from := f.GetSheetIndex("Sheet1")
to := f.NewSheet("Sheet2Copy")
if from < 1 || to < 1 || from == to { return errors.New("invalid sheet index") }
err := f.CopySheet(from, to)
Defensive patterns

Strategy: validation

Validate before calling

from := f.GetSheetIndex(fromName)
to := f.GetSheetIndex(toName)
if from < 1 || to < 1 || from == to {
    return errors.New("invalid worksheet index")
}
err := f.CopySheet(from, to)

Type guard

func copyableIndex(f *excelize.File, idx int) bool { return idx >= 1 && f.GetSheetName(idx) != "" }

Try / catch

if err := f.CopySheet(from, to); errors.Is(err, excelize.ErrSheetIdx) { // re-resolve indexes via GetSheetIndex and retry }

Prevention

When it happens

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

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

Related errors


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