qax-os/excelize · error

ErrSparkline

ErrSparkline

Error message

must have the same number of 'Location' and 'Range' parameters

What it means

Sentinel error ErrSparklineLocation returned by AddSparkline when the SparklineOptions.Location slice is empty; the guard `len(opts.Location) < 1` fires before any worksheet mutation, meaning no sparkline anchor cells were supplied.

Source

Thrown at errors.go:158

	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")
	// ErrSparklineRange defined the error message on missing sparkline Range
	// parameters
	ErrSparklineRange = errors.New("parameter 'Range' is required")
	// ErrSparklineStyle defined the error message on receive the invalid
	// sparkline Style parameters.
	ErrSparklineStyle = errors.New("parameter 'Style' value must be an integer from 0 to 35")
	// ErrSparklineType defined the error message on receive the invalid
	// sparkline Type parameters.
	ErrSparklineType = errors.New("parameter 'Type' value must be one of 'line', 'column' or 'win_loss'")
	// ErrTotalSheetHyperlinks defined the error message on hyperlinks count
	// overflow.
	ErrTotalSheetHyperlinks = errors.New("over maximum limit hyperlinks in a worksheet")
	// ErrTransparency defined the error message for receiving a transparency
	// value exceeds limit.
	ErrTransparency = errors.New("transparency value must be an integer from 0 to 100")

View on GitHub (pinned to f2483381fb)

Solutions

  1. Ensure len(opts.Location) == len(opts.Range) before calling AddSparkline
  2. Build both slices in the same loop so they stay paired
  3. Use the min length of both slices or reject the input early

Example fix

// before
opts := &SparklineOptions{Location: []string{"F3"}, Range: []string{"A1:E1", "A2:E2"}}
f.AddSparkline("Sheet1", opts)
// after
if len(opts.Location) != len(opts.Range) {
    return errors.New("Location and Range must have equal length")
}
f.AddSparkline("Sheet1", opts)
Defensive patterns

Strategy: validation

Validate before calling

if len(opts.Location) != len(opts.Range) {
    return fmt.Errorf("Location (%d) and Range (%d) counts differ", len(opts.Location), len(opts.Range))
}

Type guard

func sparklineOptsValid(o *excelize.SparklineOptions) bool {
    return len(o.Location) > 0 && len(o.Location) == len(o.Range)
}

Try / catch

if err := f.AddSparkline("Sheet1", opts); err != nil {
    if errors.Is(err, excelize.ErrSparkline) {
        // rebalance Location/Range slices
    }
    return err
}

Prevention

When it happens

Trigger: Calling f.AddSparkline("Sheet1", &SparklineOptions{Location: ["F3"], Range: ["A1:E1","A2:E2"]}) or any mismatch of len(Location) vs len(Range), via parseFormatAddSparklineSet in sparkline.go.

Common situations: Building sparkline options from loops or config where locations and ranges are collected separately and drift out of sync; multi-sparkline calls where one list got an extra entry.

Related errors


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