@canvas-components/xlsx-table 0.1.3 → 0.1.5

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.
Files changed (2) hide show
  1. package/README.md +220 -220
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,220 +1,220 @@
1
- # @canvas-components/xlsx-table
2
-
3
- `@canvas-components/xlsx-table` 是面向在线表格 / 类 Excel 编辑场景的纯原生 Canvas 核心包。
4
-
5
- 它与 `list-table` 职责分离:
6
-
7
- | 包 | 场景 |
8
- | ------------ | -------------------------------- |
9
- | `list-table` | 业务数据表、查询、汇总、列配置 |
10
- | `xlsx-table` | workbook / sheet / cell 网格编辑 |
11
-
12
- ## 安装
13
-
14
- ```bash
15
- pnpm add @canvas-components/xlsx-table
16
- ```
17
-
18
- ## 快速使用
19
-
20
- ```ts
21
- import { XlsxTable } from "@canvas-components/xlsx-table";
22
-
23
- const container = document.querySelector("#sheet") as HTMLElement;
24
-
25
- const table = new XlsxTable(container, {
26
- workbook: {
27
- sheets: [
28
- {
29
- id: "sheet1",
30
- name: "Sheet1",
31
- rowCount: 100,
32
- columnCount: 26,
33
- cells: {
34
- r0c0: { value: "商品" },
35
- r0c1: { value: "数量" },
36
- r1c0: { value: "热敏标签纸" },
37
- r1c1: { value: 20, type: "number" },
38
- },
39
- },
40
- ],
41
- },
42
- width: "100%",
43
- height: 480,
44
- onWorkbookChange: (workbook) => {
45
- console.log("workbook version", workbook.version);
46
- },
47
- });
48
-
49
- table.mount();
50
- ```
51
-
52
- ## 当前能力
53
-
54
- - 单 sheet 网格渲染、行列头、虚拟滚动
55
- - 多 sheet:切换 / 新增 / 重命名 / 复制 / 左右移动 / 删除,按 sheet 记忆选区与滚动
56
- - 活动格 / 选区:点击、拖拽、Shift 扩展、Cmd/Ctrl+点击多选区、行头/列头整行整列、Cmd/Ctrl+A(used→整表)、方向键导航
57
- - 单元格编辑:双击 / Enter / F2 / 直接输入,`onEditorRequest` 驱动宿主编辑器
58
- - 公式栏状态:`onFormulaBarChange` / `commitFormulaBarText`
59
- - 复制粘贴:TSV 与 Excel/飞书互通;Delete/Backspace 清空;`clipboard.expandBounds` 预留扩展
60
- - 填充柄:copy 策略拖拽填充
61
- - 填充柄:auto 策略(数字/日期等差序列,文本回退 copy)
62
- - 列宽 / 行高拖拽:表头边界拖拽调整,支持撤销
63
- - 单元格样式:选区文字颜色、填充色、字号、字重、斜体、下划线、水平对齐,支持撤销
64
- - 单元格数字类型:常规 / 数字 / 金额 / 百分比
65
- - 行列折叠:行头 / 列头右键折叠,支持撤销
66
- - 冻结窗格:`sheet.frozen.rows/columns`,滚动时冻结区固定
67
- - 公式求值:`=A1+B1`、比较/拼接、`IF/AND/OR`、文本与数学函数、`Sheet1!A1` 跨 sheet、区间与循环检测
68
- - 公式依赖图:增量拓扑重算、跨 sheet 下游传播、大区间依赖边、命名区域依赖
69
- - 公式依赖图调试快照:`getFormulaDependencyDebugSnapshot` / `createFormulaDependencyDebugSnapshot`
70
- - 易失公式:`TODAY()` / `NOW()` 自动纳入增量重算种子
71
- - 数组公式 MVP:数组常量 `{1,2;3,4}`、`SEQUENCE` / `TRANSPOSE` spill、`#SPILL!` 阻塞提示
72
- - 命名区域:workbook 级 `namedRanges`,支持 `=TaxRate` / `=SUM(Sales)`
73
- - 公式自动补全:函数名与命名区域候选(可复用 `getFormulaAutocompleteSuggestions`)
74
- - 高频函数:`COUNTBLANK` / `SUMIF(S)` / `COUNTIF(S)` / `VLOOKUP` / `HLOOKUP` / `INDEX` / `MATCH` / `TEXT` / `VALUE` / 日期函数
75
- - 撤销重做:Cmd/Ctrl+Z / Shift+Z / Y,统一 patch history
76
- - 多人协同:`collaboration` adapter、远端 patch apply、presence 选区渲染、mock 双实例同步
77
- - WebSocket 协同适配器:`WebSocketXlsxCollaborationAdapter`(JSON 协议、重连、出站队列、patch-ack)
78
- - 服务端权威源:`XlsxAuthoritativeCollaborationRoom`、单元格级 OT 裁剪、鉴权 hook、公式权威重算
79
-
80
- ## 与 list-table 的边界
81
-
82
- 1. 不把 Excel 编辑能力塞回 list-table。
83
- 2. xlsx-table 不实现业务查询、汇总行、行选择等数据表语义。
84
- 3. 核心包不依赖 React;React 适配见 `@canvas-components/react-xlsx-table`。
85
- 4. 多人协同提供协议、Mock 与 WebSocket adapter,不绑定具体业务服务端;见 `docs/collaboration-protocol.md`。
86
-
87
- ## 主要 API
88
-
89
- ```ts
90
- const table = new XlsxTable(container, options);
91
- table.mount();
92
- table.updateOptions(options);
93
- table.getSelection();
94
- table.getWorkbook();
95
- table.getFormulaDependencyDebugSnapshot();
96
- table.getFormulaBarState();
97
- table.getActiveSheetId();
98
- table.getNamedRanges();
99
- table.setActiveSheet(sheetId);
100
- table.addSheet({ name?: string, activate?: boolean });
101
- table.duplicateSheet(sheetId, { name?: string, activate?: boolean });
102
- table.moveSheet(sheetId, targetIndex);
103
- table.renameSheet(sheetId, name);
104
- table.removeSheet(sheetId);
105
- table.upsertNamedRange(named);
106
- table.removeNamedRange(name);
107
- table.startEditing("edit");
108
- table.commitFormulaBarText("=A1");
109
- table.undo();
110
- table.redo();
111
- table.getHistoryState();
112
- table.getRemoteSelections();
113
- table.getPresenceRecord();
114
- table.acknowledgePatches(["patch_id"]);
115
- table.hideRows([1, 2]);
116
- table.hideColumns([3]);
117
- table.clearSelectionValues();
118
- table.setSelectionCellStyle({ fontWeight: "600", color: "#1456f0" });
119
- table.toggleSelectionCellStyle({ fontWeight: "600" }, { fontWeight: undefined });
120
- table.clearSelectionCellStyle();
121
- table.clearSelectionCellStyleKeys(["color", "backgroundColor"]);
122
- table.setSelectionCellType("money");
123
- table.setFrozenPane({ rows: 1, columns: 1 });
124
- table.freezeToSelection();
125
- table.clearFreeze();
126
- table.insertFormulaTemplate("=SUM()");
127
- table.insertRows(2);
128
- table.insertColumns(3);
129
- table.deleteRows(2);
130
- table.deleteColumns(3);
131
- table.destroy();
132
- ```
133
-
134
- 命名区域:
135
-
136
- ```ts
137
- import {
138
- upsertNamedRange,
139
- removeNamedRange,
140
- } from "@canvas-components/xlsx-table";
141
-
142
- table.upsertNamedRange({
143
- name: "Sales",
144
- sheetId: "sheet1",
145
- range: {
146
- startRowIndex: 0,
147
- startColumnIndex: 1,
148
- endRowIndex: 9,
149
- endColumnIndex: 1,
150
- },
151
- });
152
- ```
153
-
154
- 常用 options:
155
-
156
- - `workbook` / `theme` / `readonly`
157
- - `onWorkbookChange` / `onSelectionChange`
158
- - `onEditorRequest` / `onFormulaBarChange`
159
- - `onActiveSheetChange`
160
- - `clipboard.expandBounds`
161
- - `collaboration` / `onLocalPatches`
162
-
163
- ## 文档站
164
-
165
- 本地文档:
166
-
167
- - `/components/xlsx-table/basic`
168
- - `/components/xlsx-table/editing`
169
- - `/components/xlsx-table/collab`
170
- - `/components/xlsx-table/multi-sheet`
171
-
172
- 交互补充:拖动列头右边界改列宽,拖动行头下边界改行高。
173
-
174
- 协同协议:`docs/collaboration-protocol.md`。
175
-
176
- ### WebSocket 协同接入
177
-
178
- ```ts
179
- import {
180
- XlsxTable,
181
- WebSocketXlsxCollaborationAdapter,
182
- } from "@canvas-components/xlsx-table";
183
-
184
- const adapter = new WebSocketXlsxCollaborationAdapter({
185
- roomId: "workbook-1",
186
- user: { id: "user-1", name: "Alice" },
187
- url: "wss://example.com/xlsx?room=workbook-1&token=...",
188
- });
189
-
190
- const table = new XlsxTable(container, {
191
- workbook,
192
- collaboration: {
193
- self: { id: "user-1", name: "Alice" },
194
- adapter,
195
- },
196
- });
197
-
198
- adapter.onPatchAck(({ patchIds }) => table.acknowledgePatches(patchIds));
199
- ```
200
-
201
- ### 服务端权威源
202
-
203
- ```ts
204
- import { XlsxAuthoritativeCollaborationRoom } from "@canvas-components/xlsx-table";
205
-
206
- const room = new XlsxAuthoritativeCollaborationRoom({
207
- workbook,
208
- conflictPolicy: "server",
209
- authorizePatch: ({ user, patch }) => {
210
- return user.id === patch.authorId || "无权写入该 patch";
211
- },
212
- });
213
-
214
- const result = room.submitPatches({
215
- user,
216
- patches,
217
- });
218
-
219
- // result.acceptedPatches 广播给房间;result.ackPatchIds 回发给提交者。
220
- ```
1
+ # @canvas-components/xlsx-table
2
+
3
+ `@canvas-components/xlsx-table` 是面向在线表格 / 类 Excel 编辑场景的纯原生 Canvas 核心包。
4
+
5
+ 它与 `list-table` 职责分离:
6
+
7
+ | 包 | 场景 |
8
+ | ------------ | -------------------------------- |
9
+ | `list-table` | 业务数据表、查询、汇总、列配置 |
10
+ | `xlsx-table` | workbook / sheet / cell 网格编辑 |
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ pnpm add @canvas-components/xlsx-table
16
+ ```
17
+
18
+ ## 快速使用
19
+
20
+ ```ts
21
+ import { XlsxTable } from "@canvas-components/xlsx-table";
22
+
23
+ const container = document.querySelector("#sheet") as HTMLElement;
24
+
25
+ const table = new XlsxTable(container, {
26
+ workbook: {
27
+ sheets: [
28
+ {
29
+ id: "sheet1",
30
+ name: "Sheet1",
31
+ rowCount: 100,
32
+ columnCount: 26,
33
+ cells: {
34
+ r0c0: { value: "商品" },
35
+ r0c1: { value: "数量" },
36
+ r1c0: { value: "热敏标签纸" },
37
+ r1c1: { value: 20, type: "number" },
38
+ },
39
+ },
40
+ ],
41
+ },
42
+ width: "100%",
43
+ height: 480,
44
+ onWorkbookChange: (workbook) => {
45
+ console.log("workbook version", workbook.version);
46
+ },
47
+ });
48
+
49
+ table.mount();
50
+ ```
51
+
52
+ ## 当前能力
53
+
54
+ - 单 sheet 网格渲染、行列头、虚拟滚动
55
+ - 多 sheet:切换 / 新增 / 重命名 / 复制 / 左右移动 / 删除,按 sheet 记忆选区与滚动
56
+ - 活动格 / 选区:点击、拖拽、Shift 扩展、Cmd/Ctrl+点击多选区、行头/列头整行整列、Cmd/Ctrl+A(used→整表)、方向键导航
57
+ - 单元格编辑:双击 / Enter / F2 / 直接输入,`onEditorRequest` 驱动宿主编辑器
58
+ - 公式栏状态:`onFormulaBarChange` / `commitFormulaBarText`
59
+ - 复制粘贴:TSV 与 Excel/飞书互通;Delete/Backspace 清空;`clipboard.expandBounds` 预留扩展
60
+ - 填充柄:copy 策略拖拽填充
61
+ - 填充柄:auto 策略(数字/日期等差序列,文本回退 copy)
62
+ - 列宽 / 行高拖拽:表头边界拖拽调整,支持撤销
63
+ - 单元格样式:选区文字颜色、填充色、字号、字重、斜体、下划线、水平对齐,支持撤销
64
+ - 单元格数字类型:常规 / 数字 / 金额 / 百分比
65
+ - 行列折叠:行头 / 列头右键折叠,支持撤销
66
+ - 冻结窗格:`sheet.frozen.rows/columns`,滚动时冻结区固定
67
+ - 公式求值:`=A1+B1`、比较/拼接、`IF/AND/OR`、文本与数学函数、`Sheet1!A1` 跨 sheet、区间与循环检测
68
+ - 公式依赖图:增量拓扑重算、跨 sheet 下游传播、大区间依赖边、命名区域依赖
69
+ - 公式依赖图调试快照:`getFormulaDependencyDebugSnapshot` / `createFormulaDependencyDebugSnapshot`
70
+ - 易失公式:`TODAY()` / `NOW()` 自动纳入增量重算种子
71
+ - 数组公式 MVP:数组常量 `{1,2;3,4}`、`SEQUENCE` / `TRANSPOSE` spill、`#SPILL!` 阻塞提示
72
+ - 命名区域:workbook 级 `namedRanges`,支持 `=TaxRate` / `=SUM(Sales)`
73
+ - 公式自动补全:函数名与命名区域候选(可复用 `getFormulaAutocompleteSuggestions`)
74
+ - 高频函数:`COUNTBLANK` / `SUMIF(S)` / `COUNTIF(S)` / `VLOOKUP` / `HLOOKUP` / `INDEX` / `MATCH` / `TEXT` / `VALUE` / 日期函数
75
+ - 撤销重做:Cmd/Ctrl+Z / Shift+Z / Y,统一 patch history
76
+ - 多人协同:`collaboration` adapter、远端 patch apply、presence 选区渲染、mock 双实例同步
77
+ - WebSocket 协同适配器:`WebSocketXlsxCollaborationAdapter`(JSON 协议、重连、出站队列、patch-ack)
78
+ - 服务端权威源:`XlsxAuthoritativeCollaborationRoom`、单元格级 OT 裁剪、鉴权 hook、公式权威重算
79
+
80
+ ## 与 list-table 的边界
81
+
82
+ 1. 不把 Excel 编辑能力塞回 list-table。
83
+ 2. xlsx-table 不实现业务查询、汇总行、行选择等数据表语义。
84
+ 3. 核心包不依赖 React;React 适配见 `@canvas-components/react-xlsx-table`。
85
+ 4. 多人协同提供协议、Mock 与 WebSocket adapter,不绑定具体业务服务端;见 `docs/collaboration-protocol.md`。
86
+
87
+ ## 主要 API
88
+
89
+ ```ts
90
+ const table = new XlsxTable(container, options);
91
+ table.mount();
92
+ table.updateOptions(options);
93
+ table.getSelection();
94
+ table.getWorkbook();
95
+ table.getFormulaDependencyDebugSnapshot();
96
+ table.getFormulaBarState();
97
+ table.getActiveSheetId();
98
+ table.getNamedRanges();
99
+ table.setActiveSheet(sheetId);
100
+ table.addSheet({ name?: string, activate?: boolean });
101
+ table.duplicateSheet(sheetId, { name?: string, activate?: boolean });
102
+ table.moveSheet(sheetId, targetIndex);
103
+ table.renameSheet(sheetId, name);
104
+ table.removeSheet(sheetId);
105
+ table.upsertNamedRange(named);
106
+ table.removeNamedRange(name);
107
+ table.startEditing("edit");
108
+ table.commitFormulaBarText("=A1");
109
+ table.undo();
110
+ table.redo();
111
+ table.getHistoryState();
112
+ table.getRemoteSelections();
113
+ table.getPresenceRecord();
114
+ table.acknowledgePatches(["patch_id"]);
115
+ table.hideRows([1, 2]);
116
+ table.hideColumns([3]);
117
+ table.clearSelectionValues();
118
+ table.setSelectionCellStyle({ fontWeight: "600", color: "#1456f0" });
119
+ table.toggleSelectionCellStyle({ fontWeight: "600" }, { fontWeight: undefined });
120
+ table.clearSelectionCellStyle();
121
+ table.clearSelectionCellStyleKeys(["color", "backgroundColor"]);
122
+ table.setSelectionCellType("money");
123
+ table.setFrozenPane({ rows: 1, columns: 1 });
124
+ table.freezeToSelection();
125
+ table.clearFreeze();
126
+ table.insertFormulaTemplate("=SUM()");
127
+ table.insertRows(2);
128
+ table.insertColumns(3);
129
+ table.deleteRows(2);
130
+ table.deleteColumns(3);
131
+ table.destroy();
132
+ ```
133
+
134
+ 命名区域:
135
+
136
+ ```ts
137
+ import {
138
+ upsertNamedRange,
139
+ removeNamedRange,
140
+ } from "@canvas-components/xlsx-table";
141
+
142
+ table.upsertNamedRange({
143
+ name: "Sales",
144
+ sheetId: "sheet1",
145
+ range: {
146
+ startRowIndex: 0,
147
+ startColumnIndex: 1,
148
+ endRowIndex: 9,
149
+ endColumnIndex: 1,
150
+ },
151
+ });
152
+ ```
153
+
154
+ 常用 options:
155
+
156
+ - `workbook` / `theme` / `readonly`
157
+ - `onWorkbookChange` / `onSelectionChange`
158
+ - `onEditorRequest` / `onFormulaBarChange`
159
+ - `onActiveSheetChange`
160
+ - `clipboard.expandBounds`
161
+ - `collaboration` / `onLocalPatches`
162
+
163
+ ## 文档站
164
+
165
+ 本地文档:
166
+
167
+ - `/components/xlsx-table/basic`
168
+ - `/components/xlsx-table/editing`
169
+ - `/components/xlsx-table/collab`
170
+ - `/components/xlsx-table/multi-sheet`
171
+
172
+ 交互补充:拖动列头右边界改列宽,拖动行头下边界改行高。
173
+
174
+ 协同协议:`docs/collaboration-protocol.md`。
175
+
176
+ ### WebSocket 协同接入
177
+
178
+ ```ts
179
+ import {
180
+ XlsxTable,
181
+ WebSocketXlsxCollaborationAdapter,
182
+ } from "@canvas-components/xlsx-table";
183
+
184
+ const adapter = new WebSocketXlsxCollaborationAdapter({
185
+ roomId: "workbook-1",
186
+ user: { id: "user-1", name: "Alice" },
187
+ url: "wss://example.com/xlsx?room=workbook-1&token=...",
188
+ });
189
+
190
+ const table = new XlsxTable(container, {
191
+ workbook,
192
+ collaboration: {
193
+ self: { id: "user-1", name: "Alice" },
194
+ adapter,
195
+ },
196
+ });
197
+
198
+ adapter.onPatchAck(({ patchIds }) => table.acknowledgePatches(patchIds));
199
+ ```
200
+
201
+ ### 服务端权威源
202
+
203
+ ```ts
204
+ import { XlsxAuthoritativeCollaborationRoom } from "@canvas-components/xlsx-table";
205
+
206
+ const room = new XlsxAuthoritativeCollaborationRoom({
207
+ workbook,
208
+ conflictPolicy: "server",
209
+ authorizePatch: ({ user, patch }) => {
210
+ return user.id === patch.authorId || "无权写入该 patch";
211
+ },
212
+ });
213
+
214
+ const result = room.submitPatches({
215
+ user,
216
+ patches,
217
+ });
218
+
219
+ // result.acceptedPatches 广播给房间;result.ackPatchIds 回发给提交者。
220
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canvas-components/xlsx-table",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Framework-agnostic canvas spreadsheet core for xlsx-table.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -26,7 +26,7 @@
26
26
  "pnpm": ">=9.0.0"
27
27
  },
28
28
  "dependencies": {
29
- "@canvas-components/utils": "0.2.8"
29
+ "@canvas-components/utils": "0.2.10"
30
30
  },
31
31
  "scripts": {
32
32
  "build": "tsup --config tsup.config.ts"