Documentation
¶
Overview ¶
Package formula is the formula language: A1 references and ranges, sheet names in references, the lexer and Pratt parser, the syntax tree, the printer that writes trees back in Sheets' spelling, and the rewriting of references when cells are copied, moved, inserted or deleted. It knows nothing of cells or values: the engine evaluates the trees, and tells the parser which functions exist.
Index ¶
- Constants
- func AxisMaps(rows bool, sp Span) (func(Addr) (Addr, bool), func(Rect) (Rect, bool))
- func ColName(c int) string
- func EachChild(n Node, fn func(Node))
- func Expr(n Node) string
- func LooksLikeRef(k string) bool
- func ParseCol(s string) (int, bool)
- func ParseLines(from, to string) (Rect, [2]Abs, bool)
- func ParseRef(s string) (Addr, Abs, bool)
- func Qualified(sheet string, r Rect) string
- func QuoteSheet(name string) string
- func RangeString(r Rect, abs [2]Abs) string
- func RefString(a Addr, abs Abs) string
- func SheetKey(name string) string
- func SplitSheet(s string) (sheet, rest string)
- func Text(n Node) string
- func WalkNames(n Node, fn func(Name))
- func WalkRefs(n Node, ref func(string, Addr), rng func(string, Rect))
- type Abs
- type Addr
- type Array
- type Binary
- type Binding
- type Bool
- type Call
- type Empty
- type Func
- type Funcs
- type Invoke
- type Local
- type Name
- type Node
- type Num
- type ParseError
- type Range
- type Rect
- type Ref
- type RefErr
- type Rewriter
- type Signature
- type Span
- type Str
- type Unary
Constants ¶
const ( MaxCols = 16384 MaxRows = 1048576 )
Worksheet bounds, matching Excel (A..XFD, 1..1048576). A reference past them isn't a reference: XFE1 reads as a name.
const MaxDepth = 1024
MaxDepth is how deeply a formula may nest: parentheses, function calls and prefix operators each open a level (an operator of higher precedence may add one or two). Excel allows 64 nested functions; this is far more than a formula written by hand needs, and it bounds the recursion of everything that walks a formula (the parser, printer, evaluator and reference rewriting), so a pathological file fails to parse rather than exhausting the stack.
Variables ¶
This section is empty.
Functions ¶
func AxisMaps ¶
AxisMaps are the cell and range mappings for inserting or deleting rows (rows true) or columns, for Relocate.
func EachChild ¶ added in v0.2.0
EachChild calls fn with every expression directly inside n: operands, arguments and array elements.
func LooksLikeRef ¶
LooksLikeRef reports whether an upper-case name reads as a cell in A1 or R1C1 style, even beyond this sheet's edges, so names stay unambiguous in other spreadsheets too.
func ParseLines ¶
ParseLines parses the ends of whole columns ("A", "$C") or whole rows ("2", "$5") as the range they span, with their absolute markers.
func QuoteSheet ¶
QuoteSheet writes a sheet name as a formula needs it: bare when it reads as a plain identifier (Sheet2), otherwise in single quotes with quotes doubled, e.g. 'Q3 plan'.
func RangeString ¶
RangeString writes a range as written in a formula, with its absolute markers: A1:B3, $A$1:B3, or whole columns (A:C) and rows (2:5).
func SplitSheet ¶
SplitSheet splits a reference such as "Sheet2!A1:B3" or "'Q3 plan'!B2" into the sheet name, unquoted, and the rest. Without a sheet, sheet is "" and rest is s.
func Text ¶
Text prints a parsed formula back to text, with a leading "=". Rewritten formulas (after a paste or an inserted row) are stored this way, in Sheets' spelling: ranges as A1:B3, functions without @.
Types ¶
type Abs ¶
type Abs uint8
Abs records which parts of a reference are absolute ($A$1). Copying a formula shifts only the relative parts.
type Addr ¶
type Addr struct {
Col, Row int
}
Addr identifies a cell by zero-based column and row.
type Array ¶ added in v0.2.0
type Array struct{ Rows [][]Node }
Array is an array literal, {1,2;3,4}: rows of elements separated by ";", elements by ",". An element may itself be a range or an array, as {A1:A3,B1:B3} joins two columns.
type Binding ¶ added in v0.2.0
type Binding uint8
Binding is how a function's arguments bind names (Local).
type Func ¶
type Func interface {
Signature() Signature
}
Func is a function a formula can call. The parser checks calls against its signature; what a call computes is up to the engine, which recovers its own type from Call.Fn.
type Invoke ¶ added in v0.2.0
Invoke calls what Fn computes to, a LAMBDA, with Args: LAMBDA(x, x*2)(3), or f(3) where LET bound f to a LAMBDA.
type Local ¶ added in v0.2.0
type Local struct{ Name string }
Local is a name LET or LAMBDA binds, used within that call, as in LET(total, SUM(A:A), total*2). Named ranges never replace it.
type Node ¶
type Node any
Node is a parsed formula expression: one of the types below.
type ParseError ¶
ParseError describes a formula that could not be parsed. Pos is the byte offset in the entry where the problem was found, used to place the edit cursor.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type Range ¶
Range is kept normalized (Rect.From is the top-left corner); Abs holds the absolute markers of Rect.From and Rect.To.
type Rect ¶
type Rect struct {
From, To Addr
}
Rect is an inclusive rectangular range of cells.
func ParseRange ¶
ParseRange parses "A1", "A1:B3", 1-2-3 style "A1..B3", or whole columns "A:C" and rows "2:5".
type Ref ¶
Ref is a cell reference. Sheet is the sheet name as written before the "!" (Sheet2!A1), or "" for the formula's own sheet.
type Rewriter ¶
type Rewriter struct {
Ref func(Ref) Node
Range func(Range) Node
Name func(Name) Node
Local func(Local) Node
}
Rewriter maps the references in a formula. Any function may return RefErr; a nil function leaves those nodes alone.
func Relocate ¶
func Relocate(on func(sheet string) bool, cell func(Addr) (Addr, bool), rng func(Rect) (Rect, bool)) Rewriter
Relocate is the rewrite for cells that move on one sheet: cell maps where each cell went (false if it's gone), and rng maps whole ranges. on reports whether a reference written with a sheet name ("" for none) points at the sheet whose cells moved; other references stay.
type Signature ¶
type Signature struct {
Name string // canonical, upper case
Args string // shown to users, e.g. "value1, [value2, ...]"
Min int
Max int // -1 for variadic
// Step > 0 means arguments after Min come in groups of Step, like
// SUMIFS' (range, criterion) pairs.
Step int
// Binds says which arguments name values for the rest of the call,
// as LET's and LAMBDA's do.
Binds Binding
}
Signature is how a function is called.
type Span ¶
type Span struct{ At, N, Size int }
Span inserts (N > 0) or deletes (N < 0) lines starting at index At, along an axis with Size lines.