macro

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var ErrCancelled = errors.New("stopped")

ErrCancelled is the reason a cancelled run gives.

View Source
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

func Check(name, src string) error

Check parses and resolves src without running it, reporting the first problem with its position.

func Functions

func Functions() []string

Functions lists the names of the functions scripts can call, sorted.

func Source

func Source(header string, actions []Action) string

Source writes a recording as a script, one action per line, under a header comment.

Types

type Action

type Action struct {
	Func    string `json:"func,omitempty"`
	Args    []any  `json:"args,omitempty"` // string, int, bool or JSON
	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.

func Call

func Call(fn string, args ...any) Action

Call is a shorthand for an Action calling fn with args.

func Note

func Note(text string) Action

Note is a comment in a recording.

func (Action) String

func (a Action) String() string

String is the action as a line of Starlark.

func (Action) With

func (a Action) With(name string, v any) Action

With adds a named argument.

type Arg

type Arg struct {
	Name  string `json:"name"`
	Value any    `json:"value"`
}

Arg is a named argument, e.g. fill=True.

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".

func (*Error) Error

func (e *Error) Error() string

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; one that opens a dialog gets the dialog's choices
	// as JSON text, from a dict or list the script gave.
	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
	// SetHeight sets the height of the rows rows, e.g. "3" or "3:5", in
	// lines; 0 fits them to their contents.
	SetHeight(rows string, height 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 JSON added in v0.3.0

type JSON string

JSON is JSON text an Action writes as a Starlark dict or list literal, keys in the order they come.

type Run

type Run struct {
	// contains filtered or unexported fields
}

Run is one run of a script.

func New

func New(name, src string, env Env) *Run

New prepares a run of src, named name in error positions.

func (*Run) Cancel

func (r *Run) Cancel()

Cancel stops the run as soon as the script next takes a step. It is safe to call from any goroutine.

func (*Run) Exec

func (r *Run) Exec() (Stats, error)

Exec runs the script to its end on the calling goroutine. Host calls go through Env.Do.

type Stats

type Stats struct {
	Steps uint64 // Starlark computation steps
	Calls int    // calls to the spreadsheet
}

Stats describe a finished run.

Jump to

Keyboard shortcuts

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