headless

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: 18 Imported by: 0

Documentation

Overview

Package headless works on workbook files without the screen, for 012 get, set, recalc and export (see docs/files/scripts.md): it opens a .012 file, finds the cells a reference names, reads and writes them, lists the errors formulas show and saves atomically. Nothing here runs a program or reaches the network unless its caller asks: notebook cells run through RunNotebooks and JEV functions through AnswerJEV, which the command line calls only behind flags.

Index

Constants

This section is empty.

Variables

View Source
var Formats = []string{"text", "csv", "tsv", "json", "nuon"}

Formats are what Get writes: text as the sheet shows it, or a table as 012 --pipe and exports write it.

Functions

func AnswerJEV

func AnswerJEV(ctx context.Context, w *sheet.Workbook, client jev.Client, timeout time.Duration) int

AnswerJEV answers w's JEV functions with client, asking what the workbook's formulas ask, a few questions at a time, until none is waiting, each within timeout. It returns how many were asked.

func Get

func Get(out io.Writer, t Target, o GetOptions) error

Get writes the target's values. One cell is written alone: as it shows, or as a JSON or NUON value typed by its format (a number, a date, a file size). A range or sheet is written as a table: aligned columns as text, or as CSV, TSV, JSON or NUON, whose columns take their names from the first row (see GetOptions.NoHeader).

func MissingOutputs

func MissingOutputs(s *sheet.Sheet) []string

MissingOutputs names the notebook outputs sent to s that show no rows because the file holds no output for their cells: never run, too large to save, or their cells gone.

func ResolveCell

func ResolveCell(w *sheet.Workbook, ref string) (*sheet.Sheet, sheet.Addr, error)

ResolveCell is Resolve for a reference that must be one cell.

func RunNotebooks

func RunNotebooks(ctx context.Context, w *sheet.Workbook, o NotebookOptions) []string

RunNotebooks runs the code cells of each notebook tab, each after the cells it reads, as Run all does on the screen: each cell's output replaces the one the file kept and goes on to the sheet it was sent to. A failure stops its notebook's run, as on the screen. It returns a line for each cell that failed or couldn't run.

func Set

func Set(w *sheet.Workbook, entries []Entry, o SetOptions) (warnings []string, err error)

Set types each entry into its cell, in order, as one change. An entry a cell can't take (a formula that doesn't parse, a validation rule that rejects it, part of an array, a pivot table or a region, a protected range without Force) stops it with an error naming the cell, and the workbook should then be thrown away rather than saved. Entries a rule would only mark invalid are set, and returned as warnings.

func SyncOutputs

func SyncOutputs(w *sheet.Workbook)

SyncOutputs sends the outputs a file kept to the sheets they were sent to, as opening the file on the screen does: reading, not running.

func Trusted

func Trusted(w *sheet.Workbook, machine string) bool

Trusted reports whether the workbook's commands may run here without asking, by the rule macros follow: they were made or trusted on this computer, whose id is machine.

func WriteAtomic

func WriteAtomic(path string, mode fs.FileMode, write func(io.Writer) error) error

WriteAtomic writes a file with write into a temporary file of its own beside path, then renames it over path with mode.

Types

type Entry

type Entry struct {
	Ref, Input string
}

Entry is one cell to set: a reference to one cell, and what to type in it, as the file stores entries (numbers and dates in en-US's form, formulas with commas, whatever the workbook's locale); "" clears it.

type File

type File struct {
	Path string
	Book *sheet.Workbook
	// New is set when there was no file at Path and Book is empty.
	New bool
	// contains filtered or unexported fields
}

File is a workbook opened from a .012 file.

func Open

func Open(path string, create bool) (*File, error)

Open reads the workbook at path, with the notebook outputs it kept sent on to their sheets, as the screen opens it. With create, a path with no file opens as an empty workbook that Save writes there, as opening a new name in 012 does.

func (*File) Save

func (f *File) Save() error

Save writes the workbook back to its file atomically: into a temporary file beside it, renamed over it once complete, so a failed write never leaves half a workbook. A file that would come out the same isn't touched, so its modification time says when it last changed.

type GetOptions

type GetOptions struct {
	Format string // one of Formats; "" is text
	// Input writes what was typed (a formula, or an entry as stored)
	// rather than the value.
	Input bool
	// NoHeader names a JSON or NUON table's columns by their letters and
	// makes every row a record, rather than taking the names from the
	// first row.
	NoHeader bool
}

GetOptions say how Get writes.

type NotebookOptions

type NotebookOptions struct {
	Runner   nushell.Runner // nu, or a fake in tests
	Timeout  time.Duration  // for each cell
	NuConfig bool           // run nu with the user's config files
}

NotebookOptions say how RunNotebooks runs cells.

type Problem

type Problem struct {
	Sheet string
	Addr  sheet.Addr
	Value string // the error as the cell shows it: #DIV/0!
	Why   string // what the screen's context line says about it, or ""
	// JEV is set when the error is a JEV function's with nothing to
	// answer it: --jev asks the model.
	JEV bool
}

Problem is a cell whose formula shows an error after recalculating.

func Errors

func Errors(w *sheet.Workbook) []Problem

Errors returns the cells of w showing errors, without recalculating. A cell an array spilled an error into is its formula's problem, so only formulas are listed.

func Recalc

func Recalc(w *sheet.Workbook) (problems []Problem, circular bool)

Recalc recomputes every formula of w and returns the cells showing errors, sheet by sheet in tab order and row by row, and whether a circular reference was found.

func (Problem) At

func (p Problem) At() string

At is the problem's cell with its sheet: Q3!B7.

type SetOptions

type SetOptions struct {
	// Force sets cells in protected ranges, which are refused otherwise,
	// as the screen asks before editing them.
	Force bool
}

SetOptions tune Set.

type Target

type Target struct {
	Sheet *sheet.Sheet
	Range sheet.Rect
	Whole bool
}

Target is what a reference names: a range of one sheet, or the whole sheet (from A1 to its last cell with contents).

func Resolve

func Resolve(w *sheet.Workbook, ref string) (Target, error)

Resolve finds what ref names in w: a cell or range (B7, A1:C9, A:A), on a sheet (Q3!B7, 'Q3 plan'!A1:C9) or else the sheet shown when the file was saved; a named range (Sales); a table by name, all of it with its header row, or by a structured reference as formulas read it (Sales[Amount]); or a sheet by name (Q3, or Q3!), meaning all of it. "" is the whole sheet shown.

func (Target) Cell

func (t Target) Cell() bool

Cell reports whether the target is one cell.

func (Target) String

func (t Target) String() string

String names the target as a formula would, with its sheet.

Jump to

Keyboard shortcuts

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