Data
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: boolstate 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: boolstate 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=totaltext (failure(total) ?? said)xlsx#
Reading#
OpenDocument spreadsheets#
Writing#
Examplea whole program
shape Line item: string qty: number price: number due: timestate 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=savetext (failure(save) ?? "")On the back end#
Examplea whole program
model Sale item: string amount: number read, add anyoneshape SaleRow item: string amount: numberserver 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.lengthstate 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=downloadtext (failure(upload) ?? said)Pitfalls#
In the source#
Edit this page:
docs/tables.md