CSV and spreadsheets in Jennifer

Jennifer

From a messy till export to an XLSX with formulas and number formats: the csv module and the @deckyard/sheets deck.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 18

The plan

StepTool
Read a messy CSV, strictlycsv.parseDialect, csv.reader
Clean and total itcsv.toRecords, convert
Hand a CSV back safelycsv.formatSafe
Build an XLSX with a formula and formatssheet.sheetOf, sheet.save
Read it back, stream a big onesheet.open, sheet.openRows
Export for the web and for papersheet.toHtmlDocument, toPdf
2 / 18

csv.parse: RFC 4180

use io;
import "csv.j" as csv;

def rows as list of list of string init
    csv.parse("name,note\n\"Doe, J\",\"said \"\"hi\"\"\"");
io.printf("%d rows; [%s] [%s]\n", len($rows), $rows[1][0], $rows[1][1]);
2 rows; [Doe, J] [said "hi"]

Every field is a string. A quoted field may hold the delimiter, a doubled quote and line breaks.

3 / 18

Strict: a broken file is an error

for (def bad in ["a,b\n1", "a,b\n5\" disk,2", "\"ab\"c"]) {
    try {
        csv.parse($bad);
    } catch (e) {
        io.printf("%s: %s\n", $e.kind, $e.message);
    }
}
csv: csv: record 2 has 1 fields, expected 2
csv: csv: a quote inside an unquoted field at line 2, column 2
csv: csv: text after a closing quote at line 1, column 5
4 / 18

A real export

# export from the till, week 44
branch;product;qty;price
North;"Rye bread; sliced";12;3.20
North;Croissant;30;1.10
 South ; "Tart ""lemon""" ;8;4.50
South;Croissant;25;1.10
5 / 18

A dialect for the file

def d as csv.Dialect init csv.dialect(";");
$d.comment = "#";
$d.trim = true;
def rows as list of list of string init
    csv.parseDialect(fs.readString("sales.csv"), $d);
for (def r in $rows) { io.printf("%s\n", csv.format([$r])); }
branch,product,qty,price
North,Rye bread; sliced,12,3.20
North,Croissant,30,1.10
South,"Tart ""lemon""",8,4.50
South,Croissant,25,1.10
6 / 18

Streaming, and reading past bad records

def r as csv.Reader init
    csv.readerWith(fs.open("bad.csv", "read"), ";");
while (not csv.readerEof($r)) {
    try { io.printf("ok:   %s\n", csv.readRow($r)[0]); }
    catch (e) { io.printf("skip: %s (%s)\n", $e.message, $e.kind); }
}
csv.closeReader($r);
ok:   branch
ok:   North
skip: csv: a quote inside an unquoted field at line 3, column 8 (csv)
skip: csv: record 4 has 3 fields, expected 4 (csv)
ok:   East
7 / 18

Records, cleaning, totals

def recs as list of map of string to string init
    csv.toRecords(csv.parseDialect(fs.readString("sales.csv"), $d));
def revenue as map of string to float init {};
for (def rec in $recs) {
    def b as string init $rec["branch"];
    if (not maps.has($revenue, $b)) { $revenue[$b] = 0.0; }
    $revenue[$b] = $revenue[$b] +
        convert.toFloat($rec["qty"]) * convert.toFloat($rec["price"]);
}
North 71.40
South 63.50
8 / 18

Writing CSV for a spreadsheet: formatSafe

io.printf("%s\n", csv.formatSafe([["note"],
    ["=HYPERLINK(\"x\")"], ["-5 kg"]]));
note
"'=HYPERLINK(""x"")"
'-5 kg
9 / 18

@deckyard/sheets: one cell model

$ jvc add @deckyard/sheets && jvc install
import "@deckyard/sheets/" as sheet;
sheet.Value variantBuilt with
Text, Number, Booltext(s), number(f), integer(i), boolean(b)
Stampdate(t), stamp(t)
Formulaformula("SUM(A1:A9)", cached)
Err, EmptyerrorValue("#N/A"), empty()
10 / 18

