antd-import-export 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +181 -2
- package/README_EN.md +129 -0
- package/dist/components/BatchExportButton.d.ts +10 -0
- package/dist/components/ExcelExportButton.d.ts +15 -0
- package/dist/components/ExcelImportButton.d.ts +11 -0
- package/dist/components/ExportTaskModal.d.ts +7 -0
- package/dist/components/ImportCheckContent.d.ts +10 -0
- package/dist/components/ImportPreviewModal.d.ts +19 -0
- package/dist/core/excel/importEditor.d.ts +7 -0
- package/dist/core/excel/importRow.d.ts +14 -0
- package/dist/core/excel/parseSheet.d.ts +18 -0
- package/dist/core/excel/runtime.d.ts +11 -0
- package/dist/core/excel/worker-url.js +8 -0
- package/dist/core/excel/worker.d.ts +12 -0
- package/dist/core/excel/worker.mjs +119 -0
- package/dist/core/excel/worksheet.d.ts +13 -0
- package/dist/core/httpError.d.ts +6 -0
- package/dist/core/limits.d.ts +5 -0
- package/dist/core/schema/index.d.ts +88 -0
- package/dist/core/schema/multiValue.d.ts +4 -0
- package/dist/core/schema/validate.d.ts +21 -0
- package/dist/core/taskManager.d.ts +123 -0
- package/dist/core/types.d.ts +16 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.mjs +1598 -0
- package/dist/react/useDebouncedValue.d.ts +2 -0
- package/dist/react/useTaskDetail.d.ts +2 -0
- package/dist/react/useThrottledValue.d.ts +2 -0
- package/dist/worksheet-BEUkpIL-.js +467 -0
- package/package.json +99 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Czw96
|
|
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,3 +1,182 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<h1 align="center">antd-import-export</h1>
|
|
3
|
+
<p align="center">基于 Ant Design 的 Excel 导入导出套件,用一份字段定义驱动模板、校验、提交与导出。</p>
|
|
4
|
+
</p>
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
<p align="center">
|
|
7
|
+
<a href="https://www.npmjs.com/package/antd-import-export"><img src="https://img.shields.io/npm/v/antd-import-export" alt="npm version" /></a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/antd-import-export"><img src="https://img.shields.io/npm/dm/antd-import-export" alt="npm downloads" /></a>
|
|
9
|
+
<a href="https://github.com/Czw96/antd-import-export/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/antd-import-export" alt="license" /></a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
中文 | [English](./README_EN.md)
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
[在线演示](https://demo.29dev.cn/antd-import-export/)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## 依赖要求
|
|
20
|
+
|
|
21
|
+
| 依赖 | 版本 |
|
|
22
|
+
| ----------------- | -------- |
|
|
23
|
+
| react | >=18.2.0 |
|
|
24
|
+
| react-dom | >=18.2.0 |
|
|
25
|
+
| antd | >=6.0.0 |
|
|
26
|
+
| @ant-design/icons | >=5.0.0 |
|
|
27
|
+
|
|
28
|
+
## 字段定义
|
|
29
|
+
|
|
30
|
+
导入导出的一切都由 `FieldSchema[]` 驱动:导出用它生成表头,导入模板用它写填写要求,解析时用它逐格校验。
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import type { FieldSchema } from "antd-import-export";
|
|
34
|
+
|
|
35
|
+
const fieldSchemas: FieldSchema[] = [
|
|
36
|
+
{
|
|
37
|
+
key: "number",
|
|
38
|
+
title: "编号",
|
|
39
|
+
type: "text",
|
|
40
|
+
property: { required: false, max_length: 20, default_value: null },
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
key: "name",
|
|
44
|
+
title: "名称",
|
|
45
|
+
type: "text",
|
|
46
|
+
property: { required: true, max_length: 60, default_value: null },
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
| type | property | 说明 |
|
|
52
|
+
| --- | --- | --- |
|
|
53
|
+
| `text` | required / max_length / default_value | 按字符数校验长度 |
|
|
54
|
+
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | 精度按十进制位数判断,不受浮点误差影响 |
|
|
55
|
+
| `boolean` | required / true_label / false_label / default_value | 单元格填文案,接口值转成布尔 |
|
|
56
|
+
| `date` | required / default_value | 严格 `YYYY-MM-DD` |
|
|
57
|
+
| `time` | required / default_value | 严格 `HH:mm`,输出补零 |
|
|
58
|
+
| `datetime` | required / default_value | 严格 `YYYY-MM-DD HH:mm` |
|
|
59
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | 逗号分号均可,导出统一用英文分号 |
|
|
60
|
+
| `single_choice` | required / option_map / default_value | 单元格填文案,接口值取 option_map 里的值 |
|
|
61
|
+
| `multiple_choice` | required / option_map / default_value | 多值用中英文分号分隔 |
|
|
62
|
+
|
|
63
|
+
`importable: false` 的列只出现在导出文件里,不参与导入模板和解析。
|
|
64
|
+
|
|
65
|
+
## 逐字段校验
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { validateField } from "antd-import-export";
|
|
69
|
+
|
|
70
|
+
const result = validateField("19.99", {
|
|
71
|
+
key: "price",
|
|
72
|
+
title: "价格",
|
|
73
|
+
type: "number",
|
|
74
|
+
property: { required: false, precision: 2 },
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// { validateSuccess: true, validateValue: 19.99 }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 组件
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import { ExcelImportButton, ExcelExportButton, BatchExportButton } from "antd-import-export";
|
|
84
|
+
|
|
85
|
+
// 导入: 内置模板下载、文件解析、预览编辑、分批提交、行级错误回填
|
|
86
|
+
<ExcelImportButton
|
|
87
|
+
title="供应商"
|
|
88
|
+
fieldSchemas={dataSchema}
|
|
89
|
+
importRows={(payloads, signal) => supplierImportData(payloads, signal)}
|
|
90
|
+
onImported={onRefresh}
|
|
91
|
+
/>;
|
|
92
|
+
|
|
93
|
+
// 按当前筛选条件导出, 内部用游标分批拉取
|
|
94
|
+
<ExcelExportButton
|
|
95
|
+
title="供应商"
|
|
96
|
+
fieldSchemas={dataSchema}
|
|
97
|
+
filters={queryForm}
|
|
98
|
+
totalCount={dataCount}
|
|
99
|
+
fetchPage={(params, signal) => supplierExportData(params, signal)}
|
|
100
|
+
/>;
|
|
101
|
+
|
|
102
|
+
// 导出已勾选的行
|
|
103
|
+
<BatchExportButton title="供应商" fieldSchemas={dataSchema} selectedRows={selectedRows} />;
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
三个组件都不发请求,只调用你传进来的 `importRows` / `fetchPage`。
|
|
107
|
+
|
|
108
|
+
### ExcelImportButton
|
|
109
|
+
|
|
110
|
+
| prop | 类型 | 说明 |
|
|
111
|
+
| --- | --- | --- |
|
|
112
|
+
| `title` | `string` | 按钮、模板文件名、任务标题共用 |
|
|
113
|
+
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
114
|
+
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | 一批行提交,返回等长结果;**空对象表示成功** |
|
|
115
|
+
| `onImported` | `() => void` | 任务结束后回调,通常用来刷新列表 |
|
|
116
|
+
|
|
117
|
+
### ExcelExportButton
|
|
118
|
+
|
|
119
|
+
| prop | 类型 | 说明 |
|
|
120
|
+
| --- | --- | --- |
|
|
121
|
+
| `title` | `string` | 文件名与任务标题 |
|
|
122
|
+
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
123
|
+
| `filters` | `Record<string, unknown>` | 并入每批请求参数 |
|
|
124
|
+
| `totalCount` | `number` | 用于条数上限校验与进度总数 |
|
|
125
|
+
| `fetchPage` | `(params, signal) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id`** |
|
|
126
|
+
| `buttonText` | `string` | 默认「导出」 |
|
|
127
|
+
|
|
128
|
+
### BatchExportButton
|
|
129
|
+
|
|
130
|
+
| prop | 类型 | 说明 |
|
|
131
|
+
| --- | --- | --- |
|
|
132
|
+
| `title` | `string` | 文件名 |
|
|
133
|
+
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
134
|
+
| `selectedRows` | `object[]` | 已勾选的行 |
|
|
135
|
+
| `disabled` | `boolean` | 可选 |
|
|
136
|
+
|
|
137
|
+
### 导入结果约定
|
|
138
|
+
|
|
139
|
+
后端逐行返回数组,顺序与请求一致:
|
|
140
|
+
|
|
141
|
+
| 返回 | 含义 |
|
|
142
|
+
| --- | --- |
|
|
143
|
+
| `{}` | 该行成功 |
|
|
144
|
+
| `{ "name": ["不能为空"] }` | 该行字段级失败,回填到预览表格 |
|
|
145
|
+
| `{ "detail": "名称已存在" }` | 该行整体失败 |
|
|
146
|
+
| HTTP 4xx/5xx、网络异常 | 暂停整个任务,可继续或取消 |
|
|
147
|
+
|
|
148
|
+
### 两个使用前提
|
|
149
|
+
|
|
150
|
+
1. 组件用 `App.useApp()` 弹提示,根节点需要包一层 antd 的 `<App>`。
|
|
151
|
+
2. 在 Next.js App Router 里使用时,引入这些组件的文件要加 `"use client"`。
|
|
152
|
+
|
|
153
|
+
## Excel 读写
|
|
154
|
+
|
|
155
|
+
ExcelJS 只在 Worker 里加载,主线程不承担解析和生成。组件接入后这一步由组件内部完成,也可以直接用:
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
159
|
+
|
|
160
|
+
const result = await parseImportFile(file, fieldSchemas);
|
|
161
|
+
// { headerMessages, formulaMessages, rows, includedKeys }
|
|
162
|
+
|
|
163
|
+
const blob = await buildExportBlob(rows, fieldSchemas);
|
|
164
|
+
await downloadImportTemplate(fieldSchemas, "供应商导入模板");
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## 模块格式
|
|
168
|
+
|
|
169
|
+
只提供 ESM。Worker 依赖 `import.meta.url` 定位自身产物,CJS 里没有这个能力。
|
|
170
|
+
|
|
171
|
+
## 本地开发
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
npm install
|
|
175
|
+
npm run build # vite 构建产物 + tsc 生成类型
|
|
176
|
+
npm run preview # 启动示例页
|
|
177
|
+
npm run test # vitest
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 许可证
|
|
181
|
+
|
|
182
|
+
MIT
|
package/README_EN.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<h1 align="center">antd-import-export</h1>
|
|
3
|
+
<p align="center">Schema-driven Excel import and export for Ant Design.</p>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<p align="center">
|
|
7
|
+
<a href="https://www.npmjs.com/package/antd-import-export"><img src="https://img.shields.io/npm/v/antd-import-export" alt="npm version" /></a>
|
|
8
|
+
<a href="https://github.com/Czw96/antd-import-export/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/antd-import-export" alt="license" /></a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
[中文](./README.md) | English
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
[Live demo](https://demo.29dev.cn/antd-import-export/)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## Requirements
|
|
19
|
+
|
|
20
|
+
| Package | Version |
|
|
21
|
+
| ----------------- | -------- |
|
|
22
|
+
| react | >=18.2.0 |
|
|
23
|
+
| react-dom | >=18.2.0 |
|
|
24
|
+
| antd | >=6.0.0 |
|
|
25
|
+
| @ant-design/icons | >=5.0.0 |
|
|
26
|
+
|
|
27
|
+
## Field schema
|
|
28
|
+
|
|
29
|
+
Everything is driven by `FieldSchema[]`: export headers, template hints and cell-level validation.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import type { FieldSchema } from "antd-import-export";
|
|
33
|
+
|
|
34
|
+
const fieldSchemas: FieldSchema[] = [
|
|
35
|
+
{
|
|
36
|
+
key: "name",
|
|
37
|
+
title: "Name",
|
|
38
|
+
type: "text",
|
|
39
|
+
property: { required: true, max_length: 60, default_value: null },
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
| type | property | notes |
|
|
45
|
+
| --- | --- | --- |
|
|
46
|
+
| `text` | required / max_length / default_value | length checked in characters |
|
|
47
|
+
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | precision judged on decimal places |
|
|
48
|
+
| `boolean` | required / true_label / false_label / default_value | labels in the cell, boolean on the wire |
|
|
49
|
+
| `date` | required / default_value | strict `YYYY-MM-DD` |
|
|
50
|
+
| `time` | required / default_value | strict `HH:mm` |
|
|
51
|
+
| `datetime` | required / default_value | strict `YYYY-MM-DD HH:mm` |
|
|
52
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | comma or semicolon separated on import |
|
|
53
|
+
| `single_choice` | required / option_map / default_value | label in the cell, mapped value on the wire |
|
|
54
|
+
| `multiple_choice` | required / option_map / default_value | multiple values separated by semicolons |
|
|
55
|
+
|
|
56
|
+
Columns marked `importable: false` only appear in exported files.
|
|
57
|
+
|
|
58
|
+
## Validation
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { validateField } from "antd-import-export";
|
|
62
|
+
|
|
63
|
+
validateField("19.99", {
|
|
64
|
+
key: "price",
|
|
65
|
+
title: "Price",
|
|
66
|
+
type: "number",
|
|
67
|
+
property: { required: false, precision: 2 },
|
|
68
|
+
});
|
|
69
|
+
// { validateSuccess: true, validateValue: 19.99 }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Components
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { ExcelImportButton, ExcelExportButton, BatchExportButton } from "antd-import-export";
|
|
76
|
+
|
|
77
|
+
<ExcelImportButton
|
|
78
|
+
title="Supplier"
|
|
79
|
+
fieldSchemas={fieldSchemas}
|
|
80
|
+
importRows={(payloads, signal) => importSuppliers(payloads, signal)}
|
|
81
|
+
onImported={refresh}
|
|
82
|
+
/>;
|
|
83
|
+
|
|
84
|
+
<ExcelExportButton
|
|
85
|
+
title="Supplier"
|
|
86
|
+
fieldSchemas={fieldSchemas}
|
|
87
|
+
filters={queryForm}
|
|
88
|
+
totalCount={totalCount}
|
|
89
|
+
fetchPage={(params, signal) => exportSuppliers(params, signal)}
|
|
90
|
+
/>;
|
|
91
|
+
|
|
92
|
+
<BatchExportButton title="Supplier" fieldSchemas={fieldSchemas} selectedRows={selectedRows} />;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The components never issue requests themselves: they call the `importRows` / `fetchPage` you provide.
|
|
96
|
+
|
|
97
|
+
### Two prerequisites
|
|
98
|
+
|
|
99
|
+
1. They use `App.useApp()`, so wrap the root with antd's `<App>`.
|
|
100
|
+
2. In Next.js App Router, add `"use client"` to the file that imports them.
|
|
101
|
+
|
|
102
|
+
## Excel I/O
|
|
103
|
+
|
|
104
|
+
ExcelJS only loads inside a Worker.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
108
|
+
|
|
109
|
+
const result = await parseImportFile(file, fieldSchemas);
|
|
110
|
+
const blob = await buildExportBlob(rows, fieldSchemas);
|
|
111
|
+
await downloadImportTemplate(fieldSchemas, "template");
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Module format
|
|
115
|
+
|
|
116
|
+
ESM only: the worker locates itself through `import.meta.url`, which CommonJS does not provide.
|
|
117
|
+
|
|
118
|
+
## Development
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
npm install
|
|
122
|
+
npm run build
|
|
123
|
+
npm run preview
|
|
124
|
+
npm run test
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { FieldSchema } from "../core/schema";
|
|
3
|
+
export interface BatchExportButtonProps {
|
|
4
|
+
title: string;
|
|
5
|
+
fieldSchemas: FieldSchema[];
|
|
6
|
+
selectedRows: object[];
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
}
|
|
9
|
+
declare const BatchExportButton: React.FC<BatchExportButtonProps>;
|
|
10
|
+
export default BatchExportButton;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { FieldSchema } from "../core/schema";
|
|
3
|
+
import type { PagedData } from "../core/types";
|
|
4
|
+
export interface ExcelExportButtonProps {
|
|
5
|
+
title: string;
|
|
6
|
+
buttonText?: string;
|
|
7
|
+
fieldSchemas: FieldSchema[];
|
|
8
|
+
filters: Record<string, unknown>;
|
|
9
|
+
/** 当前列表按同样筛选条件查出的总条数 */
|
|
10
|
+
totalCount: number;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
fetchPage: (params: Record<string, unknown>, signal: AbortSignal) => Promise<PagedData<object>>;
|
|
13
|
+
}
|
|
14
|
+
declare const ExcelExportButton: React.FC<ExcelExportButtonProps>;
|
|
15
|
+
export default ExcelExportButton;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRowErrorBody } from "../core/types";
|
|
3
|
+
import type { FieldSchema } from "../core/schema";
|
|
4
|
+
export interface ExcelImportButtonProps {
|
|
5
|
+
title: string;
|
|
6
|
+
fieldSchemas: FieldSchema[];
|
|
7
|
+
importRows: (rows: Record<string, unknown>[], signal: AbortSignal) => Promise<ImportRowErrorBody[]>;
|
|
8
|
+
onImported: () => void;
|
|
9
|
+
}
|
|
10
|
+
declare const ExcelImportButton: React.FC<ExcelImportButtonProps>;
|
|
11
|
+
export default ExcelImportButton;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export interface ImportCheckMessages {
|
|
3
|
+
/** 没有对上的必填列名 */
|
|
4
|
+
missingTitles: string[];
|
|
5
|
+
/** 表头级问题: 文件打不开、没有工作表等 */
|
|
6
|
+
headerMessages: string[];
|
|
7
|
+
}
|
|
8
|
+
/** 导入校验的明细内容, 使用普通正文样式, 不再叠加图标和强调色 */
|
|
9
|
+
declare const ImportCheckContent: React.FC<ImportCheckMessages>;
|
|
10
|
+
export default ImportCheckContent;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { ImportRow } from "../core/excel/importRow";
|
|
3
|
+
import type { FieldSchema } from "../core/schema";
|
|
4
|
+
interface ImportPreviewModalProps {
|
|
5
|
+
open: boolean;
|
|
6
|
+
title: string;
|
|
7
|
+
fieldSchemas: FieldSchema[];
|
|
8
|
+
rows: ImportRow[];
|
|
9
|
+
taskId?: string;
|
|
10
|
+
/** 文件里实际存在的列; 预览只展示这些列, 缺列不渲染也不提交 */
|
|
11
|
+
includedKeys: string[];
|
|
12
|
+
onChangeCell: (row: ImportRow, field: FieldSchema, value: unknown) => void;
|
|
13
|
+
onSaveCell: (row: ImportRow, field: FieldSchema, value: unknown) => void;
|
|
14
|
+
onDelete: (row: ImportRow) => void;
|
|
15
|
+
onConfirm: () => void;
|
|
16
|
+
onClose: () => void;
|
|
17
|
+
}
|
|
18
|
+
declare const ImportPreviewModal: React.FC<ImportPreviewModalProps>;
|
|
19
|
+
export default ImportPreviewModal;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FieldSchema } from "../schema";
|
|
2
|
+
/** 接口值 -> 表格/表单里的展示值 */
|
|
3
|
+
export declare function toEditorValue(field: FieldSchema, value: unknown): string | number | (string | undefined)[] | undefined;
|
|
4
|
+
/** 原始单元格文本 -> 编辑器值; 只用于校验失败、尚未规范化的字段 */
|
|
5
|
+
export declare function toRawEditorValue(field: FieldSchema, value: string | null): string | string[];
|
|
6
|
+
/** 表格/表单里的展示值 -> 交给 validateField 的文本 */
|
|
7
|
+
export declare function toEditorText(field: FieldSchema, value: unknown): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { FieldSchema } from "../schema";
|
|
2
|
+
/**
|
|
3
|
+
* 导入行唯一数据源: values 保存用户在表格里看到/编辑的值, errors 保存当前校验错误.
|
|
4
|
+
* 提交给接口的 payload 在真正导入时由 values 派生, 不再另外维护一份 payload.
|
|
5
|
+
*/
|
|
6
|
+
export interface ImportRow {
|
|
7
|
+
key: string;
|
|
8
|
+
rowNumber: number;
|
|
9
|
+
values: Record<string, unknown>;
|
|
10
|
+
errors: Record<string, string>;
|
|
11
|
+
taskIndex?: number;
|
|
12
|
+
}
|
|
13
|
+
/** 从唯一数据源生成接口 payload; 缺列字段跳过, 保持"不修改"语义 */
|
|
14
|
+
export declare function buildImportPayload(row: ImportRow, fieldSchemas: FieldSchema[], includedKeys: string[]): Record<string, unknown>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import ExcelJS from "exceljs";
|
|
2
|
+
import type { FieldSchema } from "../schema";
|
|
3
|
+
import type { ImportRow } from "./importRow";
|
|
4
|
+
export interface ImportParseResult {
|
|
5
|
+
/** 表头级问题: 缺必填列等, 出现即整份文件不可导入 */
|
|
6
|
+
headerMessages: string[];
|
|
7
|
+
/** 公式没有缓存结果: 无法取值, 需要用户用 Excel 打开并保存 */
|
|
8
|
+
formulaMessages: string[];
|
|
9
|
+
/** 解析后的唯一数据源;表格编辑、提交 payload、任务快照都使用这一份 */
|
|
10
|
+
rows: ImportRow[];
|
|
11
|
+
/**
|
|
12
|
+
* 文件里实际存在的列(fieldSchemas.key)。
|
|
13
|
+
* 未包含的列不会提交给后端; 更新时表示不修改该字段, 预览里也不展示该列。
|
|
14
|
+
*/
|
|
15
|
+
includedKeys: string[];
|
|
16
|
+
}
|
|
17
|
+
/** 解析并校验一张工作表: 表头错整份拒绝, 行错写进对应行的 errors */
|
|
18
|
+
export declare function parseWorksheet(worksheet: ExcelJS.Worksheet, fieldSchemas: FieldSchema[]): ImportParseResult;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ImportParseResult } from "./parseSheet";
|
|
2
|
+
import type { FieldSchema } from "../schema";
|
|
3
|
+
export { autoAdjustColumnWidths } from "./worksheet";
|
|
4
|
+
/** 导出: 在 Worker 里生成 xlsx, 返回 Blob, 由调用方决定何时下载 */
|
|
5
|
+
export declare function buildExportBlob(data: object[], fieldSchemas: FieldSchema[], signal?: AbortSignal): Promise<Blob>;
|
|
6
|
+
/** 导出: 把列表数据渲染成 Excel 并下载 */
|
|
7
|
+
export declare function exportExcel(data: object[], fieldSchemas: FieldSchema[], fileName: string): Promise<Blob>;
|
|
8
|
+
/** 导入模板: 列名与填写要求写在表头单元格里, 不写示例行 */
|
|
9
|
+
export declare function downloadImportTemplate(fieldSchemas: FieldSchema[], fileName: string): Promise<void>;
|
|
10
|
+
/** 导入: 在 Worker 里解析并校验 Excel, 不阻塞页面 */
|
|
11
|
+
export declare function parseImportFile(file: File, fieldSchemas: FieldSchema[]): Promise<ImportParseResult>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 这一行会原样拷贝进 dist, 不参与本包打包:
|
|
3
|
+
* 只有使用方的打包器看到这里的字面量, 才会把 worker 当作独立入口产出.
|
|
4
|
+
* 若由本包打包, vite 会把目标文件当成 worker 入口去解析, 而 worker.mjs 此时还不存在.
|
|
5
|
+
*/
|
|
6
|
+
export function createExcelWorker() {
|
|
7
|
+
return new Worker(new URL("./worker.mjs", import.meta.url), { type: "module" });
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ImportParseResult } from "./parseSheet";
|
|
2
|
+
import type { FieldSchema } from "../schema";
|
|
3
|
+
declare const workerAPI: {
|
|
4
|
+
/** 在 Worker 里生成 xlsx 文件内容(ExcelJS 只在 Worker 产物里, 不进主包) */
|
|
5
|
+
buildExportExcel(data: Record<string, unknown>[], fieldSchemas: FieldSchema[]): Promise<ArrayBuffer>;
|
|
6
|
+
/** 在 Worker 里生成导入模板: 列名 + 填写要求(都在表头单元格里), 不写示例行 */
|
|
7
|
+
buildImportTemplate(fieldSchemas: FieldSchema[]): Promise<ArrayBuffer>;
|
|
8
|
+
/** 在 Worker 里解析并按 fieldSchemas 校验 Excel, 返回可提交的数据与逐行问题 */
|
|
9
|
+
parseImportFile(file: File, fieldSchemas: FieldSchema[]): Promise<ImportParseResult>;
|
|
10
|
+
};
|
|
11
|
+
export type ExcelWorkerAPI = typeof workerAPI;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { a as e, l as t, n, o as r, p as i, r as a, x as o } from "../../worksheet-BEUkpIL-.js";
|
|
2
|
+
import * as s from "comlink";
|
|
3
|
+
import c from "exceljs";
|
|
4
|
+
// src/core/excel/parseSheet.ts
|
|
5
|
+
function l(e) {
|
|
6
|
+
return !Number.isFinite(e) || e < 1 || e >= 2958466 ? null : new Date(Math.round((e - 25569) * 24 * 3600 * 1e3));
|
|
7
|
+
}
|
|
8
|
+
function u(e, t = !1) {
|
|
9
|
+
let n = String(e.getUTCMonth() + 1).padStart(2, "0"), r = String(e.getUTCDate()).padStart(2, "0"), i = `${e.getUTCFullYear()}-${n}-${r}`;
|
|
10
|
+
return t ? `${i} ${String(e.getUTCHours()).padStart(2, "0")}:${String(e.getUTCMinutes()).padStart(2, "0")}` : i;
|
|
11
|
+
}
|
|
12
|
+
function d(e, t) {
|
|
13
|
+
let n = (e) => e.trim() === "" ? null : e.trim(), r = t === "datetime", i = t === "date" || r;
|
|
14
|
+
if (e.type === c.ValueType.Null) return null;
|
|
15
|
+
if (e.type === c.ValueType.Date) return u(e.value, r);
|
|
16
|
+
if (e.type === c.ValueType.Formula) {
|
|
17
|
+
if (e.result === null || e.result === void 0) return null;
|
|
18
|
+
if (e.result instanceof Date) return u(e.result, r);
|
|
19
|
+
if (i && typeof e.result == "number") {
|
|
20
|
+
let t = l(e.result);
|
|
21
|
+
if (t) return u(t, r);
|
|
22
|
+
}
|
|
23
|
+
return n(String(e.result));
|
|
24
|
+
}
|
|
25
|
+
if (e.type === c.ValueType.Number) {
|
|
26
|
+
if (i && typeof e.value == "number") {
|
|
27
|
+
let t = l(e.value);
|
|
28
|
+
if (t) return u(t, r);
|
|
29
|
+
}
|
|
30
|
+
return n(String(e.value));
|
|
31
|
+
}
|
|
32
|
+
return n(e.text);
|
|
33
|
+
}
|
|
34
|
+
function f(e) {
|
|
35
|
+
let t = e.property.default_value;
|
|
36
|
+
if (t == null) return null;
|
|
37
|
+
if (e.type === "boolean") return t ? e.property.true_label : e.property.false_label;
|
|
38
|
+
if (Array.isArray(t)) return t.length > 0 ? o(t) : null;
|
|
39
|
+
let n = String(t);
|
|
40
|
+
return n.trim() === "" ? null : n;
|
|
41
|
+
}
|
|
42
|
+
function p(e, t) {
|
|
43
|
+
let n = new Map(t.map((e) => [e.title, e])), r = [];
|
|
44
|
+
for (let t = 1; t <= e.columnCount; t++) {
|
|
45
|
+
let i = e.getRow(1).getCell(t).text.split("\n")[0].trim();
|
|
46
|
+
r[t - 1] = n.get(i) ?? null;
|
|
47
|
+
}
|
|
48
|
+
return r;
|
|
49
|
+
}
|
|
50
|
+
function m(n, a) {
|
|
51
|
+
let o = [], s = [], l = [], u = 0, m = !1, h = p(n, a), g = h.filter((e) => e !== null).map((e) => e.key), _ = new Set(g);
|
|
52
|
+
if (a.forEach((e) => {
|
|
53
|
+
e.property.required && !_.has(e.key) && o.push(`缺少列: ${e.title}`);
|
|
54
|
+
}), o.length > 0) return {
|
|
55
|
+
headerMessages: o,
|
|
56
|
+
formulaMessages: s,
|
|
57
|
+
rows: l,
|
|
58
|
+
includedKeys: g
|
|
59
|
+
};
|
|
60
|
+
let v = 0;
|
|
61
|
+
return n.eachRow({ includeEmpty: !1 }, (n, p) => {
|
|
62
|
+
if (p === 1) return;
|
|
63
|
+
let g = {};
|
|
64
|
+
if (h.forEach((e, t) => {
|
|
65
|
+
e && (g[e.key] = d(n.getCell(t + 1), e.type));
|
|
66
|
+
}), h.forEach((e, t) => {
|
|
67
|
+
if (!e) return;
|
|
68
|
+
let r = n.getCell(t + 1);
|
|
69
|
+
r.type === c.ValueType.Formula && (r.result === null || r.result === void 0) && (u += 1, s.length < 20 && s.push(`第 ${p} 行 [${e.title}] 是公式且没有保存计算结果, 请用 Excel 打开并保存后再导入`));
|
|
70
|
+
}), Object.values(g).every((e) => e === null)) return;
|
|
71
|
+
if (v += 1, v > 2e4) {
|
|
72
|
+
m ||= (o.push(`导入数据超出限制(${t} 条), 请分批导入`), !0);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
let y = {
|
|
76
|
+
key: String(l.length),
|
|
77
|
+
rowNumber: p,
|
|
78
|
+
values: {},
|
|
79
|
+
errors: {}
|
|
80
|
+
};
|
|
81
|
+
a.forEach((t) => {
|
|
82
|
+
if (!_.has(t.key)) return;
|
|
83
|
+
let n = g[t.key] ?? f(t), a = i(n, t);
|
|
84
|
+
a.validateSuccess ? y.values[t.key] = e(t, a.validateValue ?? null) : (y.values[t.key] = r(t, g[t.key] ?? null), y.errors[t.key] = a.validateMessage);
|
|
85
|
+
}), l.push(y);
|
|
86
|
+
}), u > s.length && s.push(`还有 ${u - s.length} 个公式单元格没有保存计算结果`), {
|
|
87
|
+
headerMessages: o,
|
|
88
|
+
formulaMessages: s,
|
|
89
|
+
rows: l,
|
|
90
|
+
includedKeys: g
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
// src/core/excel/worker.ts
|
|
94
|
+
s.expose({
|
|
95
|
+
async buildExportExcel(e, t) {
|
|
96
|
+
let r = new c.Workbook(), i = r.addWorksheet("Sheet");
|
|
97
|
+
return n(i, e, t), await r.xlsx.writeBuffer();
|
|
98
|
+
},
|
|
99
|
+
async buildImportTemplate(e) {
|
|
100
|
+
let t = new c.Workbook(), n = t.addWorksheet("导入模板");
|
|
101
|
+
return a(n, e), await t.xlsx.writeBuffer();
|
|
102
|
+
},
|
|
103
|
+
async parseImportFile(e, t) {
|
|
104
|
+
let n = new c.Workbook();
|
|
105
|
+
if (await n.xlsx.load(await e.arrayBuffer()), n.worksheets.length > 1) return {
|
|
106
|
+
headerMessages: ["文件包含多个工作表, 只支持单个工作表, 请删除其他工作表后再导入"],
|
|
107
|
+
formulaMessages: [],
|
|
108
|
+
rows: [],
|
|
109
|
+
includedKeys: []
|
|
110
|
+
};
|
|
111
|
+
let r = n.worksheets[0];
|
|
112
|
+
return r ? m(r, t) : {
|
|
113
|
+
headerMessages: ["文件里没有工作表"],
|
|
114
|
+
formulaMessages: [],
|
|
115
|
+
rows: [],
|
|
116
|
+
includedKeys: []
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type ExcelJS from "exceljs";
|
|
2
|
+
import type { FieldSchema } from "../schema";
|
|
3
|
+
/** 按 fieldSchemas 写表头与数据行: 列序 = 定义顺序, 不写任何隐藏列 */
|
|
4
|
+
export declare function writeExportSheet(worksheet: ExcelJS.Worksheet, data: Record<string, unknown>[], fieldSchemas: FieldSchema[]): void;
|
|
5
|
+
/** 按 fieldSchemas 写导入模板: 列名 + 每列的填写要求写在同一个表头单元格里, 不写示例行 */
|
|
6
|
+
export declare function writeImportTemplateSheet(worksheet: ExcelJS.Worksheet, fieldSchemas: FieldSchema[]): void;
|
|
7
|
+
/**
|
|
8
|
+
* 自动调整列宽(支持多行文本)
|
|
9
|
+
* @param worksheet Excel 工作表
|
|
10
|
+
* @param minWidth 最小列宽, 默认 20
|
|
11
|
+
* @param maxWidth 最大列宽, 默认 60
|
|
12
|
+
*/
|
|
13
|
+
export declare function autoAdjustColumnWidths(worksheet: ExcelJS.Worksheet, minWidth?: number, maxWidth?: number): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** 是否像一次 HTTP 请求失败: 有响应, 或请求库明确标记过 */
|
|
2
|
+
export declare function isHttpError(error: unknown): boolean;
|
|
3
|
+
/** 取 HTTP 状态码; 网络中断这类没有响应的错误返回 undefined */
|
|
4
|
+
export declare function httpStatusOf(error: unknown): number | undefined;
|
|
5
|
+
/** 取响应体, 用于展示后端返回的具体错误 */
|
|
6
|
+
export declare function httpResponseData(error: unknown): unknown;
|