Getting started with Apiary
In this guide we’ll load an Excel file into GRID’s spreadsheet engine, run a formula on the data it contains, and output the answer. This guide assumes you’re using Node.js and npm on a Unix-like system. We used Node.js v22.11.0 and npm v10.9.0 when writing this tutorial.
-
Let’s start by creating a new directory and using it for a new npm-based project:
Terminal window mkdir apiary-quickstartcd apiary-quickstartnpm init -y -
Next, we’ll configure npm to download packages within the
@grid-isscope from npmjs.com. This will give you access to GRID’s private spreadsheet engine package, Apiary. Create a.npmrcfile:Terminal window cat << 'EOF' > .npmrc@grid-is:registry=https://registry.npmjs.org//registry.npmjs.org:_authToken=${NPM_TOKEN}EOF -
Now we can add the project’s dependencies. We’ll need two direct dependencies:
- The spreadsheet engine itself, known as Apiary
- The open-source xlsx-convert package to convert Excel spreadsheets into the JSON format used by Apiary
Terminal window npm add @grid-is/apiary @borgar/xlsx-convert -
Grab a copy of our example spreadsheet. For this demo, we’ll use a simple spreadsheet that tracks monthly household expenses. It categorises our spending, provides a budget for each category, and tracks actual spending. Save a local copy of
budget.xlsxso we can use it in our project. -
Now to the meat of it. Let’s write some code that can open a spreadsheet and interact with it. Create an
index.jsfile and copy this code in:import xlsxConvert from "@borgar/xlsx-convert";import { Model } from '@grid-is/apiary';await Model.preconditions;const wb = await xlsxConvert("budget.xlsx");const model = Model.fromJSF(wb);const totalSpent = model.runFormula("=SUM(D:D)");console.info({ totalSpent }); -
Once you have the
budget.xlsxfile and the code inindex.js, we can run it using Node. Most of Apiary is written in TypeScript, but its formula parser is written in Rust and loaded as a WebAssembly module. Because of that, we need to run Node with the--experimental-wasm-modulesparameter:Terminal window node --experimental-wasm-modules index.jsAfter running that, you’ll see our monthly expenditure printed out in your terminal:
{ totalSpent: 6945 }