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 Delocalize(src string, loc *locale.Locale) string
- func EachChild(n Node, fn func(Node))
- func EscapeColumn(name string) string
- func ExcelTableRef(src string) (string, bool)
- func Expr(n Node) string
- func Localize(src string, loc *locale.Locale) string
- func LocalizeError(err error, loc *locale.Locale) error
- func LooksLikeCell(k string) bool
- 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 SameSyntax(loc *locale.Locale) bool
- func SheetKey(name string) string
- func SplitSheet(s string) (sheet, rest string)
- func StructuredEnd(src string, i int) int
- func Text(n Node) string
- func WalkNames(n Node, fn func(Name))
- func WalkRefs(n Node, ref func(string, Addr), rng func(string, Rect))
- func WalkTables(n Node, fn func(TableRef))
- 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 TableItems
- type TableRef
- 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 Delocalize ¶ added in v0.3.0
Delocalize reads a formula typed in loc's syntax into the syntax it is stored and parsed in. It works on unfinished formulas too.
func EachChild ¶ added in v0.2.0
EachChild calls fn with every expression directly inside n: operands, arguments and array elements.
func EscapeColumn ¶ added in v0.3.0
EscapeColumn writes a column's name as a structured reference holds it, with ' before each of [ ] # and '.
func ExcelTableRef ¶ added in v0.3.0
ExcelTableRef writes a structured reference, the table's name and its brackets as written in src, in Excel's file syntax, or reports false when it doesn't parse.
func Localize ¶ added in v0.3.0
Localize writes a formula (or any part of one) as stored in loc's syntax. Strings and sheet names are left alone.
func LocalizeError ¶ added in v0.3.0
LocalizeError writes the message of a parse error in err (the error itself, or one it wraps) in loc's syntax, as the formula was typed: "Expected ; or ) in ROUND" where arguments are separated by ;, and the text typed where it names what it didn't expect. The position is the same in both syntaxes.
func LooksLikeCell ¶ added in v0.3.0
LooksLikeCell reports whether an upper-case name reads as a cell in Excel, A1 to XFD1048576 or R1C1, so a sheet of that name is quoted everywhere its formulas may go. Sheet1 doesn't: SHEET is no column.
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 SameSyntax ¶ added in v0.3.0
SameSyntax reports whether loc writes formulas as they are stored.
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 StructuredEnd ¶ added in v0.3.0
StructuredEnd returns the index just past the brackets of a structured reference whose "[" is at src[i], or -1 when they aren't closed.
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 @.
func WalkNames ¶
WalkNames calls fn for every name in n. It walks the tree itself rather than through EachChild, as every formula entered is walked.
func WalkRefs ¶
WalkRefs calls fn for every single-cell reference and range in n, with the sheet it was qualified with ("" for the formula's own sheet).
func WalkTables ¶ added in v0.3.0
WalkTables calls fn for every structured reference in n.
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
Table func(TableRef) 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.
type TableItems ¶ added in v0.3.0
type TableItems uint8
TableItems are the rows of a table a structured reference names.
const ( ItemHeaders TableItems = 1 << iota // the header row: [#Headers] ItemData // the data rows: [#Data], or nothing ItemTotals // a totals row: [#Totals] ItemThisRow // the formula's own row: [#This Row] or @ // ItemAll is the whole table: [#All]. ItemAll = ItemHeaders | ItemData | ItemTotals )
type TableRef ¶ added in v0.3.0
type TableRef struct {
Table string // the table's name as written
// Items are the rows named; zero means the data rows, as when no
// item is written.
Items TableItems
// From and To are the first and last column named, as written: ""
// for every column, To "" for From alone.
From, To string
}
TableRef is a structured reference: Table[Column], Table[#All], Table[@Column], Table[[#Headers],[First]:[Last]].
func (TableRef) Cols ¶ added in v0.3.0
Cols are the first and last column the reference names, the same for one column, or "" for every column.
func (TableRef) Excel ¶ added in v0.3.0
Excel writes the reference as Excel's files hold it, where the formula's own row is [#This Row] rather than @.
func (TableRef) Rows ¶ added in v0.3.0
func (t TableRef) Rows() TableItems
Rows are the rows the reference names, with no item meaning the data rows.