← Documentation

Read-only interaction

Turn the user’s focus into useful context.

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.xlsx Select 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.

ContextWhat the user focusedWhat the snapshot adds
docx / textBrowser-native text selectionSelected text and page, paragraph, and run locators
docx / elementClicked topmost picture, chart, or shapeElement type, page bounds, and structural source locator for inline or floating content
xlsx / rangeCells, rows, columns, sheet, or multiple areasCanonical selection state, sheet identity, formulas, display text, and populated cells
xlsx / elementClicked topmost worksheet objectChart, picture, or shape type, sheet anchor, and bounded descriptive text
pptx / textBrowser-native text selectionSelected text and slide, shape, and run locators
pptx / elementClicked topmost slide elementType, 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.

Browser integration

Ask for only the context you can consume.

const document = new DocxViewer(canvas, {
  enableTextSelection: true,
  enableElementSelection: true,
  onSelectionContextChange(context) {
    updateAskAiButton(context);
  },
});

const workbook = new XlsxViewer(container, {
  enableElementSelection: true,
  onSelectionContextChange(context) {
    updateAskAiButton(context);
  },
});

const slides = new PptxViewer(canvas, {
  enableTextSelection: true,
  enableElementSelection: true,
  onSelectionContextChange(context) {
    updateAskAiButton(context);
  },
});

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.

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.

  1. 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.
  2. Run OOXML Viewer: Install / Enable MCP Server, then confirm ooxml-mcp-server under MCP: List Servers.
  3. Open a DOCX, XLSX, or PPTX preview and select text or cells; you can also click a chart, picture, or shape.
  4. Ask naturally: “Explain the selected cells” or “What does this paragraph mean?” The agent resolves the active preview context first.
  5. 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.