Documentation
¶
Overview ¶
Package macro runs macros: Starlark scripts that act on a spreadsheet through a small API, as Google Sheets macros are Apps Script. A recorded macro is a list of Actions written out as a script (Source), so recorded and hand-written macros run the same way.
Scripts can't reach the file system, the network, the clock or random numbers: the only way out is the Host. Every run has a step limit and can be cancelled from another goroutine. The package knows nothing of the terminal or the engine; internal/ui implements Host.
Index ¶
Constants ¶
const DefaultMaxSteps = 10_000_000
DefaultMaxSteps bounds a run: enough for loops over tens of thousands of cells, few enough that a script that never ends stops within a second or so.
Variables ¶
var ErrCancelled = errors.New("stopped")
ErrCancelled is the reason a cancelled run gives.
var JumpTargets = []string{"up", "down", "left", "right", "home", "start", "end"}
JumpTargets are the places Jump goes: the edge of the data in a direction (Ctrl+arrows), the row's first column (Home), A1 (Ctrl+Home) and the last used cell (Ctrl+End).
Functions ¶
func Check ¶
Check parses and resolves src without running it, reporting the first problem with its position.
Types ¶
type Action ¶
type Action struct {
Func string `json:"func,omitempty"`
Args []any `json:"args,omitempty"` // string, int or bool
Named []Arg `json:"named,omitempty"`
Comment string `json:"comment,omitempty"`
}
Action is one step of a recording: a call of one of the script's functions, or a comment when Func is "". It is plain data, so a log can be kept, compared or saved before it becomes a script.
type Env ¶
type Env struct {
Host Host
// Do runs fn where Host may be used and returns once it has; nil
// runs it directly. The UI hands calls to its own goroutine here, so
// the script can run on another.
Do func(fn func())
// Print shows a line printed by the script; called inside Do.
Print func(msg string)
// MaxSteps bounds the run; 0 means DefaultMaxSteps.
MaxSteps uint64
}
Env is where a run happens.
type Error ¶
type Error struct {
Pos string // name:line:column, or "" when not at a place in the script
Msg string
Cancel bool // the run was cancelled
Limit bool // the run hit its step limit
}
Error is a script's failure at a position in it, e.g. "Totals:3:5: set: not a cell or range: ZZ".
type Host ¶
type Host interface {
// Values returns the computed values of ref, row by row: nil for a
// blank, float64, string, bool, or an error value's code as a string
// ("#DIV/0!").
Values(ref string) ([][]any, error)
// Formulas returns what was typed in each cell of ref, row by row,
// when it's a formula, and "" otherwise.
Formulas(ref string) ([][]string, error)
// SetInput enters input, as if typed, in every cell of ref.
SetInput(ref, input string) error
// SetInputs enters a block of inputs, as if typed, from ref's first
// cell.
SetInputs(ref string, rows [][]string) error
// SetFormula enters formula in ref's first cell and fills it over the
// rest, adjusting relative references as a copy does.
SetFormula(ref, formula string) error
// Clear erases the contents of ref, or of the selection when ref is "".
Clear(ref string) error
// NumberFormat is the number format of ref's first cell: its kind
// ("auto", "number", "currency"...) and decimals.
NumberFormat(ref string) (kind string, decimals int, err error)
// SetNumberFormat formats ref; decimals < 0 keeps the kind's default,
// and pattern is for the "custom" kind.
SetNumberFormat(ref, kind string, decimals int, pattern string) error
// Selection is the selected range and the active cell in it.
Selection() (sel, active string)
// Select selects ref, switching sheets if it names one, with active
// as the active cell ("" for ref's first cell).
Select(ref, active string) error
// Move moves the active cell by cols and rows and drops the
// selection.
Move(cols, rows int) error
// Extend selects from the active cell to cols and rows away from it;
// whole is "", "columns" or "rows".
Extend(cols, rows int, whole string) error
// Jump moves as Ctrl+arrows, Home, Ctrl+Home or Ctrl+End do (see
// JumpTargets), extending the selection when extend is set.
Jump(to string, extend bool) error
// Enter stores text in the active cell as if typed and accepted, or
// in every selected cell, references adjusted, when fill is set. With
// an origin, a formula is taken as typed there and moved to the
// active cell, relative references following, as in a copy.
Enter(text string, fill bool, origin string) error
// PasteText pastes tab-separated text at the active cell, as pasting
// from another program does.
PasteText(text string) error
// Sheets lists the sheet names in tab order.
Sheets() []string
// ActiveSheet names the sheet shown.
ActiveSheet() string
// ActivateSheet shows the named sheet.
ActivateSheet(name string) error
// AddSheet adds a sheet after the one shown and shows it, returning
// its name; name "" picks the next free one.
AddSheet(name string) (string, error)
// MoveSheet moves the sheet shown to position pos, counting from 1.
MoveSheet(pos int) error
// Run runs a registered command by id. A command that asks a
// question (a width, a name, a confirmation) gets answer, which is
// required then.
Run(id string, answer *string) error
// SetWidth sets the width of the columns cols, e.g. "B" or "B:D".
SetWidth(cols string, width int) error
// Fill fills from the selection as dragging the fill handle does: to
// the range to, or by rows (down, or up when negative) or cols.
Fill(to string, rows, cols int) error
}
Host is the spreadsheet a script acts on. References are A1 strings, optionally with a sheet ("Sheet2!B3:C9"); a range is a cell or a rectangle. Its methods are only called inside Env.Do.
type Run ¶
type Run struct {
// contains filtered or unexported fields
}
Run is one run of a script.