qax-os/excelize · error

formula not valid

Error message

formula not valid

What it means

ErrInvalidFormula is returned by the formula calculator when a formula cannot be parsed or evaluated, e.g. when the operand stack is empty at a point where an operand is required, or a prefix operator like '-' has no operand. It signals a structurally invalid formula string.

Source

Thrown at errors.go:93

	// ErrFillPattern defined the error message on receive an invalid fill
	// pattern.
	ErrFillPattern = errors.New("fill pattern value must be between 0 and 18")
	// ErrFontLength defined the error message on the length of the font
	// family name overflow.
	ErrFontLength = fmt.Errorf("the length of the font family name must be less than or equal to %d", MaxFontFamilyLength)
	// ErrFontSize defined the error message on the size of the font is invalid.
	ErrFontSize = fmt.Errorf("font size must be an integer from %d to %d points", MinFontSize, MaxFontSize)
	// ErrFormControlValue defined the error message for receiving a scroll
	// value exceeds limit.
	ErrFormControlValue = fmt.Errorf("scroll value must be an integer from 0 to %d", MaxFormControlValue)
	// ErrGroupSheets defined the error message on group sheets.
	ErrGroupSheets = errors.New("group worksheet must contain an active worksheet")
	// ErrImgExt defined the error message on receive an unsupported image
	// extension.
	ErrImgExt = errors.New("unsupported image extension")
	// ErrInvalidFormula defined the error message on receive an invalid
	// formula.
	ErrInvalidFormula = errors.New("formula not valid")
	// ErrMaxFilePathLength defined the error message on receive the file path
	// length overflow.
	ErrMaxFilePathLength = fmt.Errorf("file path length exceeds maximum limit %d characters", MaxFilePathLength)
	// ErrMaxRowHeight defined the error message on receive an invalid row
	// height.
	ErrMaxRowHeight = fmt.Errorf("the height of the row must be less than or equal to %d points", MaxRowHeight)
	// ErrMaxRows defined the error message on receive a row number exceeds
	// maximum limit.
	ErrMaxRows = errors.New("row number exceeds maximum limit")
	// ErrNameLength defined the error message on receiving the defined name or
	// table name length exceeds the limit.
	ErrNameLength = fmt.Errorf("the name length exceeds the %d characters limit", MaxFieldLength)
	// ErrMaxGraphicAltTextLength defined the error message on receiving the
	// graphic alt text length exceeds the limit.
	ErrMaxGraphicAltTextLength = fmt.Errorf("the alt text length exceeds the %d characters limit", MaxGraphicAltTextLength)
	// ErrMaxGraphicNameLength defined the error message on receiving the
	// graphic name length exceeds the limit.
	ErrMaxGraphicNameLength = fmt.Errorf("the name length exceeds the %d characters limit", MaxGraphicNameLength)

View on GitHub (pinned to f2483381fb)

Solutions

  1. Inspect the formula string and fix the syntax so every operator has its operands (e.g. "=-A1" instead of "=-").
  2. Validate the formula with a parser or test evaluation via CalcCellValue before writing it.
  3. Guard string concatenation that builds formulas so empty parts cannot produce dangling operators.
  4. Escape or quote values correctly, e.g. text literals need double quotes inside the formula string.

Example fix

// before
f.SetCellFormula("A1", "=1+")
// after
f.SetCellFormula("A1", "=1+B1")
Defensive patterns

Strategy: validation

Validate before calling

if strings.HasSuffix(strings.TrimSpace(formula), "+") || strings.HasSuffix(strings.TrimSpace(formula), "-") || strings.HasSuffix(strings.TrimSpace(formula), "=") {
    return errors.New("formula ends with a dangling operator")
}

Try / catch

if _, err := f.CalcCellValue(sheet, cell); errors.Is(err, excelize.ErrInvalidFormula) {
    log.Printf("invalid formula %q: %v", formula, err)
    // fall back to storing the formula as text or repairing it
}

Prevention

When it happens

Trigger: f.SetCellFormula with formulas like "=", "=-", "=1+", or other malformed expressions; also surfaces from CalcCellValue when evaluation runs out of operands.

Common situations: Building formulas by string concatenation with empty cell references or user input, truncated formulas from templates, or copying formulas between locales/functions that were mangled.

Related errors


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