Building the sheet

for (def rec in $recs) {          # $lines holds the header row
    def at as string init convert.toString(len($lines) + 1);
    $lines[] = [sheet.text($rec["branch"]), sheet.text($rec["product"]),
        sheet.integer(convert.toInt($rec["qty"])),
        sheet.number(convert.toFloat($rec["price"])),
        sheet.formula("C" + $at + "*D" + $at, "")];
}
def last as string init convert.toString(len($lines));
$lines[] = [sheet.text("Total"), sheet.empty(), sheet.empty(),
    sheet.empty(), sheet.formula("SUM(E2:E" + $last + ")", "")];
def s as sheet.Sheet init sheet.sheetOf("Week 44", $lines);
11 / 18

Number formats and styles

def money as sheet.Style init
    sheet.withFormat(sheet.style(), "#,##0.00");
def bold as sheet.Style init sheet.withBold(sheet.style(), true);
$s = sheet.setAreaStyle($s, "D2:E6", $money);
$s = sheet.setAreaStyle($s, "A1:E1", $bold);
Code1234.567 shows as
#,##0.001,234.57
0.0%123456.7%
0.00;(0.00)negative numbers in brackets
d mmm yyyya date, from a date serial
12 / 18

Recalculate and save as XLSX

def wb as sheet.Workbook init sheet.addSheet(sheet.newWorkbook(), $s);
$wb = sheet.putSheet($wb, sheet.recalculate($wb, "Week 44").sheet);
sheet.save($wb, "week44.xlsx");
wrote week44.xlsx, 2727 bytes
13 / 18

Reading it back

def s as sheet.Sheet init
    sheet.sheetByName(sheet.open("week44.xlsx"), "Week 44");
def total as sheet.Value init sheet.cell($s, "E6");
match ($total) {
    when Formula(f) { io.printf("%s = %s\n", $f.source, $f.cached); }
    else { }
}
io.printf("value %s, shown %s\n", sheet.asString($total),
    sheet.displayAt($s, "E6"));
SUM(E2:E5) = 134.9
value 134.9, shown 134.90
14 / 18

Errors: values and throws

sheet.asString(sheet.evaluate($wb, "Week 44", "E6/0"));    # #DIV/0!
sheet.evaluate($wb, "Week 44", "SUM(E2:E5");                # throws
sheet.evaluate($wb, "Week 44", $seventyNestedBrackets);     # throws
sheet.sheetByName($wb, "Week 9");                           # throws
sheet.open("fake.xlsx");                                    # throws
sheets: the argument list of "SUM" is never closed
limit: the formula nests deeper than 64 levels
sheets: the workbook has no sheet named "Week 9" (it has "Week 44")
sheets: not a readable ZIP container: archive.unpack: zip: not a
  valid zip file
15 / 18

Streaming a large sheet

def r as sheet.RowReader init sheet.openRows("year.xlsx", "Year");
while (sheet.hasRow($r)) {
    $r = sheet.nextRow($r);
    if (sheet.rowNumber($r) == 1) { continue; }      # the header
    $total = $total + sheet.asNumber(sheet.rowValues($r)[1]);
}
20001 rows, 390000 items
16 / 18

Export: HTML, JSON, PDF

fs.writeString("week44.html", sheet.toHtmlDocument($s, "Week 44"));
fs.writeString("week44.jsonl", sheet.toJsonLines($s));
fs.writeBytes("week44.pdf", sheet.toPdf($s, sheet.a4()));
{"Branch":"North","Product":"Rye bread; sliced","Qty":"12",
  "Price":"3.2","Revenue":"38.400000000000006"}
{"Branch":"Total","Product":"","Qty":"","Price":"","Revenue":"134.9"}
17 / 18

Read more

Based on Jennifer 0.27.0, November 2026.

18 / 18