Skip to content
@deckyard/sheets

Cheatsheet

Every exported name, written bare. In your code they carry the alias you import the deck under (sheet.cell(...) in these docs). Every mutator returns a fresh value: $s = setCell($s, ...).

Errors and version

ConstantValueMeaning
ERROR_KIND"sheets"The kind of every error this deck raises
MAX_NESTING100Element depth past which a reader refuses the document
VERSION"0.1.0"The deck version, kept in step with deck.toml

Styles

SignatureWhat it does
style()The default style: everything left to the application
isPlain(st as Style)Whether a style sets nothing
withBold(st as Style, on as bool)Bold on or off
withItalic(st as Style, on as bool)Italic on or off
withUnderline(st as Style, on as bool)Underline on or off
withFont(st as Style, name as string, size as float)Font family and size in points; "" or 0.0 leaves that half alone
withColour(st as Style, rgb as string)Text colour, "RRGGBB" (a leading # is accepted)
withBackground(st as Style, rgb as string)Background fill, "RRGGBB"
withAlign(st as Style, how as Align)Horizontal alignment
withVerticalAlign(st as Style, how as VAlign)Vertical alignment
withWrap(st as Style, on as bool)Wrap long text or not
withBorder(st as Style, weight as BorderStyle)One border weight on all four edges
withFormat(st as Style, code as string)Number format code; General is stored as ""
isDateFormat(st as Style)Whether the style's format code shows a date

Style

Two styles that look the same are equal, which is what lets a sheet intern them. "" and 0.0 mean "not set", so the application default applies.

FieldWhat it holds
bold as boolbold text
italic as boolitalic text
underline as boolunderlined text
fontName as stringfont family, "" for the default
fontSize as floatsize in points, 0.0 for the default
colour as stringtext colour "RRGGBB", "" for the default
background as stringfill colour "RRGGBB", "" for none
align as Alignhorizontal alignment
valign as VAlignvertical alignment
wrap as boolwrap long text
border as BorderStyleweight drawn on all four edges
format as stringnumber format code, "" for General

Align

VariantMeaning
GeneralThe format's default: text left, numbers right. Not the same as Left
LeftLeft
CentreCentred
RightRight

VAlign

VariantMeaning
BottomBottom
MiddleMiddle
TopTop

BorderStyle

Dashed, dotted and double rules read back as the nearest of these.

VariantMeaning
NoBorderNo border
ThinThin rule
MediumMedium rule
ThickThick rule

Values

SignatureWhat it does
empty()An empty cell
text(body as string)A text cell
number(amount as float)A numeric cell
integer(amount as int)A numeric cell from an int (stored as a float)
boolean(flag as bool)A boolean cell
date(at as time.Time)A date cell, no time of day
stamp(at as time.Time)A date-and-time cell
formula(source as string, cached as string)A formula cell: A1 expression without =, and its last result
errorValue(code as string)An error cell such as "#N/A"
isEmpty(v as Value)Whether a cell holds nothing
kindOf(v as Value)The variant's name
asString(v as Value)The value as text; a formula gives its cached result
asNumber(v as Value)The value as a number; throws when there is no numeric reading
asBool(v as Value)The value as a boolean; throws when there is none
asTime(v as Value)The value as an instant; a number is read as a 1900 date serial
parseIsoDate(body as string)Read an ODS date-value attribute
toSerial(at as time.Time, date1904 as bool)The date serial for an instant
fromSerial(serial as float, date1904 as bool)The instant a date serial names

Value

The zero value is Empty, which is also what an absent cell reads as.

VariantPayload
Empty-
Textbody as string
Numberamount as float
Boolflag as bool
Stampat as time.Time, dateOnly as bool
Formulasource as string, cached as string
Errcode as string

Workbooks and sheets

SignatureWhat it does
newSheet(name as string)An empty sheet; throws on a name no application accepts
newWorkbook()A workbook with no sheets
workbookWith(name as string)A workbook holding one empty sheet
cell(s as Sheet, at as string)A cell's value by A1 reference; not for loops
cellAt(s as Sheet, at as Ref)A cell's value by reference; not for loops
setCell(s as Sheet, at as string, v as Value)Set a cell; Empty removes it
setCellAt(s as Sheet, at as Ref, v as Value)Set a cell by reference
styleAt(s as Sheet, at as string)A cell's style; the default when it has none
styleAtRef(s as Sheet, at as Ref)A cell's style by reference
setStyle(s as Sheet, at as string, st as Style)Set a cell's style; the default style removes the entry
setStyleAt(s as Sheet, at as Ref, st as Style)Set a cell's style by reference
setAreaStyle(s as Sheet, ref as string, st as Style)One style across an area, in one copy of the sheet
clearCell(s as Sheet, at as string)Remove a cell's value; extent and style stay
cellCount(s as Sheet)How many cells are stored
extent(s as Sheet)The area the cells span
row(s as Sheet, row as int)One row of values, padded to the sheet width
rows(s as Sheet)Every row, top to bottom
valuesIn(s as Sheet, ref as string)Every value in an area, row by row, Empty outside the sheet
setRow(s as Sheet, row as int, values as list of Value)Replace a row from column A
sheetOf(name as string, lines as list of list of Value)Build a sheet from rows in one pass
addRow(s as Sheet, values as list of Value)Append a row below everything
sheetByName(wb as Workbook, name as string)A sheet by name; throws naming the sheets there are
hasSheet(wb as Workbook, name as string)Whether the sheet exists
sheetNames(wb as Workbook)The sheet names in file order
putSheet(wb as Workbook, s as Sheet)Replace the sheet of the same name, or append
addSheet(wb as Workbook, s as Sheet)Append; throws on a duplicate name
removeSheet(wb as Workbook, name as string)Remove; throws on the last sheet
renameSheet(wb as Workbook, from as string, to2 as string)Rename in place

Sheet

Read and write cells through the functions above, not the maps: the keys are the canonical A1 spelling cellRef produces.

FieldWhat it holds
name as stringsheet name
cells as map of string to Valuenon-empty cells, keyed by A1
cellStyles as map of string to intstyle index per cell, keyed by A1
styles as list of Styleinterned styles, index 0 the default
rowCount as intone past the highest row index used
colCount as intone past the highest column index used

Workbook

FieldWhat it holds
sheets as list of Sheetsheets in file order
date1904 as booldate serials count from 1904 (old Mac files) rather than 1900

Addressing

All indices are zero-based: B7 is row 6, column 1.

SignatureWhat it does
ref(row as int, col as int)A reference; throws off the grid
columnName(col as int)0 -> "A", 26 -> "AA"
columnIndex(name as string)"XFD" -> 16383, case-insensitive
cellRef(at as Ref)The A1 spelling
parseRef(text as string)Read an A1 reference; $ markers are dropped
area(a as Ref, b as Ref)An area from two corners in either order
areaRef(box as Area)The A1 spelling; a one-cell area is the single reference
parseArea(text as string)Read an area, with or without a colon
areaSize(box as Area)How many cells it covers
areaContains(box as Area, at as Ref)Whether it covers a cell
splitSheetRef(text as string)[sheet, rest], sheet "" when unqualified
sheetRef(sheetName as string, cell as string)Sheet!A1, quoting the name when needed
ConstantValueMeaning
MAX_COLUMN16383Last column index (XFD)
MAX_ROW1048575Last row index (row 1048576)

Ref

FieldWhat it holds
row as introw index, 0 for row 1
col as intcolumn index, 0 for column A

Area

Inclusive on both corners.

FieldWhat it holds
from as Reftop-left corner
to2 as Refbottom-right corner

Formats and files

SignatureWhat it does
formatOf(name as string)The format the extension names; throws on an unknown one
formatOfBytes(raw as bytes)The format the bytes identify (not CSV/TSV)
readFormat(raw as bytes, kind as Format)Read bytes as a format
writeFormat(wb as Workbook, kind as Format)Write a format to bytes; CSV/TSV write the first sheet
open(filePath as string)Read a file, format by extension
openDetected(filePath as string)Read a file, format by content
save(wb as Workbook, filePath as string)Write a file, format by extension
formatName(kind as Format)"ods", "xlsx", "gnumeric", "csv" or "tsv"

Format

VariantExtensions
Ods.ods
Xlsx.xlsx, .xlsm
Gnumeric.gnumeric, .gnm
Csv.csv
Tsv.tsv, .tab

Streaming rows

SignatureWhat it does
openRows(filePath as string, sheetName as string)A cursor over a sheet of a file; "" for the first sheet
openRowsFromBytes(raw as bytes, kind as Format, sheetName as string)The same over bytes in memory
hasRow(r as RowReader)Whether another row is waiting
nextRow(r as RowReader)Advance to the next row
rowValues(r as RowReader)The current row as values
rowNumber(r as RowReader)The current row's one-based number
rowStrings(r as RowReader)The current row as text

RowReader

Treat it as opaque and use the functions above.

FieldWhat it holds
src as bytesthe part holding the sheet
at as intscan position
limit as intend of the sheet's rows
rowIndex as intzero-based index of the current row
values as list of Valuethe current row
more as boolwhether another row is waiting
kind as Formatwhich markup is being walked
shared as list of stringXLSX shared string table
dateStyles as map of string to boolXLSX styles that mean a date
date1904 as boolthe workbook's date system

OpenDocument

SignatureWhat it does
readOds(raw as bytes)Read an ODS container
readOdsContent(content as bytes)Read an ODS content.xml on its own
odsFormulaSource(raw as string)An ODS formula attribute in the model's A1 dialect
writeOds(wb as Workbook)Write an ODS container
writeOdsContent(wb as Workbook)Write only content.xml
ConstantValueMeaning
ODS_MIMETYPEapplication/vnd.oasis.opendocument.spreadsheetThe media type in the mimetype member

Office Open XML

SignatureWhat it does
readXlsx(raw as bytes)Read an XLSX container
writeXlsx(wb as Workbook)Write an XLSX container

Gnumeric

SignatureWhat it does
readGnumeric(raw as bytes)Read a Gnumeric file, gzipped or not
writeGnumeric(wb as Workbook)Write a gzipped Gnumeric file
writeGnumericXml(wb as Workbook)Write the uncompressed document

Delimited text

SignatureWhat it does
readCsv(body as string, typed as bool)Read CSV into a one-sheet workbook
readDelimited(body as string, delimiter as string, typed as bool)The same with any one-character separator
writeCsv(wb as Workbook, sheetName as string)One sheet as CSV; "" for the first
writeDelimited(wb as Workbook, sheetName as string, delimiter as string)The same with any one-character separator
writeCsvSafe(wb as Workbook, sheetName as string)CSV with formula injection neutralised
ConstantValueMeaning
CSV_SHEET_NAME"Sheet1"The name of the sheet a CSV read produces

Formula dialects

SignatureWhat it does
toA1Formula(source as string)OpenFormula ([.B3]*[.C2]) to A1 (B3*C2)
toOpenFormula(source as string)A1 to OpenFormula

Number formats

asString gives the value; these give what an application shows. See Number formats.

SignatureWhat it does
applyFormat(v as Value, code as string)A value through a format code
displayAt(s as Sheet, at as string)A cell through its own style's format
displayAtRef(s as Sheet, at as Ref)The same by reference
displayRow(s as Sheet, row as int)One row as shown
displayRows(s as Sheet)Every row as shown
builtinFormat(id as int)The code an XLSX built-in numFmtId stands for
defaultLocale()Dot, comma and English names
withSeparators(it as Locale, decimal as string, grouping as string)Other separators
withMonthNames(it as Locale, full as list of string, short as list of string)Other month names
withDayNames(it as Locale, full as list of string, short as list of string)Other weekday names
applyFormatIn(v as Value, code as string, locale as Locale)applyFormat in a locale
displayAtIn(s as Sheet, at as string, locale as Locale)displayAt in a locale
displayRowsIn(s as Sheet, locale as Locale)displayRows in a locale
ConstantValueMeaning
MAX_FORMATTED1e15Above this magnitude a number renders plainly
MAX_FORMAT_CODE255A longer code is not read; the value renders plainly

Locale

FieldWhat it holds
decimal as stringdecimal separator
grouping as stringthousands separator
months as list of stringtwelve month names, January first; [] for English
monthAbbrevs as list of stringtheir short forms; [] cuts the full names
days as list of stringseven weekday names, Sunday first; [] for English
dayAbbrevs as list of stringtheir short forms; [] cuts the full names

Formula evaluation

See Evaluating formulas.

SignatureWhat it does
evaluate(wb as Workbook, sheetName as string, source as string)Compute one A1 expression; a leading = is ignored
recalculate(wb as Workbook, sheetName as string)Recompute every formula cell on a sheet
isErrorValue(v as Value)Whether a value is an error
tokenize(source as string)Split a formula into tokens
parseFormula(source as string)Parse a formula into a tree
ConstantValueMeaning
ERR_DIV0#DIV/0!Division or modulo by zero
ERR_VALUE#VALUE!An argument of the wrong type
ERR_REF#REF!A reference to nothing
ERR_NAME#NAME?Unknown function or defined name
ERR_NUM#NUM!A number outside the operation's domain, or a range over MAX_RANGE_CELLS
ERR_NA#N/AA lookup found nothing
ERR_CIRCULAR#REF!A formula depends on itself
MAX_FORMULA_DEPTH64Bracket nesting and prefix-operator runs
MAX_FORMULA_TOKENS512Tokens in one formula; parsing is quadratic in it
MAX_RESOLVED_CELLS500000Cells one evaluation may resolve
MAX_RANGE_CELLS1000000Cells one range may cover
MAX_TEXT_RESULT32767Characters in one text result, which bounds REPT
MAX_TEXT_GROWTH2097152Characters one evaluation may build by concatenation

Recalculated

FieldWhat it holds
sheet as Sheetthe sheet with results written into the caches
evaluated as intformula cells computed
failed as intof those, how many yielded an error

Token

A range such as A1:B2 is one Reference; a bare TRUE is a Logical.

VariantPayload
Numberamount as float
Textbody as string
Logicalflag as bool
Referencebody as string
Namebody as string
Operatorsymbol as string
OpenParen-
CloseParen-
Separator-
ErrorTokencode as string

Expr

VariantPayload
Numamount as float
Strbody as string
Logicflag as bool
ErrLitbody as string
Cellbody as string
Spanbody as string
NameRefname as string
Unarysymbol as string, parts as list of Expr
Binarysymbol as string, parts as list of Expr
Callname as string, parts as list of Expr

Exports

One-way; see Exporting.

SignatureWhat it does
toHtml(s as Sheet)One <table>, styles inline
workbookToHtml(wb as Workbook)Every sheet as a labelled table
toHtmlDocument(s as Sheet, title as string)A standalone page
toRecords(s as Sheet)Rows as maps keyed by the header row
toJson(s as Sheet)The records as indented JSON
toJsonLines(s as Sheet)The records, one JSON object per line
toPdf(s as Sheet, size as PageSize)A paginated PDF table
a4()A4 portrait: 595 x 842 pt, margin 40, 9 pt text
a4Landscape()A4 landscape: 842 x 595 pt, margin 40, 9 pt text
letter()US Letter portrait: 612 x 792 pt, margin 40, 9 pt text
ConstantValueMeaning
MAX_EXPORT_ROWS100000Rows past which every export refuses the sheet

PageSize

All in PDF points.

FieldWhat it holds
width as intpage width
height as intpage height
margin as intmargin on every side
fontSize as inttext size