The jolt API
Project code reaches JOLT through the global jolt, or with import { jolt } from "jolt". JADE's editor has full IntelliSense for it.
import { jolt } from "jolt"; // or use the global
jolt.print(`Running in ${jolt.host}, JOLT ${jolt.version}`);
Environment
| Member | Description |
|---|---|
jolt.version | The JOLT runtime version, for example "0.1.0". |
jolt.host | "excel", "word", "powerpoint", or "browser" outside Office. |
jolt.functionsNamespace | "JOLT", or "JOLTDEV" with the development version of JET. Use it when your code writes a formula that calls an Excel function: `=${jolt.functionsNamespace}.RUN("greet", "Ada")`. |
Office
jolt.run(fn)
Runs fn(context) in the current host's batch context (Excel.run, Word.run or PowerPoint.run) and returns its result.
const total = await jolt.run(async (context) => {
const range = context.workbook.getSelectedRange().load("values");
await context.sync();
return range.values.flat().reduce((a, b) => a + Number(b || 0), 0);
});
jolt.call(file, name)
Runs an exported function from a project file, the same way JADE's Run button does. A function with one parameter receives the host context.
Output
| Member | Description |
|---|---|
jolt.print(data, heading?) | Writes a value to the Output panel. Objects and arrays are shown as indented JSON. The panel keeps the last 500 entries. A heading starts a new section. |
jolt.clear() | Clears the Output panel. |
console.log, console.warn and console.error also go to Output, and to VS Code when JUICE is connected.
The app's page
| Member | Description |
|---|---|
jolt.root | The element the app is rendered into: the <body> of the app's own frame, in JET and in JADE's Output View. |
jolt.$(selector) | querySelector within the app. |
jolt.$$(selector) | querySelectorAll within the app, returned as an array. |
jolt.show(content) | Replaces the app's content with an HTML string or a DOM node. |
Project files
Read-only access to the project's own files, which is handy for templates and data.
| Member | Description |
|---|---|
jolt.files.list() | Every file path in the project. |
jolt.files.read(path) | A file's text. |
jolt.files.readJSON(path) | A file, parsed as JSON. |
jolt.show(jolt.files.read("templates/report.html"));
const rates = jolt.files.readJSON("data/rates.json");
Storage
A small key/value store saved inside the document, with the project. Values must be JSON-serialisable. set and remove write to the document, and the data is kept once the document is saved.
| Member | Description |
|---|---|
jolt.storage.get(key) | A stored value, or undefined. |
await jolt.storage.set(key, value) | Stores a value. |
await jolt.storage.remove(key) | Deletes a key. |
jolt.storage.keys() | Every stored key. |
const runs = (jolt.storage.get("runs") ?? 0) + 1;
await jolt.storage.set("runs", runs);
Storage belongs to the document. Embedding a new version of the project from JUICE keeps the storage. Delete all project files… removes it.
Remote code
| Member | Description |
|---|---|
await jolt.import(url) | Imports a remote ES module. |
await jolt.loadScript(url) | Loads a classic (non-module) script into the global scope. JOLT fetches the text and runs it inline, so it works even when the server sends text/plain. |
While you build, both ask you to trust the site the first time. In an approved app they load without asking. JET can't list an address the app works out while running, so its approval screen warns that the app can load code that can't be reviewed in advance. See Security. For packages, a plain import is usually simpler. See Libraries.
Automations
jolt.automations() returns the exported functions that have an @automation tag:
{ file: "main.js", name: "cleanUp", params: 1, async: true, line: 12,
automation: { title: "Clean up", description: "Trim spaces in the selected cells." } }
Dialogs and notifications
The browser's alert, confirm and prompt don't work inside Office add-ins. Use these instead:
| Member | Returns |
|---|---|
jolt.notify(message, kind?) | Shows a short toast. kind is "info", "success" or "error". |
await jolt.alert(message, title?) | Resolves when the user closes the dialog. |
await jolt.confirm(message, { title?, ok?, cancel? }) | true or false. |
await jolt.prompt(message, initial?, { title?, ok? }) | The text entered, or null if cancelled. |
if (await jolt.confirm("Overwrite the summary sheet?", { ok: "Overwrite" })) {
await jolt.call("summary.js", "rebuild");
jolt.notify("Summary rebuilt", "success");
}