Data
Tables
Data
tables.md

Tables

Spreadsheets in and out: csv reads and writes CSV (and TSV, and the other delimiters people use), xlsx reads and writes Excel workbooks. Both work on a page and in a server fn, give plain rows or rows typed into a shape, and say where a cell is when it is not what the shape wants. Back to the map.

Examplea whole program
shape Order
item: string
price: number
when: time?
paid: bool
state orders: Order[] = []
state said = ""
action open
let f = fs.pick(".csv,.xlsx")?
if f == none return
orders = f.name.endsWith(".xlsx") ? xlsx.records(f.data, Order)? : csv.records(f.data, Order)?
said = `${orders.length} orders`
action export
let book = xlsx.write([{name: "Orders", rows: orders, columns: [{key: "item", title: "Item"}, {key: "price", title: "Price", format: "currency"}, {key: "when", title: "When", format: "date"}, {key: "paid", title: "Paid"}], filter: true}])?
fs.save(book, "orders.xlsx")?
column gap=8 padding=16
row gap=8
button "Open a CSV or workbook" press=open
button "Save as .xlsx" press=export disabled=(orders.length == 0)
text (failure(open) ?? said) color=dim
for o in orders
text `${o.item}: ${o.price}${o.paid ? ", paid" : ""}`

Mental model#

csv#

A large file, a piece at a time#

Examplea whole program
shape Order
item: string
price: number
paid: bool
state said = ""
action total
let f = fs.pick(".csv", {read: false})?
if f == none return
let r = csv.open(f, Order)?
let sum = 0
let n = 0
while true
let rows = r.next()?
if rows == none break
n += rows.length
for o in rows
sum += o.price
said = `${n} orders, ${sum} in all (columns: ${r.header.join(", ")})`
button "Add up a CSV" press=total
text (failure(total) ?? said)

xlsx#

Reading#

OpenDocument spreadsheets#

Writing#

Examplea whole program
shape Line
item: string
qty: number
price: number
due: time
state lines: Line[] = [{item: "Oak door", qty: 2, price: 412.5, due: 1794441600000}, {item: "Ash shelf", qty: 5, price: 38, due: 1794528000000}]
action save
let book = xlsx.write([
{name: "Quote", rows: lines, columns: [{key: "item", title: "Item", width: 24}, {key: "qty", title: "Qty", format: "integer"}, {key: "price", title: "Price", format: "currency"}, {key: "due", title: "Due", format: "date"}], filter: true},
{name: "Total", header: false, rows: [["Total", {formula: "SUMPRODUCT(Quote!B2:B3,Quote!C2:C3)", format: "currency", bold: true}], [{value: "Our site", link: "https://example.com"}, {value: "Thank you", fill: "#fff3c4", align: "center", border: true}]], merge: []}
])?
fs.save(book, "quote.xlsx")?
button "Save the quote" press=save
text (failure(save) ?? "")

On the back end#

Examplea whole program
model Sale
item: string
amount: number
read, add anyone
shape SaleRow
item: string
amount: number
server fn exportSales(): bytes
let sales = Sale.find({}, {order: "item"})?
return xlsx.write([{name: "Sales", rows: sales.map(s => ({item: s.item, amount: s.amount})), columns: [{key: "item", title: "Item"}, {key: "amount", title: "Amount", format: "currency"}]}])?
server fn importSales(file: bytes): number
let rows = file[0] == 80 ? xlsx.records(file, SaleRow)? : csv.records(file, SaleRow)?
for r in rows
Sale.add({item: r.item, amount: r.amount})?
return rows.length
state said = ""
action upload
let f = fs.pick(".csv,.xlsx")?
if f == none return
said = `${importSales(f.data)?} rows added`
action download
fs.save(exportSales()?, "sales.xlsx")?
row gap=8
button "Import" press=upload
button "Export" press=download
text (failure(upload) ?? said)

Pitfalls#

In the source#

Edit this page:docs/tables.md
esc
1072 sections of 111 pages, ranked as you type.A heading's words count for more than what stands under it.
↑↓move
↵open
ranked by BM25, in the page