Tips
A grab-bag of short examples that go beyond the quickstart: load workbooks in the browser, start from a blank model, read and write cells, export .xlsx files, and address cells in a specific sheet.
Loading a workbook in the browser
Section titled “Loading a workbook in the browser”You can use Model.fromXLSXFile in Node, but in the browser you should fetch the .xlsx as an ArrayBuffer and pass it to Model.fromXLSX along with a filename:
const res = await fetch("/budget.xlsx");const model = await Model.fromXLSX(await res.arrayBuffer(), "budget.xlsx");Starting with a blank workbook
Section titled “Starting with a blank workbook”If you don’t have an existing file, Model.empty creates a model containing a single empty workbook:
const model = Model.empty("workbook.xlsx");Reading and writing cells
Section titled “Reading and writing cells”As well as evaluating ad-hoc formulas with runFormula, a Model lets you read and write individual cells. Reading comes in three shapes: a bare value, a full cell object, or a 2D array for a range:
// Get the value in a cell (string, number, boolean, null, or error)const revenue = model.readValue("=B2");// Get the full cell objectconst revenueCell = model.readCell("=B2");// Get a 2D array of cell objectsconst table = model.readCells("=A1:C10");Writing values to one or more cells:
// Write a value to a single cellmodel.write("B2", 42);// Write multiple cells in one recalculation passmodel.writeMultiple([ ["B2", 42], ["B3", "Total"], ["B4", true],]);Writing a formula goes through the workbook rather than the model, because changing a formula rebuilds that workbook’s dependency graph:
const wb = model.getWorkbook("budget.xlsx");wb.editCell("B5", { f: "=SUM(E4:E17)" });Saving as .xlsx
Section titled “Saving as .xlsx”In Node, write the workbook straight to disk with toXLSXFile:
await model.getWorkbook("budget.xlsx").toXLSXFile("budget-final-draft-v2-FINAL.xlsx");In the browser, toXLSX returns a buffer you can wrap in a Blob and offer as a download:
const buffer = await model.getWorkbook("budget.xlsx").toXLSX("arraybuffer");const blob = new Blob([buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",});const url = URL.createObjectURL(blob);const a = Object.assign(document.createElement("a"), { href: url, download: "budget.xlsx",});a.click();URL.revokeObjectURL(url);Referencing specific sheets
Section titled “Referencing specific sheets”By default, when you refer to cells without a sheet name (e.g. A1, A1:B4, D:F), the engine looks at the first sheet in the workbook. To reference a cell in a specific sheet, prefix it with the sheet name and an exclamation mark (e.g. Sheet2!A1, Sheet2!A1:B4, Sheet2!D:F). If a sheet name contains a space you must wrap the name in single quotes (e.g. 'My Sheet'!A1, 'My Sheet'!A1:B4, 'My Sheet'!D:F).