JOLT docs v0.1.0 Apps Install

Excel functions: =JOLT.RUN

Write a function in JavaScript, save it in the workbook, and use it in a formula like any Excel function: =JOLT.RUN("TAX", B2, 0.07). It's the JOLT version of a VBA user-defined function.

This works in Excel on Windows, Mac and the web, with the JET add-in installed (see Install). JET doesn't have to be open: Excel loads it in the background when a formula needs it.

Try the starter

A new Excel project in JADE (or from JUICE's New Project) comes with one function, greet, in functions.js. It returns a greeting picked at random for the name you give it, such as "Howdy, Ada" or "Good morning, Ada", using your computer's clock. Run the app and click Use Custom Function: it writes =JOLT.RUN("greet", "Ada") into the active cell. Excel shows a new greeting each time the workbook recalculates (Ctrl+Alt+F9).

JADE's Run menu doesn't list the functions file's exports, because a formula calls them with cell values, not an Office context.

Write a function

Add a file named functions.js to the workbook's project, in JADE or in VS Code with JUICE. Each function a formula can call is an export:

// functions.js
import { round2 } from "./lib/util.js";

/** Sales tax, rounded to cents. */
export const TAX = (amount, rate) => round2(amount * rate);

/** A table of n and n², which spills into the cells below and beside the formula. */
export const SQUARES = (n) => Array.from({ length: n }, (_, i) => [i + 1, (i + 1) ** 2]);

/** The total of a range: ranges arrive as rows of cells. */
export const TOTAL = (range) => range.flat().reduce((sum, x) => sum + (Number(x) || 0), 0);

Then use them in cells:

FormulaResult
=JOLT.RUN("TAX", 100, 0.07)7
=JOLT.RUN("SQUARES", 4)a 4 × 2 table that spills
=JOLT.RUN("TOTAL", A1:C10)the sum of the range
=JOLT.RUN("lib/greet.js#HELLO", "Ada")the HELLO export of another file
  • Arguments: a single cell or value arrives as that value. A range arrives as an array of rows, for example [[1, 2], [3, 4]].
  • Results: return a number, text, boolean or date, or an array. A list fills a row, and a list of rows fills a block. A function can be async and use fetch.
  • Errors: if a function throws, the cell shows #VALUE!; hover over it to see the message.
  • Another file name: to use a file other than functions.js for plain names, set "functions": "calc.js" in jolt.json.
  • Functions run in JET's page, not in an app, so the jolt API isn't available to them. They can call Excel's API (Excel.run), and import other project files and libraries.

Call a function by its own name

When you save the project, in JADE or with JUICE, each export of the functions file also gets an Excel name of its own, so a cell can write =GREET("Ada") instead of =JOLT.RUN("greet", "Ada"). Excel's autocomplete offers these names, with the function's parameter names.

Each is a workbook name (Formulas → Name Manager) defined with Excel's LAMBDA, so it travels with the workbook:

GREET   =LAMBDA(name, JOLT.RUN("greet", name))
  • The name is the export's name in capitals. JOLT marks the names it made, updates them when you save, and removes them when their export goes. It never changes a name you made yourself.
  • An export gets no name when the name would clash: with one of Excel's own functions (round → ROUND), with a cell address (tax1 → TAX1), or with a name already in the workbook. JOLT tells you which ones when you save. Call those with =JOLT.RUN.
  • Every parameter is required. A rest parameter (...values) is left out, since a LAMBDA has a fixed list.
  • LAMBDA needs Excel for Microsoft 365 or Excel 2024 and later. In older versions the names don't work, but =JOLT.RUN does.

Enabling a workbook's functions

A workbook's functions run whenever Excel recalculates, even with JET closed. So, like macros, they must be enabled on each computer before they run. Enabling the workbook's app in JET (Approving an app) enables its functions too, and the other way round.

  1. Until then, the cells show #N/A ("Open JET to enable this workbook's functions").
  2. Open JET (Home → Jolt → Run Apps). A bar at the top lists the files with the code. Only enable code you trust: it can read and change the workbook.
  3. Click Review & enable…. JET shows the workbook's code on the same approval screen as any app, with Review with your AI. Click Enable & run: Excel recalculates, and the cells show their results.

The approval is stored on your computer, never in the workbook, so a workbook can't enable itself. It's for that exact code: if the code changes, JET asks again. Code you save yourself, in JADE or with JUICE's sync or Embed in Document, is enabled automatically, and the workbook recalculates.

To share functions, share the workbook. Anyone who opens it needs the JET add-in, and enables the functions once.

While you develop in VS Code

With JET connected to JUICE, vscode: calls the files in your VS Code folder directly, without embedding them: =JOLT.RUN("vscode:TAX", 100, 0.07). Each time you save, Excel recalculates those cells. They need the connection, so change them to the plain form ("TAX") and embed the project before you share the workbook.

Code from a site

A function can also come from a JavaScript module on a website, such as one you published with JUICE: =JOLT.RUN("https://tools.example.workers.dev/lib.js#TAX", 100, 0.07). The first time, JET asks you to allow code from that site, as it does for apps run from a URL. The site must allow cross-origin requests (CORS).

When the code changes

Excel only recalculates a formula when its inputs change. After you edit code outside JADE and JUICE, open JET's ☰ menu and choose Recalculate JOLT functions. This also loads code from sites again.

Limits

  • Excel only. Word and PowerPoint have no worksheet functions.
  • Excel's Insert Function dialog lists JOLT.RUN, but not your functions. Their names (see above) are in autocomplete and the Name Manager.
  • In the development version of JET (installed with npm start), the function is =JOLTDEV.RUN, so it can be installed beside the released JET.