Help us shape the future of Go! Take the 2026 Go Developer Survey

formula

package
v0.1.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package formula parses, prints and evaluates the spreadsheet formulas lfm builds: the portable subset Excel 2019+, Google Sheets and LibreOffice all compute the same way. Placeholders in square brackets ([Log.amount], [.amount], [income]) name columns and cells by meaning; a resolver turns them into references for one edition, so a sheet can be renamed per language without touching a formula.

Index

Constants

View Source
const (
	ErrDiv0  = "#DIV/0!"
	ErrNA    = "#N/A"
	ErrValue = "#VALUE!"
	ErrRef   = "#REF!"
	ErrName  = "#NAME?"
	ErrNum   = "#NUM!"
)

The error values.

Variables

This section is empty.

Functions

func CellName

func CellName(col, row int) string

CellName is A1 for column 1, row 1.

func ColumnName

func ColumnName(n int) string

ColumnName turns 1 into A and 27 into AA.

func ColumnNumber

func ColumnNumber(letters string) int

ColumnNumber turns A into 1 and AA into 27.

func Date

func Date(serial float64) time.Time

Date is the calendar date of a serial.

func Definable

func Definable(name string) bool

Definable reports whether a name can be an Excel defined name: not a cell address (tax1 is column TAX, row 1), not R1C1 notation, not a logical value or a function's name. Google Sheets and LibreOffice accept the same names.

func Functions

func Functions() []string

Functions lists the portable function names, sorted.

func Print

func Print(n Node) string

Print writes n in the syntax xlsx files store: English function names with the prefixes newer functions need, commas between arguments, a dot as the decimal mark and quoted sheet names. Every app shows the formula in its own language from this.

func PrintAt

func PrintAt(n Node, row int) string

PrintAt prints n for a cell in row, which Here references take.

func QuoteSheet

func QuoteSheet(name string) string

QuoteSheet quotes a sheet name for a reference: 'Mein Budget'!A1.

func Serial

func Serial(t time.Time) float64

Serial is the date serial of t: days since 30 December 1899, as Excel, Sheets and LibreOffice count from March 1900.

Types

type Area

type Area struct {
	Sheet    string
	From, To Ref
}

Area is a rectangle of cells on one sheet.

type AreaBook

type AreaBook interface {
	Book
	Area(sheet string, c1, r1, c2, r2 int) (rows [][]Value, cells *Run)
}

AreaBook is a Book that reads whole areas itself, such as one that memoizes them; the rows it returns are never modified.

type Binary

type Binary struct {
	Op   string
	L, R Node
}

Binary is an operator between two operands.

type Book

type Book interface {
	Cell(sheet string, col, row int) Value
}

Book gives the evaluator the cells of a workbook. A cell holding a formula returns its evaluated value; the book evaluates it on demand.

type Bool

type Bool bool

Num, Str, Bool and Err are literals.

type Call

type Call struct {
	Name string // upper case, without prefix
	Args []Node
}

Call is a function call.

type Context

type Context struct {
	Book  Book
	Sheet string // the sheet of the cell, for references without a sheet
	Row   int    // the row of the cell, for Here references
	Today time.Time
	// Volatile is set when the formula reads TODAY(): its value changes
	// with the day it is computed.
	Volatile *bool
	// Unportable receives what the formula did that the apps compute
	// differently, such as joining TRUE into text.
	Unportable func(string)
}

Context is where a formula is evaluated.

type Err

type Err string

Num, Str, Bool and Err are literals.

type Kind

type Kind uint8

Kind is the type of a value.

const (
	Blank Kind = iota
	Number
	Text
	Logical
	Error
	Array // a rectangle of values: a range or an array operation's result
)

Value kinds.

type Node

type Node interface {
	// contains filtered or unexported methods
}

Node is a parsed formula.

func Parse

func Parse(src string, resolve Resolver) (Node, error)

Parse parses src, which may start with "=". Placeholders go through resolve; functions outside the portable set are an error.

type Num

type Num float64

Num, Str, Bool and Err are literals.

type Ref

type Ref struct {
	Sheet          string
	Col, Row       int
	AbsCol, AbsRow bool
	Here           bool
	Name           string // a defined name the file stores instead of the address
}

Ref is one cell; Col and Row count from 1. A Here reference is in the row of the cell the formula belongs to: one parsed formula serves every row of a table column.

type Resolver

type Resolver func(name string) (Node, error)

Resolver turns a placeholder's text (without brackets) into a reference.

type Run

type Run struct {
	Values []Value
	// contains filtered or unexported fields
}

Run is an area's cells in reading order. Its index of texts, which lets SUMIFS and the other *IFS functions find the rows equal to a text without scanning, is built on first use.

type Str

type Str string

Num, Str, Bool and Err are literals.

type Unary

type Unary struct {
	Op string
	X  Node
}

Unary is -x, +x or x%.

type Value

type Value struct {
	Kind Kind
	Num  float64
	Str  string // text, or the error code
	Rows [][]Value
	// contains filtered or unexported fields
}

Value is the result of a formula or the content of a cell.

func BoolValue

func BoolValue(b bool) Value

BoolValue is b as a logical value.

func ErrorValue

func ErrorValue(code string) Value

func Eval

func Eval(n Node, c *Context) Value

Eval evaluates n.

func NumberValue

func NumberValue(f float64) Value

NumberValue is f as a value.

func ReadArea

func ReadArea(b Book, sheet string, c1, r1, c2, r2 int) (rows [][]Value, cells []Value)

ReadArea reads an area cell by cell, row by row. The rows share one backing slice, which it also returns: the area's cells in reading order.

func TextValue

func TextValue(s string) Value

TextValue is s as a value.

func (Value) String

func (v Value) String() string

String shows a value the way a test or a finding names it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL