Office Scripts
Witan can run Excel Office Script TypeScript through witan xlsx exec. Use this when you want to reuse scripts written for Excel on the web, or when an agent should work in the same scripting language and object model that Excel exposes.
Office Scripts are a scripting surface over the same Witan workbook engine used by the xlsx.* API, Calc, and Render. Workbook edits still run in the same sandboxed exec session and return structured JSON.
Running A Script
Office Scripts are normally authored as TypeScript in Excel. Witan follows that convention: exec accepts .ts scripts, compiles TypeScript before execution, and then runs the script in the headless workbook sandbox.
Start the script with an @office-script pragma and define function main(workbook: ExcelScript.Workbook). Witan passes an ExcelScript-like workbook object to main:
witan xlsx exec report.xlsx --save --script ./format-summary.ts
// @office-script
function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getWorksheet("Summary")
const range = sheet.getRange("B2:B20")
range.setNumberFormat("$#,##0")
workbook.getApplication().calculate(ExcelScript.CalculationType.full)
return range.getTexts()
}
If --input-json is provided, Witan passes the parsed input as the second main argument:
// @office-script
function main(
workbook: ExcelScript.Workbook,
input: { discountRate: number }
) {
const sheet = workbook.getWorksheet("Inputs")
sheet.getRange("B5").setValue(input.discountRate)
workbook.getApplication().calculate(ExcelScript.CalculationType.full)
return workbook.getWorksheet("Summary").getRange("F25").getText()
}
Why Use It
Use Office Script-style code when:
- You already have scripts from Excel on the web that you want to adapt.
- You want to use Excel's workbook/worksheet/range object model instead of Witan's
xlsx.*function calls. - You want the same agent logic to target both an Excel add-in environment and Witan's headless Excel-compatible runtime.
- You want Office Script chart, table, range-formatting, conditional-formatting, data-validation, comment, or pivot-table methods that map naturally to existing Excel examples.
Use CLI Scripting and the xlsx API when you want the Witan-native API surface, including compact reads, dependency tracing, sweepInputs, lint, render previews, and structured feature models.
Rendering From Office Scripts
Office Scripts in Excel do not have a screenshot API for Witan's verifier workflow, but Witan's exec runtime still exposes the render preview helper around the Office Script entry point. Call xlsx.previewStyles(wb, range) when an Office Script wants to render a range after editing it:
// @office-script
async function main(workbook: ExcelScript.Workbook) {
const sheet = workbook.getWorksheet("Summary")
sheet.getRange("B2:B20").setNumberFormat("$#,##0")
workbook.getApplication().calculate(ExcelScript.CalculationType.full)
await xlsx.previewStyles(wb, "Summary!A1:F20")
return sheet.getRange("B2:B20").getTexts()
}
previewStyles is a Witan bridge, not an ExcelScript workbook method. The Office Script object model stays Excel-compatible, while the enclosing Witan runtime can still request a render preview for verification.
Other Witan-only analysis APIs are not part of the Office Script workbook object model. That includes lint diagnostics, dependency tracing, sweepInputs, formula-evaluation helpers, describeSheet, tableLookup, compact TSV reads, and structured xlsx.* feature models. Use Witan-native CLI Scripting when those features are the main goal.
Implemented Surface
The Office Script runtime covers common ExcelScript workflows across:
| Area | Examples |
|---|---|
| Workbook and worksheets | Sheet lookup, add/delete/rename, active sheet, document properties, named items, visibility, tab color, gridlines, headings, freeze panes, and calculation |
| Ranges | Values, formulas, R1C1 formulas, displayed text, number formats, hyperlinks, merged areas, surrounding regions, used ranges, spill ranges, range navigation, copy, clear, delete, insert, sort, and remove duplicates |
| Formatting | Font, fill, borders, alignment, protection flags, row height, wrapping, shrink-to-fit, text orientation, and number formats |
| Tables | Add, inspect, resize, delete, sort, filter, totals rows, columns, rows, table styles, and default table style |
| Charts | Add, inspect, update, delete, position, axes, titles, legends, labels, trendlines, error bars, styles, fills, lines, markers, and data tables for supported chart families |
| Conditional formatting | Cell-value, text, top/bottom, preset criteria, custom formula, color scale, data bar, and icon-set surfaces |
| Data validation | Inspect, set, clear, prompts, error alerts, list rules, numeric/date/time/text-length rules, and custom formulas |
| Comments | Workbook and worksheet comment lookup, creation, replies, resolution, and deletion |
| Page layout and breaks | Print areas, margins, headers/footers, orientation, paper size, print titles, page breaks, and print options |
| Protection | Workbook, worksheet, and range protection flags and options |
| Pivot tables | Inspect, create, refresh, worksheet/workbook lookup, style APIs, and common layout/value/filter/row/column/data hierarchy operations |
Unsupported Office Script members throw explicit NotImplementedError-style errors rather than silently doing nothing. That makes compatibility gaps visible while adapting scripts.
Compatibility Gaps
Witan implements the Office Script surface that maps to headless workbook editing, calculation, rendering, and verification. It does not try to emulate Excel UI state, user selection, external services, or every Office Script object family.
Notable unsupported areas include:
| Area | Examples |
|---|---|
| Excel UI/session state | Active cell/chart/slicer, selected range(s), selection APIs, chart activation, workbook dirty/autosave flags, and chart data-point tracking |
| Localized formula/format APIs | getFormulaLocal, setFormulaLocal, getFormulasLocal, setFormulasLocal, getNumberFormatLocal, and related local-format methods |
| External connections and data services | Linked workbook management, custom XML parts, Power Query query APIs, data connection refresh, linked data types, cards, and controls |
| Bindings, slicers, and timelines | Workbook bindings, slicer creation/lookup, slicer styles, and timeline styles |
| Some worksheet UI features | Worksheet custom properties, named sheet views, worksheet enable-calculation toggles, data type icons, and some shape authoring such as geometric shapes, groups, lines, and text boxes |
| Some formatting details | Auto-indent, range column-width APIs, tint-and-shade APIs on fonts/fills/borders, and some predefined-cell-style mutation |
| Some filter and sort modes | Subfield filters, cell-color/font-color/icon filters, stroke-count sorting, table-sort match-case mode, and multi-field table-sort application |
| Some chart edge cases | Pivot chart options, chart plotBy for unsupported chart layouts, changing combo chart type/data through Office Script, arbitrary title/plot-area positioning in some cases, and some shadow/line-pattern options |
| Some protection operations | Worksheet protection pause/resume/password update flows and allow-edit-range APIs |
| Some pivot-table details | Pivot/table IDs, renaming pivot fields or hierarchies, some subtotals/show-all-items APIs, sortByValues, and some date/value filter variants |
The error message names the missing member, for example NotImplementedError: ExcelScript: Workbook.getBindings is not implemented by xlsx-serve.
Calculation And Results
Office Script application.calculate, worksheet.calculate, and range.calculate use the same calculation engine as witan xlsx calc. Formula-affecting writes update cached values through the workbook engine, and explicit calculation calls are available when an Office Script expects them.
The return value from main is serialized as JSON and becomes the result field of the exec response. Use print(...) for progress output during the script.
Sandbox
Office Script-style code runs in the same exec sandbox as Witan-native scripts:
- No filesystem access.
- No network access.
- No imports.
- The script can only manipulate the open workbook through the supplied workbook object and the preloaded runtime.
See CLI Scripting for the shared exec workflow and xlsx API for the Witan-native scripting API.