@betteroffice/xlsx-react 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -366
- package/dist/chunk-2S2QUCJE.js +1 -0
- package/dist/chunk-OADYDRKQ.js +3 -0
- package/dist/index.d.ts +218 -27
- package/dist/index.js +1 -1
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -1,387 +1,138 @@
|
|
|
1
1
|
# @betteroffice/xlsx-react
|
|
2
2
|
|
|
3
|
-
React
|
|
4
|
-
|
|
5
|
-
drop-in `<XlsxEditor>` component with the toolbar, selection, keyboard, and
|
|
6
|
-
clipboard wired up. Formula calculation and rendering run in the core's
|
|
7
|
-
Rust/WebAssembly engine; the grid is painted onto canvas.
|
|
3
|
+
React XLSX editor with a canvas grid, toolbar, selection, keyboard input, and clipboard support.
|
|
4
|
+
The engine runs in a Web Worker by default, with promise-returning host operations and exports.
|
|
8
5
|
|
|
9
|
-
|
|
6
|
+
## Install
|
|
10
7
|
|
|
11
8
|
```bash
|
|
12
|
-
bun add @betteroffice/xlsx-react
|
|
9
|
+
bun add @betteroffice/xlsx-react react react-dom
|
|
13
10
|
```
|
|
14
11
|
|
|
15
|
-
`react` and `react-dom`
|
|
12
|
+
`react` and `react-dom` 18 or 19 are peer dependencies; the XLSX core is included as a dependency.
|
|
16
13
|
|
|
17
|
-
##
|
|
14
|
+
## Usage
|
|
18
15
|
|
|
19
16
|
```tsx
|
|
20
|
-
import { XlsxEditor } from
|
|
21
|
-
|
|
22
|
-
<XlsxEditor file={bytes} fileName="report.xlsx" />;
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
`file` is a `Uint8Array` of `.xlsx` bytes; omit it to render an empty frame.
|
|
26
|
-
|
|
27
|
-
## Open and save
|
|
28
|
-
|
|
29
|
-
```tsx
|
|
30
|
-
import { useState } from "react";
|
|
31
|
-
import { XlsxEditor } from "@betteroffice/xlsx-react";
|
|
32
|
-
|
|
33
|
-
export function App() {
|
|
34
|
-
const [file, setFile] = useState<Uint8Array>();
|
|
17
|
+
import { XlsxEditor } from '@betteroffice/xlsx-react';
|
|
35
18
|
|
|
19
|
+
export function Spreadsheet({ bytes }: { bytes: Uint8Array }) {
|
|
36
20
|
return (
|
|
37
|
-
|
|
38
|
-
<
|
|
39
|
-
|
|
40
|
-
accept=".xlsx"
|
|
41
|
-
onChange={async (e) => {
|
|
42
|
-
const f = e.target.files?.[0];
|
|
43
|
-
if (f) setFile(new Uint8Array(await f.arrayBuffer()));
|
|
44
|
-
}}
|
|
45
|
-
/>
|
|
46
|
-
<XlsxEditor
|
|
47
|
-
file={file}
|
|
48
|
-
fileName="workbook.xlsx"
|
|
49
|
-
onSave={(bytes) => console.log(`saved ${bytes.length} bytes`)}
|
|
50
|
-
/>
|
|
51
|
-
</>
|
|
21
|
+
<div style={{ height: 600 }}>
|
|
22
|
+
<XlsxEditor file={bytes} fileName="report.xlsx" />
|
|
23
|
+
</div>
|
|
52
24
|
);
|
|
53
25
|
}
|
|
54
26
|
```
|
|
55
27
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
landed when they return. An open entry the workbook refuses stays open and
|
|
153
|
-
they change nothing (`selectCells` returns `false`); one it refused earlier
|
|
154
|
-
reopens at its own cell once they land on its sheet. Commands run right
|
|
155
|
-
after them, even in the same handler, act on the new selection and sheet.
|
|
156
|
-
- The store enforces read-only mode, the selection a command needs and its
|
|
157
|
-
arguments for the editor's UI. `api.handle` stays unrestricted host
|
|
158
|
-
authority, and workbook protection is separate from the store.
|
|
159
|
-
- Narrow toolbars move trailing groups into a keyboard-accessible More menu. Host
|
|
160
|
-
`ToolbarButton`s get an entry there; wrap other content in `ToolbarOverflow`.
|
|
161
|
-
A group holding content without an entry stays in the row, which scrolls
|
|
162
|
-
horizontally when that content does not fit.
|
|
163
|
-
|
|
164
|
-
The prop-based `EditorToolbar`, `Toolbar` and `useEditorToolbar` API keeps
|
|
165
|
-
working unchanged in the default legacy mode and is deprecated for new
|
|
166
|
-
toolbars; in command mode `useEditorToolbar` returns a projection of the
|
|
167
|
-
command state whose callbacks run commands, and passing those props throws.
|
|
168
|
-
|
|
169
|
-
## AI agents
|
|
170
|
-
|
|
171
|
-
`onReady`'s `handle` is the `WorkbookHandle`. An agent stages edits with
|
|
172
|
-
`propose()` instead of applying them; the editor paints per-cell ghosts and a
|
|
173
|
-
review panel where the human accepts or rejects. The full proposal API lives in
|
|
174
|
-
[`@betteroffice/xlsx`](https://www.npmjs.com/package/@betteroffice/xlsx).
|
|
175
|
-
|
|
176
|
-
```tsx
|
|
177
|
-
import { isProposalsAvailable } from "@betteroffice/xlsx";
|
|
178
|
-
import type { XlsxEditorApi } from "@betteroffice/xlsx-react";
|
|
179
|
-
|
|
180
|
-
<XlsxEditor
|
|
181
|
-
file={file}
|
|
182
|
-
onReady={({ handle, refreshProposals }: XlsxEditorApi) => {
|
|
183
|
-
if (!isProposalsAvailable()) return;
|
|
184
|
-
handle.propose("copilot", "add totals", [
|
|
185
|
-
{ sheet: 0, row: 9, col: 2, input: "=SUM(C1:C9)" },
|
|
186
|
-
]);
|
|
187
|
-
refreshProposals();
|
|
188
|
-
}}
|
|
189
|
-
/>;
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
## Edit batches
|
|
193
|
-
|
|
194
|
-
The `onReady` API's `version`, `readCells`, `findText`, `validateEdits` and
|
|
195
|
-
`applyEdits` run in the same queue as commands: cell and formula entries, chart
|
|
196
|
-
nudges, pastes and cuts accepted before them land first, and an IME composition
|
|
197
|
-
ends with its text written. When that cannot happen they reject with
|
|
198
|
-
`XlsxCommandAdmissionError`, whose `code` is `input-failed` while a refused entry
|
|
199
|
-
waits for correction, `gesture-active` during a chart drag, and
|
|
200
|
-
`document-replaced` when the workbook is replaced meanwhile. Refusals of the
|
|
201
|
-
batch itself stay data. `applyEdits` keeps the
|
|
202
|
-
caller's `expectVersion`, so input that lands first refuses the batch with
|
|
203
|
-
`stale-version`; read the version through the API to include it. While
|
|
204
|
-
`readOnly`, writes refuse with `read-only`. An applied batch repaints once and
|
|
205
|
-
calls `onChange` once.
|
|
206
|
-
|
|
207
|
-
```tsx
|
|
208
|
-
import { XlsxCommandAdmissionError, XlsxEditor, type XlsxEditorApi } from "@betteroffice/xlsx-react";
|
|
209
|
-
|
|
210
|
-
<XlsxEditor
|
|
211
|
-
file={file}
|
|
212
|
-
onReady={(api: XlsxEditorApi) => {
|
|
213
|
-
void (async () => {
|
|
214
|
-
const version = await api.version();
|
|
215
|
-
const result = await api.applyEdits({
|
|
216
|
-
expectVersion: version,
|
|
217
|
-
steps: [
|
|
218
|
-
{
|
|
219
|
-
op: "setCellInputs",
|
|
220
|
-
target: { sheetId: "sheet:0", range: { kind: "a1", a1: "B3" } },
|
|
221
|
-
inputs: [["120"]],
|
|
222
|
-
},
|
|
223
|
-
],
|
|
224
|
-
});
|
|
225
|
-
if (!result.ok) console.warn(result.failure.code);
|
|
226
|
-
})().catch((error: unknown) => {
|
|
227
|
-
if (error instanceof XlsxCommandAdmissionError) console.warn(error.code);
|
|
228
|
-
else throw error;
|
|
229
|
-
});
|
|
230
|
-
}}
|
|
231
|
-
/>;
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
See [`@betteroffice/xlsx`](https://www.npmjs.com/package/@betteroffice/xlsx) for
|
|
235
|
-
the step vocabulary and its limits.
|
|
236
|
-
|
|
237
|
-
## Host plugins
|
|
238
|
-
|
|
239
|
-
Host-owned tools (review aids, checks, templates) install through the `plugins`
|
|
240
|
-
prop. A plugin contributes a docked panel, an overlay on the grid and commands,
|
|
241
|
-
and works through restricted clients rather than the editor API or the workbook
|
|
242
|
-
handle:
|
|
243
|
-
|
|
244
|
-
```tsx
|
|
245
|
-
import { XlsxEditor, defineXlsxPlugin } from "@betteroffice/xlsx-react";
|
|
246
|
-
|
|
247
|
-
type State = { version: string | null; sheets: number };
|
|
248
|
-
|
|
249
|
-
const review = defineXlsxPlugin<State>({
|
|
250
|
-
id: "acme.review",
|
|
251
|
-
createState: () => ({ version: null, sheets: 0 }),
|
|
252
|
-
async onEvent(context, event) {
|
|
253
|
-
if (event.type !== "load" && event.type !== "document-change") return;
|
|
254
|
-
const read = await context.read.readCells({ ranges: [] });
|
|
255
|
-
if (read.ok) context.setState({ version: read.version, sheets: read.sheets.length }, read.version);
|
|
256
|
-
},
|
|
257
|
-
panel: {
|
|
258
|
-
title: "Review",
|
|
259
|
-
placement: "right",
|
|
260
|
-
render: ({ context }) => <p>{context.state.sheets} sheets</p>,
|
|
261
|
-
},
|
|
262
|
-
overlay: ({ context, geometry }) => {
|
|
263
|
-
const selection = context.snapshot.selection;
|
|
264
|
-
const focus = selection?.cells?.focus;
|
|
265
|
-
const box = selection && focus ? geometry.getCellRect({ sheetId: selection.sheetId, ...focus }) : null;
|
|
266
|
-
return box ? (
|
|
267
|
-
<div style={{ position: "absolute", left: box.x, top: box.y, width: box.width, height: box.height, outline: "2px dashed #2563eb" }} />
|
|
268
|
-
) : null;
|
|
269
|
-
},
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
<XlsxEditor
|
|
273
|
-
file={bytes}
|
|
274
|
-
plugins={[review]}
|
|
275
|
-
pluginGrants={{ "acme.review": { document: "write", editBatches: true } }}
|
|
276
|
-
onPluginError={(error) => report(error)}
|
|
277
|
-
/>;
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
- **Lifecycle.** Once a workbook is open, each plugin gets fresh state,
|
|
281
|
-
`initialize`, then one `load` event (`loaded`, `replaced`, or `attached` for a
|
|
282
|
-
plugin added to an open workbook), and its contributions appear. A workbook
|
|
283
|
-
change the hook did not make itself aborts it and delivers `load` again; after
|
|
284
|
-
ten such runs the plugin is stopped and reported. It then receives
|
|
285
|
-
`document-change` (the committed version after recalculation, for typing,
|
|
286
|
-
pastes, commands, batches, its own included, undo, redo and remote updates,
|
|
287
|
-
never for refusals or no-ops), `selection-change`, `mode-change` (with
|
|
288
|
-
`readOnly`), `layout-change` and `grants-change`. Events describe current
|
|
289
|
-
state: several changes may arrive as one, and a newer one aborts the hook
|
|
290
|
-
still handling the previous (`context.signal`), except a change that hook's
|
|
291
|
-
own edit batch made. Replacing the workbook, removing the plugin, changing its `revision`, unmounting, or a
|
|
292
|
-
failure ends the activation: its signals abort, its clients refuse, and every
|
|
293
|
-
`onCleanup` disposer runs once with the reason. Plugins are matched by `id`
|
|
294
|
-
and `revision`, so new array or callback identities and reordering keep their
|
|
295
|
-
state. `defineXlsxPlugin` copies the panel, commands and toolbar, so changing
|
|
296
|
-
them takes a new definition. `context.run(action)` gives event handlers a
|
|
297
|
-
fresh context and isolates their failures. `onReady` is unaffected by plugins.
|
|
298
|
-
- **Stale results.** `setState` returns false once the context is superseded or
|
|
299
|
-
ended, or when the workbook is no longer at `atVersion` (by default the
|
|
300
|
-
version the context was created at).
|
|
301
|
-
- **Reads and edits.** `context.read` offers `version`, `readCells`, `findText`
|
|
302
|
-
and `validateEdits`, and `context.edits.applyEdits` the editor's
|
|
303
|
-
version-checked batches. Each call waits for pending input in order with it,
|
|
304
|
-
but plugin handlers run outside that queue, so a command can await its own
|
|
305
|
-
batch.
|
|
306
|
-
- **Grants.** Without a grant a plugin reads, validates and navigates only, and
|
|
307
|
-
`context.edits` is null. Built-in commands need their id in `commands`, and
|
|
308
|
-
mutating ones also `document: "write"`; edit batches need `document: "write"`
|
|
309
|
-
and `editBatches`, and `history: "none"` also `untrackedHistory`. The grant
|
|
310
|
-
and `readOnly` are checked again right before each change, so a revoked grant,
|
|
311
|
-
`readOnly` or a replaced workbook refuses even through a client obtained
|
|
312
|
-
earlier. Mutating built-in commands refuse plugins with `unsupported-policy`;
|
|
313
|
-
plugins change workbooks through edit batches. Grants govern the plugin API,
|
|
314
|
-
separately from sheet and workbook protection.
|
|
315
|
-
- **Contributed commands** register as `plugin:<pluginId>/<id>` on
|
|
316
|
-
`api.commands`. They always run with their own plugin's clients, even when the
|
|
317
|
-
toolbar, a shortcut or the host invokes them. `execute` returns
|
|
318
|
-
`{ ok: true, status }` or a failure with the plugin's own code, or a refused
|
|
319
|
-
edit batch as-is; callers receive it unchanged. `mutatesDocument` disables a
|
|
320
|
-
command while read-only but grants nothing. Plugin shortcuts must use Mod or
|
|
321
|
-
Alt, or a function key; built-in shortcuts win and a clashing plugin shortcut
|
|
322
|
-
is reported. `toolbar` lists local ids in
|
|
323
|
-
order: the default toolbar shows them, and replacement chrome places
|
|
324
|
-
`<XlsxPluginToolbar />` where it wants them. `ToolbarCommandButton`,
|
|
325
|
-
`ToolbarCommand`, `useXlsxCommand` and `useXlsxCommandState` also take a
|
|
326
|
-
contributed id.
|
|
327
|
-
- **Selection and navigation.** `snapshot.selection` names the active sheet
|
|
328
|
-
(`sheetId` and the zero-based `sheetIndex`), the selected `cells` with their
|
|
329
|
-
`anchor` and `focus` direction, and the `chartId` of a selected chart.
|
|
330
|
-
`navigation.selectCells({ sheetId, selection }, { expectVersion, focus })`
|
|
331
|
-
activates the sheet, selects the cells and reveals the focus cell.
|
|
332
|
-
`navigation.scrollToCell({ sheetId, row, col }, { expectVersion, align })`
|
|
333
|
-
reveals a cell (`nearest` by default, `start` or `center`) and keeps the
|
|
334
|
-
selection; a cell on another sheet activates it with nothing selected. Both
|
|
335
|
-
resolve sheet ids after pending input against `expectVersion`, keep keyboard
|
|
336
|
-
focus unless `selectCells` gets `focus: true`, and refuse with
|
|
337
|
-
`stale-version` or `missing-target` rather than retarget. Sheet ids and
|
|
338
|
-
versions are session-scoped.
|
|
339
|
-
- **Geometry.** `context.geometry` exists only while the canvas shows a painted
|
|
340
|
-
frame of the current version. `layout` carries the frame's `sheetId`, `zoom`
|
|
341
|
-
and painted `viewport` (unzoomed sheet pixels), and gets a new `id` with every
|
|
342
|
-
scroll, zoom, resize and sheet switch. `getCellRect({ sheetId, row, col })`
|
|
343
|
-
and `getRangeRect({ sheetId, range })` return pixels of the overlay layer,
|
|
344
|
-
zoomed and clipped to the visible grid with frozen panes placed as painted;
|
|
345
|
-
cells scrolled out of view or on another sheet return null. `getCellRect` is
|
|
346
|
-
one grid cell; address a merged area as a range. `getPositionAtPoint` is null;
|
|
347
|
-
check it before calling. Every method returns null once its layout is gone. The overlay layer sits on the grid below the editor's
|
|
348
|
-
selection and cell editor, and ignores the pointer unless an element sets
|
|
349
|
-
`pointer-events: auto`.
|
|
350
|
-
- **Panels** dock left, right or bottom of the grid, below the toolbar and
|
|
351
|
-
above the sheet tabs, with tabs when several share a side. `preferredSize` is
|
|
352
|
-
clamped to 40% of the workspace. In a narrow editor side docks show their
|
|
353
|
-
tabs, and a tab opens its panel as a drawer that Escape closes. Collapsing a
|
|
354
|
-
panel keeps the plugin running. The proposals panel opens inside the grid
|
|
355
|
-
area.
|
|
356
|
-
- **Failures.** Every contribution renders behind its own error boundary with a
|
|
357
|
-
command context restricted to its plugin. Keys pressed in plugin chrome are
|
|
358
|
-
the user's: built-in shortcuts act on the editor there, a plugin's text fields
|
|
359
|
-
keep their own editing keys, and no key or click in a contribution reaches the
|
|
360
|
-
grid. A throwing hook, renderer, command-state function, command or
|
|
361
|
-
`context.run` action stops only that plugin, runs its cleanups and reports
|
|
362
|
-
`{ pluginId, generation, phase, error }` to `onPluginError`. Plugins run in
|
|
363
|
-
the page's realm: grants govern the supported API, not a sandbox.
|
|
28
|
+
`file` contains XLSX bytes. Give the editor's container a height; omitting `file` shows an empty
|
|
29
|
+
editor that asks for an XLSX file. See [hosting](https://docs.betteroffice.dev/docs/migrating-to-0-5#hosting) for worker, chunk and CSP needs.
|
|
30
|
+
|
|
31
|
+
## Key options
|
|
32
|
+
|
|
33
|
+
The default props are described by `XlsxAsyncEditorProps`:
|
|
34
|
+
|
|
35
|
+
| prop | type | description |
|
|
36
|
+
| --- | --- | --- |
|
|
37
|
+
| `file` | `Uint8Array \| ArrayBuffer` | XLSX bytes to open. |
|
|
38
|
+
| `fileName` | `string` | Download name; defaults to `workbook.xlsx`. |
|
|
39
|
+
| `onReady` | `(api: XlsxAsyncEditorApi) => void \| (() => void)` | Receive the editor API; optionally return cleanup. |
|
|
40
|
+
| `onSave` | `(bytes: Uint8Array) => void` | Receive saved workbook bytes. |
|
|
41
|
+
| `onSaveRequest` | `() => boolean \| void \| Promise<boolean \| void>` | Intercept saves; return `true` to continue. |
|
|
42
|
+
| `downloadOnSave` | `boolean` | Save also downloads; defaults to `!onSave`. |
|
|
43
|
+
| `onDocumentChange` | `(change: XlsxDocumentChange) => void` | Receives `{ version }` after a committed edit. |
|
|
44
|
+
| `initialZoom` | `number` | Initial scale, clamped to 0.1 through 5; defaults to 1. |
|
|
45
|
+
| `loadingIndicator` | `React.ReactNode` | Shown while a supplied file opens. |
|
|
46
|
+
| `style` | `React.CSSProperties` | Overrides root inline styles. |
|
|
47
|
+
| `onChange` | `() => void` | Called after a user edit changes the workbook. |
|
|
48
|
+
| `onError` | `(error: Error) => void` | Receive engine errors. |
|
|
49
|
+
| `readOnly` | `boolean` | Block user edits while keeping navigation and selection. |
|
|
50
|
+
| `toolbar` | `React.ReactNode` | Replace the default toolbar; `null` removes it. |
|
|
51
|
+
| `showToolbar` | `boolean` | `false` hides the toolbar region, including custom content. |
|
|
52
|
+
| `collaboration` | `XlsxEditorCollaborationOptions` | Configure the editor-owned replica and presence provider. |
|
|
53
|
+
| `i18n` | `Translations` | Override UI strings using `@betteroffice/xlsx-i18n`. |
|
|
54
|
+
| `plugins` | `readonly XlsxPlugin[]` | Install host plugins. |
|
|
55
|
+
| `pluginGrants` | `Readonly<Record<string, XlsxPluginGrant>>` | Grant capabilities by plugin ID. |
|
|
56
|
+
| `onPluginError` | `(error: XlsxPluginError) => void` | Receive plugin failures. |
|
|
57
|
+
| `className` | `string` | CSS class for the editor root. |
|
|
58
|
+
|
|
59
|
+
All listed props are optional. The default toolbar is hidden when `readOnly`; a supplied `toolbar`
|
|
60
|
+
remains visible unless `showToolbar` is `false`.
|
|
61
|
+
|
|
62
|
+
Deprecated: `legacySyncApi` keeps the synchronous API on the in-thread engine and is removed in 0.6.0; omit it
|
|
63
|
+
for the default asynchronous editor API.
|
|
64
|
+
|
|
65
|
+
## Editor API
|
|
66
|
+
|
|
67
|
+
`onReady` receives `XlsxAsyncEditorApi`. Its exports wait for pending editor input before reading the
|
|
68
|
+
workbook, and its command store is shared with the toolbar and host controls.
|
|
69
|
+
|
|
70
|
+
| member | purpose |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| `hydrated` | Whether the worker editor is ready. |
|
|
73
|
+
| `whenHydrated()` | Await editor readiness. |
|
|
74
|
+
| `failure` | Current engine failure, or `null`. |
|
|
75
|
+
| `commands` | Inspect command availability and execute actions asynchronously. |
|
|
76
|
+
| `version()`, `readCells`, `findText` | Read after pending input commits. |
|
|
77
|
+
| `validateEdits`, `applyEdits` | Validate or apply version-checked batches; refuse while read-only. |
|
|
78
|
+
| `save()` | Commit input, return bytes and call `onSave` without downloading or consulting `onSaveRequest`. |
|
|
79
|
+
| `focus()` | Focus the editor. |
|
|
80
|
+
| `renderPng`, `renderRangePng`, `printDisplayList` | Render asynchronously. |
|
|
81
|
+
| `exportStructured`, `exportMarkdown` | Export content asynchronously. |
|
|
82
|
+
| `proposeChanges` | Optional method for staging tracked cell edits on worker editors. |
|
|
83
|
+
| `collaboration` | Asynchronous collaboration replica, or `null`. |
|
|
84
|
+
| `recoverySave()` | Recover bytes after a non-collaborative worker session fails. |
|
|
85
|
+
|
|
86
|
+
Treat the API as belonging to the open document. Return cleanup from `onReady` to release host
|
|
87
|
+
subscriptions when that document is replaced or the editor unmounts.
|
|
88
|
+
|
|
89
|
+
## Saving
|
|
90
|
+
|
|
91
|
+
Use `await api.commands.execute('save', null)` to save from host UI. The command waits for accepted
|
|
92
|
+
input and calls `onSave` when supplied. It also downloads when `downloadOnSave ?? !onSave` is true.
|
|
93
|
+
Use `await api.save()` to receive bytes directly without downloading or consulting `onSaveRequest`.
|
|
94
|
+
|
|
95
|
+
`onSaveRequest` intercepts toolbar saves, the save command, and Ctrl/Cmd+S before serialization.
|
|
96
|
+
Return `true` to continue built-in saving; `false` or `void` handles or cancels the request. Promises
|
|
97
|
+
are awaited, concurrent requests are coalesced, and requests for replaced documents are discarded.
|
|
98
|
+
|
|
99
|
+
Commands return a success or a typed failure. A refused cell draft must be corrected or discarded
|
|
100
|
+
before saving can complete. `recoverySave()` requires a failed non-collaborative worker session and
|
|
101
|
+
returns `{ bytes, recovery: true }`.
|
|
102
|
+
|
|
103
|
+
## Toolbar
|
|
104
|
+
|
|
105
|
+
The toolbar composition API is experimental. Use `toolbar` to supply controls connected to the
|
|
106
|
+
editor's command store. See
|
|
107
|
+
[Compose the XLSX toolbar](https://docs.betteroffice.dev/docs/javascript#compose-the-xlsx-toolbar)
|
|
108
|
+
for built-in parts, custom controls, and command results.
|
|
109
|
+
|
|
110
|
+
## Plugins and edit batches
|
|
111
|
+
|
|
112
|
+
The plugin API is experimental and may change in minor releases. Plugins contribute panels,
|
|
113
|
+
overlays, and commands through restricted clients; their read and edit
|
|
114
|
+
clients support version-checked operations. See
|
|
115
|
+
[Host XLSX editor plugins](https://docs.betteroffice.dev/docs/javascript#host-xlsx-editor-plugins)
|
|
116
|
+
and [Edit XLSX in version-checked batches](https://docs.betteroffice.dev/docs/javascript#edit-xlsx-in-version-checked-batches).
|
|
117
|
+
|
|
118
|
+
## Structured export
|
|
119
|
+
|
|
120
|
+
Await `api.exportStructured()` or `api.exportMarkdown()` for content with positional anchors and
|
|
121
|
+
omission diagnostics. See
|
|
122
|
+
[Export XLSX content with anchors](https://docs.betteroffice.dev/docs/javascript#export-xlsx-content-with-anchors)
|
|
123
|
+
for scopes, hidden content, and export limits.
|
|
364
124
|
|
|
365
125
|
## Collaboration
|
|
366
126
|
|
|
367
|
-
Pass `collaboration`
|
|
368
|
-
|
|
127
|
+
Pass `collaboration` and connect a provider when `collaboration.onReplica` publishes the replica.
|
|
128
|
+
The editor retains its default worker runtime; see the
|
|
129
|
+
[collaboration guide](https://docs.betteroffice.dev/docs/collaboration) for transport setup and cleanup.
|
|
369
130
|
|
|
370
|
-
|
|
371
|
-
import { CollaborationProvider } from "@betteroffice/xlsx/collaboration";
|
|
372
|
-
|
|
373
|
-
<XlsxEditor
|
|
374
|
-
file={file}
|
|
375
|
-
collaboration={{ clientId }}
|
|
376
|
-
onReady={({ handle }) => {
|
|
377
|
-
const provider = new CollaborationProvider(handle, transport, {
|
|
378
|
-
user: { name: "Ada" }, // shown on this peer's selection flag
|
|
379
|
-
});
|
|
380
|
-
provider.connect();
|
|
381
|
-
return () => provider.destroy();
|
|
382
|
-
}}
|
|
383
|
-
/>;
|
|
384
|
-
```
|
|
131
|
+
## Documentation
|
|
385
132
|
|
|
386
|
-
[JavaScript guide](https://docs.betteroffice.dev/docs/javascript)
|
|
387
|
-
[
|
|
133
|
+
- [JavaScript guide](https://docs.betteroffice.dev/docs/javascript)
|
|
134
|
+
- [Collaboration guide](https://docs.betteroffice.dev/docs/collaboration)
|
|
135
|
+
- [Package guide](https://docs.betteroffice.dev/docs/packages)
|
|
136
|
+
- [XLSX core on npm](https://www.npmjs.com/package/@betteroffice/xlsx)
|
|
137
|
+
- [Localization package on npm](https://www.npmjs.com/package/@betteroffice/xlsx-i18n)
|
|
138
|
+
- [Repository](https://github.com/openooxml/betteroffice)
|