antd-import-export 0.0.0-stage → 1.0.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 -0
- package/README.md +197 -2
- package/README_EN.md +145 -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/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.cjs +6 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.mjs +2172 -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/package.json +96 -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,198 @@
|
|
|
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
|
+
Excel 的解析和生成在主线程执行——这是 ExcelJS 自身的用法,官方没有提供 Worker 方案。代价是处理大文件时页面会短暂无响应:
|
|
31
|
+
|
|
32
|
+
| 行数 | 导入解析 | 导出生成 |
|
|
33
|
+
| --- | --- | --- |
|
|
34
|
+
| 1,000 | 约 25ms | 约 60ms |
|
|
35
|
+
| 5,000 | 约 125ms | 约 300ms |
|
|
36
|
+
| 20,000 | 约 500ms | 约 1 秒 |
|
|
37
|
+
|
|
38
|
+
单次上限 2 万行,超出会在前端直接拦截。生成文件期间取消按钮会禁用,因为主线程被占满时点击不会即时生效,避免让用户误以为没响应。
|
|
39
|
+
|
|
40
|
+
绝大多数实际文件在几千行以内,感知不到停顿。
|
|
41
|
+
|
|
42
|
+
## 字段定义
|
|
43
|
+
|
|
44
|
+
导入导出的一切都由 `FieldSchema[]` 驱动:导出用它生成表头,导入模板用它写填写要求,解析时用它逐格校验。
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import type { FieldSchema } from "antd-import-export";
|
|
48
|
+
|
|
49
|
+
const fieldSchemas: FieldSchema[] = [
|
|
50
|
+
{
|
|
51
|
+
key: "number",
|
|
52
|
+
title: "编号",
|
|
53
|
+
type: "text",
|
|
54
|
+
property: { required: false, max_length: 20, default_value: null },
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
key: "name",
|
|
58
|
+
title: "名称",
|
|
59
|
+
type: "text",
|
|
60
|
+
property: { required: true, max_length: 60, default_value: null },
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
| type | property | 说明 |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `text` | required / max_length / default_value | 按字符数校验长度 |
|
|
68
|
+
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | 精度按十进制位数判断,不受浮点误差影响 |
|
|
69
|
+
| `boolean` | required / true_label / false_label / default_value | 单元格填文案,接口值转成布尔 |
|
|
70
|
+
| `date` | required / default_value | 严格 `YYYY-MM-DD` |
|
|
71
|
+
| `time` | required / default_value | 严格 `HH:mm`,输出补零 |
|
|
72
|
+
| `datetime` | required / default_value | 严格 `YYYY-MM-DD HH:mm` |
|
|
73
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | 逗号分号均可,导出统一用英文分号 |
|
|
74
|
+
| `single_choice` | required / option_map / default_value | 单元格填文案,接口值取 option_map 里的值 |
|
|
75
|
+
| `multiple_choice` | required / option_map / default_value | 多值用中英文分号分隔 |
|
|
76
|
+
|
|
77
|
+
`importable: false` 的列只出现在导出文件里,不参与导入模板和解析。
|
|
78
|
+
|
|
79
|
+
## 逐字段校验
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { validateField } from "antd-import-export";
|
|
83
|
+
|
|
84
|
+
const result = validateField("19.99", {
|
|
85
|
+
key: "price",
|
|
86
|
+
title: "价格",
|
|
87
|
+
type: "number",
|
|
88
|
+
property: { required: false, precision: 2 },
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// { validateSuccess: true, validateValue: 19.99 }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## 组件
|
|
95
|
+
|
|
96
|
+
```tsx
|
|
97
|
+
import { ExcelImportButton, ExcelExportButton, BatchExportButton } from "antd-import-export";
|
|
98
|
+
|
|
99
|
+
// 导入: 内置模板下载、文件解析、预览编辑、分批提交、行级错误回填
|
|
100
|
+
<ExcelImportButton
|
|
101
|
+
title="供应商"
|
|
102
|
+
fieldSchemas={dataSchema}
|
|
103
|
+
importRows={(payloads, signal) => supplierImportData(payloads, signal)}
|
|
104
|
+
onImported={onRefresh}
|
|
105
|
+
/>;
|
|
106
|
+
|
|
107
|
+
// 按当前筛选条件导出, 内部用游标分批拉取
|
|
108
|
+
<ExcelExportButton
|
|
109
|
+
title="供应商"
|
|
110
|
+
fieldSchemas={dataSchema}
|
|
111
|
+
filters={queryForm}
|
|
112
|
+
totalCount={dataCount}
|
|
113
|
+
fetchPage={(params, signal) => supplierExportData(params, signal)}
|
|
114
|
+
/>;
|
|
115
|
+
|
|
116
|
+
// 导出已勾选的行
|
|
117
|
+
<BatchExportButton title="供应商" fieldSchemas={dataSchema} selectedRows={selectedRows} />;
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
三个组件都不发请求,只调用你传进来的 `importRows` / `fetchPage`。
|
|
121
|
+
|
|
122
|
+
### ExcelImportButton
|
|
123
|
+
|
|
124
|
+
| prop | 类型 | 说明 |
|
|
125
|
+
| --- | --- | --- |
|
|
126
|
+
| `title` | `string` | 按钮、模板文件名、任务标题共用 |
|
|
127
|
+
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
128
|
+
| `importRows` | `(rows, signal) => Promise<Record<string, unknown>[]>` | 一批行提交,返回等长结果;**空对象表示成功** |
|
|
129
|
+
| `onImported` | `() => void` | 任务结束后回调,通常用来刷新列表 |
|
|
130
|
+
|
|
131
|
+
### ExcelExportButton
|
|
132
|
+
|
|
133
|
+
| prop | 类型 | 说明 |
|
|
134
|
+
| --- | --- | --- |
|
|
135
|
+
| `title` | `string` | 文件名与任务标题 |
|
|
136
|
+
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
137
|
+
| `filters` | `Record<string, unknown>` | 并入每批请求参数 |
|
|
138
|
+
| `totalCount` | `number` | 用于条数上限校验与进度总数 |
|
|
139
|
+
| `fetchPage` | `(params, signal) => Promise<{ count, results }>` | 游标分页;**每行必须带数字 `id`** |
|
|
140
|
+
| `buttonText` | `string` | 默认「导出」 |
|
|
141
|
+
|
|
142
|
+
### BatchExportButton
|
|
143
|
+
|
|
144
|
+
| prop | 类型 | 说明 |
|
|
145
|
+
| --- | --- | --- |
|
|
146
|
+
| `title` | `string` | 文件名 |
|
|
147
|
+
| `fieldSchemas` | `FieldSchema[]` | 字段定义 |
|
|
148
|
+
| `selectedRows` | `object[]` | 已勾选的行 |
|
|
149
|
+
| `disabled` | `boolean` | 可选 |
|
|
150
|
+
|
|
151
|
+
### 导入结果约定
|
|
152
|
+
|
|
153
|
+
后端逐行返回数组,顺序与请求一致:
|
|
154
|
+
|
|
155
|
+
| 返回 | 含义 |
|
|
156
|
+
| --- | --- |
|
|
157
|
+
| `{}` | 该行成功 |
|
|
158
|
+
| `{ "name": ["不能为空"] }` | 该行字段级失败,回填到预览表格 |
|
|
159
|
+
| `{ "detail": "名称已存在" }` | 该行整体失败 |
|
|
160
|
+
| HTTP 4xx/5xx、网络异常 | 暂停整个任务,可继续或取消 |
|
|
161
|
+
|
|
162
|
+
### 两个使用前提
|
|
163
|
+
|
|
164
|
+
1. 组件用 `App.useApp()` 弹提示,根节点需要包一层 antd 的 `<App>`。
|
|
165
|
+
2. 在 Next.js App Router 里使用时,引入这些组件的文件要加 `"use client"`。
|
|
166
|
+
|
|
167
|
+
## Excel 读写
|
|
168
|
+
|
|
169
|
+
ExcelJS 只在 Worker 里加载,主线程不承担解析和生成。组件接入后这一步由组件内部完成,也可以直接用:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
173
|
+
|
|
174
|
+
const result = await parseImportFile(file, fieldSchemas);
|
|
175
|
+
// { headerMessages, formulaMessages, rows, includedKeys }
|
|
176
|
+
|
|
177
|
+
const blob = await buildExportBlob(rows, fieldSchemas);
|
|
178
|
+
await downloadImportTemplate(fieldSchemas, "供应商导入模板");
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## 模块格式
|
|
182
|
+
|
|
183
|
+
同时提供 ESM 与 CJS,类型声明通过 `exports` 分发。
|
|
184
|
+
|
|
185
|
+
## 本地开发
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
npm install
|
|
189
|
+
npm run dev # 监听构建
|
|
190
|
+
npm run preview # 构建后启动示例页
|
|
191
|
+
npm run verify # 发布门槛: lint + 测试 + 构建 + 示例页类型检查
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
发布到 npm 的完整流程见 [RELEASING.md](./RELEASING.md)。
|
|
195
|
+
|
|
196
|
+
## 许可证
|
|
197
|
+
|
|
198
|
+
MIT
|
package/README_EN.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
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
|
+
## Large files
|
|
28
|
+
|
|
29
|
+
Parsing and generating happen on the main thread — that is how ExcelJS works, it ships no worker mode. Very large files block the UI briefly:
|
|
30
|
+
|
|
31
|
+
| Rows | Import parse | Export build |
|
|
32
|
+
| --- | --- | --- |
|
|
33
|
+
| 1,000 | ~25ms | ~60ms |
|
|
34
|
+
| 5,000 | ~125ms | ~300ms |
|
|
35
|
+
| 20,000 | ~500ms | ~1s |
|
|
36
|
+
|
|
37
|
+
The hard limit is 20,000 rows per operation. The cancel button is disabled while the file is being generated, because clicks cannot take effect while the main thread is busy.
|
|
38
|
+
|
|
39
|
+
Most real-world files stay in the low thousands of rows and show no noticeable pause.
|
|
40
|
+
|
|
41
|
+
## Field schema
|
|
42
|
+
|
|
43
|
+
Everything is driven by `FieldSchema[]`: export headers, template hints and cell-level validation.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import type { FieldSchema } from "antd-import-export";
|
|
47
|
+
|
|
48
|
+
const fieldSchemas: FieldSchema[] = [
|
|
49
|
+
{
|
|
50
|
+
key: "name",
|
|
51
|
+
title: "Name",
|
|
52
|
+
type: "text",
|
|
53
|
+
property: { required: true, max_length: 60, default_value: null },
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
| type | property | notes |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| `text` | required / max_length / default_value | length checked in characters |
|
|
61
|
+
| `number` | required / precision / max_digits? / min_value? / max_value? / default_value | precision judged on decimal places |
|
|
62
|
+
| `boolean` | required / true_label / false_label / default_value | labels in the cell, boolean on the wire |
|
|
63
|
+
| `date` | required / default_value | strict `YYYY-MM-DD` |
|
|
64
|
+
| `time` | required / default_value | strict `HH:mm` |
|
|
65
|
+
| `datetime` | required / default_value | strict `YYYY-MM-DD HH:mm` |
|
|
66
|
+
| `list` | required / list_max_length / text_max_length / item_key? / default_value | comma or semicolon separated on import |
|
|
67
|
+
| `single_choice` | required / option_map / default_value | label in the cell, mapped value on the wire |
|
|
68
|
+
| `multiple_choice` | required / option_map / default_value | multiple values separated by semicolons |
|
|
69
|
+
|
|
70
|
+
Columns marked `importable: false` only appear in exported files.
|
|
71
|
+
|
|
72
|
+
## Validation
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { validateField } from "antd-import-export";
|
|
76
|
+
|
|
77
|
+
validateField("19.99", {
|
|
78
|
+
key: "price",
|
|
79
|
+
title: "Price",
|
|
80
|
+
type: "number",
|
|
81
|
+
property: { required: false, precision: 2 },
|
|
82
|
+
});
|
|
83
|
+
// { validateSuccess: true, validateValue: 19.99 }
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Components
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { ExcelImportButton, ExcelExportButton, BatchExportButton } from "antd-import-export";
|
|
90
|
+
|
|
91
|
+
<ExcelImportButton
|
|
92
|
+
title="Supplier"
|
|
93
|
+
fieldSchemas={fieldSchemas}
|
|
94
|
+
importRows={(payloads, signal) => importSuppliers(payloads, signal)}
|
|
95
|
+
onImported={refresh}
|
|
96
|
+
/>;
|
|
97
|
+
|
|
98
|
+
<ExcelExportButton
|
|
99
|
+
title="Supplier"
|
|
100
|
+
fieldSchemas={fieldSchemas}
|
|
101
|
+
filters={queryForm}
|
|
102
|
+
totalCount={totalCount}
|
|
103
|
+
fetchPage={(params, signal) => exportSuppliers(params, signal)}
|
|
104
|
+
/>;
|
|
105
|
+
|
|
106
|
+
<BatchExportButton title="Supplier" fieldSchemas={fieldSchemas} selectedRows={selectedRows} />;
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The components never issue requests themselves: they call the `importRows` / `fetchPage` you provide.
|
|
110
|
+
|
|
111
|
+
### Two prerequisites
|
|
112
|
+
|
|
113
|
+
1. They use `App.useApp()`, so wrap the root with antd's `<App>`.
|
|
114
|
+
2. In Next.js App Router, add `"use client"` to the file that imports them.
|
|
115
|
+
|
|
116
|
+
## Excel I/O
|
|
117
|
+
|
|
118
|
+
ExcelJS only loads inside a Worker.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
import { parseImportFile, buildExportBlob, downloadImportTemplate } from "antd-import-export";
|
|
122
|
+
|
|
123
|
+
const result = await parseImportFile(file, fieldSchemas);
|
|
124
|
+
const blob = await buildExportBlob(rows, fieldSchemas);
|
|
125
|
+
await downloadImportTemplate(fieldSchemas, "template");
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Module format
|
|
129
|
+
|
|
130
|
+
Ships both ESM and CJS, with type declarations resolved through `exports`.
|
|
131
|
+
|
|
132
|
+
## Development
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm install
|
|
136
|
+
npm run dev # watch build
|
|
137
|
+
npm run preview # build, then start the demo
|
|
138
|
+
npm run verify # release gate: lint + tests + build + demo type check
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
See [RELEASING.md](./RELEASING.md) for the full npm release process.
|
|
142
|
+
|
|
143
|
+
## License
|
|
144
|
+
|
|
145
|
+
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 { FieldSchema } from "../schema";
|
|
2
|
+
import type { ImportParseResult } from "./parseSheet";
|
|
3
|
+
export { autoAdjustColumnWidths } from "./worksheet";
|
|
4
|
+
/** 导出: 生成 xlsx 内容, 由调用方决定何时下载 */
|
|
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
|
+
/** 导入: 解析并按字段定义校验 Excel, 返回可提交的数据与逐行问题 */
|
|
11
|
+
export declare function parseImportFile(file: File, fieldSchemas: FieldSchema[]): Promise<ImportParseResult>;
|
|
@@ -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;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** 字段定义的公共部分 */
|
|
2
|
+
interface FieldSchemaBase {
|
|
3
|
+
key: string;
|
|
4
|
+
title: string;
|
|
5
|
+
/** 是否参与导入(模板与解析): 导出固定用全部列, 标记为 false 的列只出现在导出里 */
|
|
6
|
+
importable?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface TextFieldSchema extends FieldSchemaBase {
|
|
9
|
+
type: "text";
|
|
10
|
+
property: {
|
|
11
|
+
required: boolean;
|
|
12
|
+
max_length: number;
|
|
13
|
+
default_value?: string | null;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export interface NumberFieldSchema extends FieldSchemaBase {
|
|
17
|
+
type: "number";
|
|
18
|
+
property: {
|
|
19
|
+
required: boolean;
|
|
20
|
+
precision: number;
|
|
21
|
+
/** 总位数上限, 对应数据库 DecimalField.max_digits */
|
|
22
|
+
max_digits?: number;
|
|
23
|
+
min_value?: number;
|
|
24
|
+
max_value?: number;
|
|
25
|
+
default_value?: number | null;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface BooleanFieldSchema extends FieldSchemaBase {
|
|
29
|
+
type: "boolean";
|
|
30
|
+
property: {
|
|
31
|
+
required: boolean;
|
|
32
|
+
true_label: string;
|
|
33
|
+
false_label: string;
|
|
34
|
+
default_value?: boolean | null;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface DateFieldSchema extends FieldSchemaBase {
|
|
38
|
+
type: "date";
|
|
39
|
+
property: {
|
|
40
|
+
required: boolean;
|
|
41
|
+
default_value?: string | null;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export interface TimeFieldSchema extends FieldSchemaBase {
|
|
45
|
+
type: "time";
|
|
46
|
+
property: {
|
|
47
|
+
required: boolean;
|
|
48
|
+
default_value?: string | null;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export interface DateTimeFieldSchema extends FieldSchemaBase {
|
|
52
|
+
type: "datetime";
|
|
53
|
+
property: {
|
|
54
|
+
required: boolean;
|
|
55
|
+
default_value?: string | null;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export interface ListFieldSchema extends FieldSchemaBase {
|
|
59
|
+
type: "list";
|
|
60
|
+
property: {
|
|
61
|
+
required: boolean;
|
|
62
|
+
list_max_length: number;
|
|
63
|
+
text_max_length: number;
|
|
64
|
+
/** 数组元素是对象时, 取该键的值拼成文本(导出展示用) */
|
|
65
|
+
item_key?: string;
|
|
66
|
+
default_value?: string[] | [];
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export interface SingleChoiceFieldSchema extends FieldSchemaBase {
|
|
70
|
+
type: "single_choice";
|
|
71
|
+
property: {
|
|
72
|
+
required: boolean;
|
|
73
|
+
option_map: Record<string, string>;
|
|
74
|
+
default_value?: string | null;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export interface MultipleChoiceFieldSchema extends FieldSchemaBase {
|
|
78
|
+
type: "multiple_choice";
|
|
79
|
+
property: {
|
|
80
|
+
required: boolean;
|
|
81
|
+
option_map: Record<string, string>;
|
|
82
|
+
default_value?: string[] | [];
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export type FieldSchema = TextFieldSchema | NumberFieldSchema | BooleanFieldSchema | DateFieldSchema | TimeFieldSchema | DateTimeFieldSchema | ListFieldSchema | SingleChoiceFieldSchema | MultipleChoiceFieldSchema;
|
|
86
|
+
/** 导入模板与解析用: 去掉只供导出的列 */
|
|
87
|
+
export declare function importableFields(fieldSchemas: FieldSchema[]): FieldSchema[];
|
|
88
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { BooleanFieldSchema, FieldSchema, ListFieldSchema, MultipleChoiceFieldSchema, NumberFieldSchema, SingleChoiceFieldSchema, TextFieldSchema } from "./index";
|
|
2
|
+
interface ValidateSuccess {
|
|
3
|
+
validateSuccess: true;
|
|
4
|
+
validateValue: unknown;
|
|
5
|
+
}
|
|
6
|
+
interface ValidateFail {
|
|
7
|
+
validateSuccess: false;
|
|
8
|
+
validateMessage: string;
|
|
9
|
+
}
|
|
10
|
+
type ValidateResult = ValidateSuccess | ValidateFail;
|
|
11
|
+
export declare function validateTextField(value: string, fieldSchema: TextFieldSchema): ValidateResult;
|
|
12
|
+
export declare function validateNumberField(value: string, fieldSchema: NumberFieldSchema): ValidateResult;
|
|
13
|
+
export declare function validateBooleanField(value: string, fieldSchema: BooleanFieldSchema): ValidateResult;
|
|
14
|
+
export declare function validateDateField(value: string): ValidateResult;
|
|
15
|
+
export declare function validateTimeField(value: string): ValidateResult;
|
|
16
|
+
export declare function validateDateTimeField(value: string): ValidateResult;
|
|
17
|
+
export declare function validateListField(value: string, fieldSchema: ListFieldSchema): ValidateResult;
|
|
18
|
+
export declare function validateSingleChoiceField(value: string, fieldSchema: SingleChoiceFieldSchema): ValidateResult;
|
|
19
|
+
export declare function validateMultipleChoiceField(value: string, fieldSchema: MultipleChoiceFieldSchema): ValidateResult;
|
|
20
|
+
export declare function validateField(value: string | null, fieldSchema: FieldSchema): ValidateResult;
|
|
21
|
+
export {};
|