@senlinz/import-export 0.1.0-beta.9 → 0.1.1
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/LICENSE +21 -21
- package/README.md +196 -61
- package/README.zh.md +188 -0
- package/dist/index.js +2 -0
- package/dist/types/ExcelDefinition.d.ts +116 -0
- package/dist/types/index.d.ts +17 -7
- package/dist/types/runtime.d.ts +20 -0
- package/dist/types/utils.d.ts +8 -7
- package/package.json +22 -23
- package/dist/components/editable-cell.d.ts +0 -11
- package/dist/components/editable-cell.js +0 -2
- package/dist/components/import-export-definition.d.ts +0 -11
- package/dist/components/import-export-definition.js +0 -2
- package/dist/components/import-export-studio.d.ts +0 -11
- package/dist/components/import-export-studio.js +0 -2
- package/dist/components/import-export-table.d.ts +0 -11
- package/dist/components/import-export-table.js +0 -2
- package/dist/components/index.d.ts +0 -33
- package/dist/components/index.js +0 -2
- package/dist/components/p-3b0da608.js +0 -2
- package/dist/components/p-c9904f6a.js +0 -2
- package/dist/components/p-dd1bf849.js +0 -2
- package/dist/components/p-ddbe189c.js +0 -2
- package/dist/components/p-f7586df1.js +0 -2
- package/dist/tests/dist/editable-cell.d.ts +0 -11
- package/dist/tests/dist/imexport-table.d.ts +0 -11
- package/dist/tests/dist/import-export-definition.d.ts +0 -11
- package/dist/tests/dist/import-export-definiton.d.ts +0 -11
- package/dist/tests/dist/import-export-studio.d.ts +0 -11
- package/dist/tests/dist/import-export-table.d.ts +0 -11
- package/dist/tests/dist/index.d.ts +0 -33
- package/dist/tests/dist/table-definition.d.ts +0 -11
- package/dist/types/components/editable-cell/editable-cell.d.ts +0 -18
- package/dist/types/components/imexport-table/imexport-table.d.ts +0 -19
- package/dist/types/components/import-export-definition/import-export-definition.d.ts +0 -7
- package/dist/types/components/import-export-definition/import-export-definiton.d.ts +0 -6
- package/dist/types/components/import-export-studio/import-export-studio.d.ts +0 -14
- package/dist/types/components/import-export-studio/import-export-table.d.ts +0 -5
- package/dist/types/components/import-export-table/import-export-table.d.ts +0 -6
- package/dist/types/components/table-definition/table-definition.d.ts +0 -0
- package/dist/types/components.d.ts +0 -116
- package/dist/types/declarations/ExcelDefinition.d.ts +0 -17
- package/dist/types/declarations/ExcelDefintion.d.ts +0 -17
- package/dist/types/stencil-public-runtime.d.ts +0 -1674
- package/dist/types/types/index.d.ts +0 -19
- package/dist/types/utils/utils.d.ts +0 -9
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,61 +1,196 @@
|
|
|
1
|
-
# @senlinz/import-export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
1
|
+
# @senlinz/import-export
|
|
2
|
+
|
|
3
|
+
High-level browser API for Excel template generation, export, and import.
|
|
4
|
+
|
|
5
|
+
[中文文档](./README.zh.md)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @senlinz/import-export
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Choose your mode
|
|
14
|
+
|
|
15
|
+
### Default mode (recommended)
|
|
16
|
+
|
|
17
|
+
- No setup required.
|
|
18
|
+
- `importExcel`, `importExcelDynamic`, `exportExcel`, `fromExcel`, `fromExcelDynamic`, `toExcel`, `downloadExcelTemplate`, and `generateExcelTemplate` automatically initialize the bundled WASM runtime.
|
|
19
|
+
- Best for most browser applications.
|
|
20
|
+
|
|
21
|
+
### Advanced mode
|
|
22
|
+
|
|
23
|
+
- Call `initializeWasm(...)` yourself before using the Excel APIs.
|
|
24
|
+
- Use this when you want custom WASM hosting, bundler control, or to reuse bytes/modules that you loaded elsewhere.
|
|
25
|
+
- Supported manual inputs: `source`, `bytes`, or `module`.
|
|
26
|
+
- `bundledWasmSource` is also exported for self-contained demos/tests that still want explicit manual initialization.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { initializeWasm, toExcel } from '@senlinz/import-export';
|
|
30
|
+
|
|
31
|
+
const wasmBytes = new Uint8Array(
|
|
32
|
+
await (await fetch('/assets/imexport_wasm_bg.wasm')).arrayBuffer()
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
initializeWasm({ bytes: wasmBytes });
|
|
36
|
+
|
|
37
|
+
const workbook = await toExcel({
|
|
38
|
+
name: 'TomAndJerry',
|
|
39
|
+
columns: [{ key: 'name', name: 'Name', dataType: 'text' }],
|
|
40
|
+
}, [{ name: 'Tom' }]);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If you provide invalid manual input, `initializeWasm(...)` throws a clear error immediately.
|
|
44
|
+
|
|
45
|
+
## Supported API
|
|
46
|
+
|
|
47
|
+
### Stable definition fields
|
|
48
|
+
|
|
49
|
+
- `name` - file name used for template/export downloads
|
|
50
|
+
- `sheetName` - worksheet name used for export and preferred during import
|
|
51
|
+
- `columns` - stable schema for headers, validation, and row mapping
|
|
52
|
+
- `author` - optional workbook author metadata
|
|
53
|
+
- `createTime` - optional workbook creation time as a `Date` or date string
|
|
54
|
+
- `title`, `titleHeight`, `titleFormat` - optional merged title row and its layout/format
|
|
55
|
+
- `defaultRowHeight`, `headerRowHeight` - row heights for exported data rows and header rows
|
|
56
|
+
- `dx`, `dy` - horizontal and vertical offsets before the header starts
|
|
57
|
+
- `isHeaderFreeze` - freezes the header area so column labels stay visible while scrolling
|
|
58
|
+
- `progressCallback` - progress hook for long-running import/export work
|
|
59
|
+
- `imageFetcher` - required resolver for `image` columns during export
|
|
60
|
+
|
|
61
|
+
### Schema-less import
|
|
62
|
+
|
|
63
|
+
Use `importExcelDynamic(options?)` when you want to open the browser file picker without defining `columns` ahead of time.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { importExcelDynamic } from '@senlinz/import-export';
|
|
67
|
+
|
|
68
|
+
const result = await importExcelDynamic({
|
|
69
|
+
sheetName: 'sheet1',
|
|
70
|
+
headerRow: 1,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log(result.headers);
|
|
74
|
+
console.log(result.rows);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Use `fromExcelDynamic(buffer, options?)` when you want to read caller-provided workbook bytes without defining `columns` ahead of time.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { fromExcelDynamic } from '@senlinz/import-export';
|
|
81
|
+
|
|
82
|
+
const result = await fromExcelDynamic(fileBytes, {
|
|
83
|
+
sheetName: 'sheet1',
|
|
84
|
+
headerRow: 1,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
console.log(result.headers);
|
|
88
|
+
console.log(result.rows);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
- `importExcelDynamic(...)` depends on DOM/browser file upload APIs.
|
|
92
|
+
- `sheetName` is optional and falls back to the first worksheet when missing.
|
|
93
|
+
- `headerRow` is optional, 1-based, and defaults to the first non-empty row in the selected sheet.
|
|
94
|
+
- The result shape is `{ sheetName, headers, rows }`.
|
|
95
|
+
- Dynamic import requires non-empty, unique header names in the selected header row.
|
|
96
|
+
|
|
97
|
+
### Stable column fields
|
|
98
|
+
|
|
99
|
+
- `key` - unique programmatic field key
|
|
100
|
+
- `name` - visible worksheet header label
|
|
101
|
+
- `width` - exported column width
|
|
102
|
+
- `note` - header note/comment shown in Excel
|
|
103
|
+
- `dataType` - scalar cell type
|
|
104
|
+
- `allowedValues` - validation list for allowed cell values
|
|
105
|
+
- `parent` - parent header key for multi-row headers
|
|
106
|
+
- `format` - base cell format for the column
|
|
107
|
+
- `valueFormat` - direct or conditional format overrides for exported values
|
|
108
|
+
- `dataGroup` - logical group identifier for nested export data
|
|
109
|
+
- `dataGroupParent` - parent group identifier for nested export data
|
|
110
|
+
|
|
111
|
+
### Supported `dataType` values
|
|
112
|
+
|
|
113
|
+
- `text` - default text cells
|
|
114
|
+
- `number` - finite numeric values
|
|
115
|
+
- `date` - `Date` instances or parseable date strings on export
|
|
116
|
+
- `image` - image URLs or identifiers resolved through `imageFetcher`
|
|
117
|
+
|
|
118
|
+
## Import/export flow
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { downloadExcelTemplate, exportExcel, importExcel } from '@senlinz/import-export';
|
|
122
|
+
|
|
123
|
+
const definition = {
|
|
124
|
+
name: 'TomAndJerry',
|
|
125
|
+
sheetName: 'sheet1',
|
|
126
|
+
columns: [
|
|
127
|
+
{ key: 'name', name: 'Name', dataType: 'text', width: 20, note: 'required' },
|
|
128
|
+
{ key: 'age', name: 'Age', dataType: 'number', width: 10 },
|
|
129
|
+
{ key: 'birthday', name: 'Birthday', dataType: 'date', width: 18 },
|
|
130
|
+
{ key: 'category', name: 'Category', allowedValues: ['Cat', 'Mouse'] },
|
|
131
|
+
{ key: 'image', name: 'Image', dataType: 'image', width: 10 },
|
|
132
|
+
],
|
|
133
|
+
imageFetcher: async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer()),
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
await downloadExcelTemplate(definition);
|
|
137
|
+
|
|
138
|
+
await exportExcel(definition, [
|
|
139
|
+
{ name: 'Tom', age: 12, birthday: '2024-11-01 00:00:00', category: 'Cat', image: '/Tom.jpg' },
|
|
140
|
+
{ name: 'Jerry', age: null, birthday: null, category: 'Mouse', image: '/Jerry.png' },
|
|
141
|
+
]);
|
|
142
|
+
|
|
143
|
+
const rows = await importExcel(definition);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Import behavior
|
|
147
|
+
|
|
148
|
+
- Headers must match the configured `columns[].name` values exactly.
|
|
149
|
+
- Imported `number` columns are returned as numbers.
|
|
150
|
+
- Empty imported `number` and `date` cells are returned as `null`.
|
|
151
|
+
- Imported `date` values are returned as formatted strings.
|
|
152
|
+
- Unknown or malformed schemas fail fast before any workbook operation starts.
|
|
153
|
+
- Use `importExcelDynamic(...)` for browser uploads without a schema, or `fromExcelDynamic(...)` when you already have workbook bytes.
|
|
154
|
+
|
|
155
|
+
## Export behavior
|
|
156
|
+
|
|
157
|
+
- `null` and `undefined` values are exported as blank cells.
|
|
158
|
+
- Number columns reject non-finite values.
|
|
159
|
+
- Date columns accept `Date` instances or parseable date strings.
|
|
160
|
+
- Grouped export objects must use `{ value?, children: [...] }` for configured `dataGroup` columns.
|
|
161
|
+
- Image columns require `imageFetcher`.
|
|
162
|
+
|
|
163
|
+
## Advanced supported features
|
|
164
|
+
|
|
165
|
+
These advanced features are supported and considered part of the public API:
|
|
166
|
+
|
|
167
|
+
- merged title rows
|
|
168
|
+
- frozen headers
|
|
169
|
+
- column notes
|
|
170
|
+
- allowed value validation lists
|
|
171
|
+
- conditional `valueFormat`
|
|
172
|
+
- nested grouped export data via `dataGroup` and `dataGroupParent`
|
|
173
|
+
- image export via `imageFetcher`
|
|
174
|
+
|
|
175
|
+
## Browser/runtime support
|
|
176
|
+
|
|
177
|
+
- Browser ESM runtimes are the primary target.
|
|
178
|
+
- `downloadExcelTemplate`, `exportExcel`, `importExcel`, and `importExcelDynamic` require DOM/browser APIs.
|
|
179
|
+
- `fromExcel`, `toExcel`, and `generateExcelTemplate` can be used in other runtimes when browser-compatible globals are available.
|
|
180
|
+
- `fromExcelDynamic` can also be used in other runtimes when browser-compatible globals are available.
|
|
181
|
+
- `initializeWasm` can be used to pre-initialize the runtime with caller-managed `source`, `bytes`, or `module`.
|
|
182
|
+
|
|
183
|
+
## Examples
|
|
184
|
+
|
|
185
|
+
- [Basic browser example](./examples/basic-browser.html)
|
|
186
|
+
- [Manual WASM browser example](./examples/manual-wasm-browser.html)
|
|
187
|
+
- [Grouped export example](./examples/grouped-export.html)
|
|
188
|
+
- [Definition validation example](./examples/definition-errors.html)
|
|
189
|
+
|
|
190
|
+
These examples are covered by Playwright tests in [`./tests/import-export.spec.ts`](./tests/import-export.spec.ts).
|
|
191
|
+
|
|
192
|
+
## Known limitations
|
|
193
|
+
|
|
194
|
+
- Parent headers and data groups must be declared before dependent columns.
|
|
195
|
+
- Date imports normalize to strings instead of `Date` instances.
|
|
196
|
+
- Import currently validates a single worksheet at a time.
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# @senlinz/import-export
|
|
2
|
+
|
|
3
|
+
面向浏览器的高阶 Excel 模板生成、导出与导入 API。
|
|
4
|
+
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @senlinz/import-export
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 选择使用模式
|
|
14
|
+
|
|
15
|
+
### 默认模式(推荐)
|
|
16
|
+
|
|
17
|
+
- 无需额外初始化。
|
|
18
|
+
- `importExcel`、`importExcelDynamic`、`exportExcel`、`fromExcel`、`fromExcelDynamic`、`toExcel`、`downloadExcelTemplate`、`generateExcelTemplate` 会自动初始化内置 WASM 运行时。
|
|
19
|
+
- 适合绝大多数浏览器场景。
|
|
20
|
+
|
|
21
|
+
### 高级模式
|
|
22
|
+
|
|
23
|
+
- 在使用这些 Excel API 之前,先手动调用 `initializeWasm(...)`。
|
|
24
|
+
- 适用于自定义 WASM 托管、需要自己控制 bundler 行为,或希望复用已加载 bytes / module 的场景。
|
|
25
|
+
- 支持的手动输入:`source`、`bytes`、`module`。
|
|
26
|
+
- 同时也导出了 `bundledWasmSource`,方便在自包含 demo / 测试中显式走手动初始化流程。
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { initializeWasm, toExcel } from '@senlinz/import-export';
|
|
30
|
+
|
|
31
|
+
const wasmBytes = new Uint8Array(
|
|
32
|
+
await (await fetch('/assets/imexport_wasm_bg.wasm')).arrayBuffer()
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
initializeWasm({ bytes: wasmBytes });
|
|
36
|
+
|
|
37
|
+
const workbook = await toExcel({
|
|
38
|
+
name: 'TomAndJerry',
|
|
39
|
+
columns: [{ key: 'name', name: 'Name', dataType: 'text' }],
|
|
40
|
+
}, [{ name: 'Tom' }]);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
如果手动传入的 WASM 输入无效,`initializeWasm(...)` 会立即抛出清晰的错误信息。
|
|
44
|
+
|
|
45
|
+
## 支持的 API
|
|
46
|
+
|
|
47
|
+
### 稳定的 definition 字段
|
|
48
|
+
|
|
49
|
+
- `name`:模板 / 导出下载时使用的文件名
|
|
50
|
+
- `sheetName`:导出时使用、导入时优先匹配的工作表名称
|
|
51
|
+
- `columns`:表头、校验和行映射使用的稳定列定义
|
|
52
|
+
- `author`:可选的工作簿作者元数据
|
|
53
|
+
- `createTime`:可选的工作簿创建时间,支持 `Date` 或日期字符串
|
|
54
|
+
- `title`、`titleHeight`、`titleFormat`:可选的合并标题行及其高度、样式
|
|
55
|
+
- `defaultRowHeight`、`headerRowHeight`:导出时数据行和表头行的行高
|
|
56
|
+
- `dx`、`dy`:表头写入前的横向、纵向偏移量
|
|
57
|
+
- `isHeaderFreeze`:冻结表头区域,滚动时仍保持列标题可见
|
|
58
|
+
- `progressCallback`:长时间导入 / 导出过程中的进度回调
|
|
59
|
+
- `imageFetcher`:导出 `image` 列时必需的图片数据解析回调
|
|
60
|
+
|
|
61
|
+
### 稳定的列字段
|
|
62
|
+
|
|
63
|
+
- `key`:唯一的程序字段名
|
|
64
|
+
- `name`:工作表中显示的列表头
|
|
65
|
+
- `width`:导出列宽
|
|
66
|
+
- `note`:附加在表头单元格上的备注 / 注释
|
|
67
|
+
- `dataType`:标量单元格类型
|
|
68
|
+
- `allowedValues`:单元格允许值的校验列表
|
|
69
|
+
- `parent`:多级表头时的父级表头 key
|
|
70
|
+
- `format`:列的基础单元格格式
|
|
71
|
+
- `valueFormat`:导出值的直接或条件格式覆盖
|
|
72
|
+
- `dataGroup`:嵌套导出数据使用的逻辑分组标识
|
|
73
|
+
- `dataGroupParent`:嵌套导出数据使用的父级分组标识
|
|
74
|
+
|
|
75
|
+
### 支持的 `dataType`
|
|
76
|
+
|
|
77
|
+
- `text`:默认文本类型
|
|
78
|
+
- `number`:有限数值
|
|
79
|
+
- `date`:导出时支持 `Date` 实例或日期字符串
|
|
80
|
+
- `image`:通过 `imageFetcher` 解析图片 URL 或标识
|
|
81
|
+
|
|
82
|
+
## 导入 / 导出流程
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { downloadExcelTemplate, exportExcel, importExcel } from '@senlinz/import-export';
|
|
86
|
+
|
|
87
|
+
const definition = {
|
|
88
|
+
name: 'TomAndJerry',
|
|
89
|
+
sheetName: 'sheet1',
|
|
90
|
+
columns: [
|
|
91
|
+
{ key: 'name', name: 'Name', dataType: 'text', width: 20, note: 'required' },
|
|
92
|
+
{ key: 'age', name: 'Age', dataType: 'number', width: 10 },
|
|
93
|
+
{ key: 'birthday', name: 'Birthday', dataType: 'date', width: 18 },
|
|
94
|
+
{ key: 'category', name: 'Category', allowedValues: ['Cat', 'Mouse'] },
|
|
95
|
+
{ key: 'image', name: 'Image', dataType: 'image', width: 10 },
|
|
96
|
+
],
|
|
97
|
+
imageFetcher: async (url: string) => new Uint8Array(await (await fetch(url)).arrayBuffer()),
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
await downloadExcelTemplate(definition);
|
|
101
|
+
|
|
102
|
+
await exportExcel(definition, [
|
|
103
|
+
{ name: 'Tom', age: 12, birthday: '2024-11-01 00:00:00', category: 'Cat', image: '/Tom.jpg' },
|
|
104
|
+
{ name: 'Jerry', age: null, birthday: null, category: 'Mouse', image: '/Jerry.png' },
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
const rows = await importExcel(definition);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## 动态导入
|
|
111
|
+
|
|
112
|
+
当你不想预先定义 `columns`,并希望直接使用浏览器文件选择框时,可使用 `importExcelDynamic(options?)`。
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { importExcelDynamic } from '@senlinz/import-export';
|
|
116
|
+
|
|
117
|
+
const result = await importExcelDynamic({
|
|
118
|
+
sheetName: 'sheet1',
|
|
119
|
+
headerRow: 1,
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
当你已经拿到工作簿字节数据时,可继续使用 `fromExcelDynamic(buffer, options?)`。
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { fromExcelDynamic } from '@senlinz/import-export';
|
|
127
|
+
|
|
128
|
+
const result = await fromExcelDynamic(fileBytes, {
|
|
129
|
+
sheetName: 'sheet1',
|
|
130
|
+
headerRow: 1,
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
- `importExcelDynamic(...)` 依赖 DOM / 浏览器文件上传 API。
|
|
135
|
+
- `sheetName` 可选,未提供或不存在时会回退到第一个工作表。
|
|
136
|
+
- `headerRow` 可选,使用从 `1` 开始的行号,默认会自动选择首个非空行。
|
|
137
|
+
- 返回值结构为 `{ sheetName, headers, rows }`。
|
|
138
|
+
|
|
139
|
+
## 导入行为
|
|
140
|
+
|
|
141
|
+
- 表头必须与 `columns[].name` 完全一致。
|
|
142
|
+
- `number` 列导入后返回数字。
|
|
143
|
+
- 空的 `number` / `date` 单元格导入后返回 `null`。
|
|
144
|
+
- `date` 列导入后返回格式化字符串。
|
|
145
|
+
- 非法或不完整的 schema 会在读取工作簿前立即失败。
|
|
146
|
+
- 无 schema 导入时,可使用 `importExcelDynamic(...)`(浏览器上传)或 `fromExcelDynamic(...)`(字节缓冲)。
|
|
147
|
+
|
|
148
|
+
## 导出行为
|
|
149
|
+
|
|
150
|
+
- `null` 和 `undefined` 会导出为空单元格。
|
|
151
|
+
- 数字列会拒绝非有限值。
|
|
152
|
+
- 日期列支持 `Date` 实例或日期字符串。
|
|
153
|
+
- 分组导出对象必须使用 `{ value?, children: [...] }` 结构。
|
|
154
|
+
- 图片列必须提供 `imageFetcher`。
|
|
155
|
+
|
|
156
|
+
## 已支持的高级能力
|
|
157
|
+
|
|
158
|
+
以下高级能力已纳入公开 API:
|
|
159
|
+
|
|
160
|
+
- 合并标题行
|
|
161
|
+
- 冻结表头
|
|
162
|
+
- 列备注
|
|
163
|
+
- 下拉候选值
|
|
164
|
+
- 条件 `valueFormat`
|
|
165
|
+
- 使用 `dataGroup` / `dataGroupParent` 的嵌套分组导出
|
|
166
|
+
- 通过 `imageFetcher` 导出图片
|
|
167
|
+
|
|
168
|
+
## 浏览器 / 运行时支持
|
|
169
|
+
|
|
170
|
+
- 主要面向浏览器 ESM 运行时。
|
|
171
|
+
- `downloadExcelTemplate`、`exportExcel`、`importExcel`、`importExcelDynamic` 依赖 DOM / 浏览器 API。
|
|
172
|
+
- 当运行时提供兼容的浏览器全局对象时,也可以使用 `fromExcel`、`fromExcelDynamic`、`toExcel`、`generateExcelTemplate`。
|
|
173
|
+
- `initializeWasm` 可用于使用调用方提供的 `source`、`bytes` 或 `module` 预初始化运行时。
|
|
174
|
+
|
|
175
|
+
## 示例
|
|
176
|
+
|
|
177
|
+
- [基础浏览器示例](./examples/basic-browser.html)
|
|
178
|
+
- [手动初始化 WASM 的浏览器示例](./examples/manual-wasm-browser.html)
|
|
179
|
+
- [分组导出示例](./examples/grouped-export.html)
|
|
180
|
+
- [Definition 校验示例](./examples/definition-errors.html)
|
|
181
|
+
|
|
182
|
+
这些示例由 [`./tests/import-export.spec.ts`](./tests/import-export.spec.ts) 中的 Playwright 用例覆盖。
|
|
183
|
+
|
|
184
|
+
## 已知限制
|
|
185
|
+
|
|
186
|
+
- 父级表头和数据分组必须先于依赖它们的列声明。
|
|
187
|
+
- `date` 导入结果会规范化为字符串,而不是 `Date` 实例。
|
|
188
|
+
- 当前一次只校验一个工作表。
|