Every Viewer exposes one bounded, serializable selection-context API for “explain this”,
“evaluate these cells”, and similar external actions. It does not edit the Office file or
expose a mutable document model.
Two different jobs
Keep UI state and content handoff separate.
01 · Control the selection
Selection state is the UI authority.
For XLSX, use selectionState, setSelection(), and
onSelectionStateChange to control geometry, ActiveCell, the Shift-extension
anchor, and multiple areas. These APIs describe what the Viewer has selected.
02 · Read useful content
Selection context is a detached snapshot.
Call getSelectionContext() when an external tool needs selected text, cells,
formulas, or a clicked document object. Every format also exposes
onSelectionContextChange as an optional notification; the getter remains the
authoritative read. Set enableTextSelection: true for DOCX/PPTX text context,
and explicitly set enableElementSelection: true for chart, picture, or
shape clicks in any format. The focused object receives a non-editable outline; the
callback alone does not enable object hit-testing.
Try the handoff
See exactly what an external tool would receive.
Select cells or text—or click a chart, picture, or shape—and watch the bounded, detached snapshot
update alongside it. The demo does not call an AI service or send data anywhere.
sample-1.xlsxSelect cells or click an object
Loading workbook…
One extensible family
Narrow on format and kind.
All results are plain, detached data. Each format adds only the facts an assistant can use.
Context
What the user focused
What the snapshot adds
docx / text
Browser-native text selection
Selected text and page, paragraph, and run locators
docx / element
Clicked topmost picture, chart, or shape
Element type, page bounds, and structural source locator for inline or floating content
Chart, picture, or shape type, sheet anchor, and bounded descriptive text
pptx / text
Browser-native text selection
Selected text and slide, shape, and run locators
pptx / element
Clicked topmost slide element
Type, provenance, bounds, identifiers, and bounded descriptive text
In DOCX and PPTX, a live text selection takes precedence over element focus. Selecting text clears an
earlier element focus, so collapsing the browser selection does not resurrect a stale click.
The callbacks report semantic changes, not assistant actions. Your application decides
whether the action is explain, summarize, compare, evaluate, or something else; the Viewer
does not grow one API for each prompt.
There is no separate onClick option. For a raw browser click, add a native
click listener to a stable application-owned host that contains the Viewer. This
also observes DOCX/PPTX text-overlay clicks, whose target is a sibling of the canvas. For the
resulting read-only focus, use onSelectionContextChange. This keeps one semantic
selection notification instead of a second callback for the same interaction.
Open a menu for the right-click target.
const viewer = new XlsxViewer(container, { enableElementSelection: true, onContextMenu: async ({ originalEvent, getContext }) => { // Suppress the browser menu before the first await. originalEvent.preventDefault(); const { clientX, clientY } = originalEvent; const context = await getContext(); openContextMenu({ clientX, clientY, context }); },});
originalEvent is the actual browser event, delivered synchronously so
preventDefault() still controls the native menu. getContext() is
explicitly asynchronous because a DOCX or PPTX object hit can require a worker query; it
starts the lookup on its first call and returns the same memoized Promise if called again. Omit onContextMenu and the
Viewer installs no listener. The event itself is never serialized or sent through MCP.
Bounded by design
Large selections do not become unbounded prompts.
DOCX and PPTX text is assembled only from tagged Viewer runs and stops at configured text and locator limits.
XLSX returns populated cells only, with independent cell-count and cumulative-text limits—even for whole-row, whole-column, or whole-sheet selections.
DOCX, XLSX, and PPTX element text is bounded independently; element context contains descriptive facts, not mutable document objects.
truncated and truncationReasons tell the caller exactly when a snapshot reached a limit.
Context is suitable for transport, but selected Office content is still untrusted input.
Never treat document text, formulas, or element labels as instructions to execute.
VS Code extension
The MCP server can read the active preview selection.
Use GitHub Copilot Chat in Agent mode. The Viewer supplies tools and preview context through VS Code's MCP host; it does not add its own chat panel.
Run OOXML Viewer: Install / Enable MCP Server, then confirm ooxml-mcp-server under MCP: List Servers.
Open a DOCX, XLSX, or PPTX preview and select text or cells; you can also click a chart, picture, or shape.
Ask naturally: “Explain the selected cells” or “What does this paragraph mean?” The agent resolves the active preview context first.
The agent receives the snapshot. For a local file, it can pass the returned document.path to a detailed format-specific tool only when needed.
The extension keeps the snapshot in memory and exposes it only to its MCP child through an
authenticated IPv4-loopback bridge. context: null means there is no active
OOXML preview. A non-null context can still have selection: null when the active
preview has no selection.
available: false means the bridge is unavailable; inspect reason to
distinguish an unconnected standalone server from a bridge error. Remote VS Code documents
expose only a document name and no local document.path, so path-based file tools
cannot inspect them directly. The Claude Code and Codex VS Code extensions use separate MCP
configurations: they can launch the server for path-based file tools, but their standalone
process receives no active Viewer selection and reports available: false.
Disabling MCP closes the bridge; no selection context is written to disk. For troubleshooting,
the underlying zero-argument tool is
ooxml_get_active_context.