@senlinz/import-export 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -2
- package/README.zh.md +38 -3
- package/dist/index.js +1 -1
- package/dist/types/ExcelDefinition.d.ts +22 -2
- package/dist/types/index.d.ts +7 -2
- package/dist/types/utils.d.ts +24 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ pnpm add @senlinz/import-export
|
|
|
15
15
|
### Default mode (recommended)
|
|
16
16
|
|
|
17
17
|
- No setup required.
|
|
18
|
-
- `importExcel`, `exportExcel`, `fromExcel`, `toExcel`, `downloadExcelTemplate`, and `generateExcelTemplate` automatically initialize the bundled WASM runtime.
|
|
18
|
+
- `importExcel`, `importExcelDynamic`, `exportExcel`, `fromExcel`, `fromExcelDynamic`, `toExcel`, `downloadExcelTemplate`, and `generateExcelTemplate` automatically initialize the bundled WASM runtime.
|
|
19
19
|
- Best for most browser applications.
|
|
20
20
|
|
|
21
21
|
### Advanced mode
|
|
@@ -51,12 +51,51 @@ If you provide invalid manual input, `initializeWasm(...)` throws a clear error
|
|
|
51
51
|
- `columns` - stable schema for headers, validation, and row mapping
|
|
52
52
|
- `author` - optional workbook author metadata
|
|
53
53
|
- `createTime` - optional workbook creation time as a `Date` or date string
|
|
54
|
+
- `maxFileSizeBytes` - browser upload limit in bytes (default 25 MiB) enforced before reading a file
|
|
54
55
|
- `title`, `titleHeight`, `titleFormat` - optional merged title row and its layout/format
|
|
55
56
|
- `defaultRowHeight`, `headerRowHeight` - row heights for exported data rows and header rows
|
|
56
57
|
- `dx`, `dy` - horizontal and vertical offsets before the header starts
|
|
57
58
|
- `isHeaderFreeze` - freezes the header area so column labels stay visible while scrolling
|
|
58
59
|
- `progressCallback` - progress hook for long-running import/export work
|
|
59
60
|
- `imageFetcher` - required resolver for `image` columns during export
|
|
61
|
+
- `escapeFormulas` - escapes formula-like text during export to block Excel formula injection (default: enabled)
|
|
62
|
+
|
|
63
|
+
### Schema-less import
|
|
64
|
+
|
|
65
|
+
Use `importExcelDynamic(options?)` when you want to open the browser file picker without defining `columns` ahead of time.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { importExcelDynamic } from '@senlinz/import-export';
|
|
69
|
+
|
|
70
|
+
const result = await importExcelDynamic({
|
|
71
|
+
sheetName: 'sheet1',
|
|
72
|
+
headerRow: 1,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
console.log(result.headers);
|
|
76
|
+
console.log(result.rows);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Use `fromExcelDynamic(buffer, options?)` when you want to read caller-provided workbook bytes without defining `columns` ahead of time.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { fromExcelDynamic } from '@senlinz/import-export';
|
|
83
|
+
|
|
84
|
+
const result = await fromExcelDynamic(fileBytes, {
|
|
85
|
+
sheetName: 'sheet1',
|
|
86
|
+
headerRow: 1,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
console.log(result.headers);
|
|
90
|
+
console.log(result.rows);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- `importExcelDynamic(...)` depends on DOM/browser file upload APIs.
|
|
94
|
+
- `sheetName` is optional and falls back to the first worksheet when missing.
|
|
95
|
+
- `headerRow` is optional, 1-based, and defaults to the first non-empty row in the selected sheet.
|
|
96
|
+
- `maxFileSizeBytes` is optional and uses the same default 25 MiB browser upload limit as schema-based import.
|
|
97
|
+
- The result shape is `{ sheetName, headers, rows }`.
|
|
98
|
+
- Dynamic import requires non-empty, unique header names in the selected header row.
|
|
60
99
|
|
|
61
100
|
### Stable column fields
|
|
62
101
|
|
|
@@ -114,12 +153,15 @@ const rows = await importExcel(definition);
|
|
|
114
153
|
- Empty imported `number` and `date` cells are returned as `null`.
|
|
115
154
|
- Imported `date` values are returned as formatted strings.
|
|
116
155
|
- Unknown or malformed schemas fail fast before any workbook operation starts.
|
|
156
|
+
- Browser uploads enforce `maxFileSizeBytes` before reading the file (default 25 MiB).
|
|
157
|
+
- Use `importExcelDynamic(...)` for browser uploads without a schema, or `fromExcelDynamic(...)` when you already have workbook bytes.
|
|
117
158
|
|
|
118
159
|
## Export behavior
|
|
119
160
|
|
|
120
161
|
- `null` and `undefined` values are exported as blank cells.
|
|
121
162
|
- Number columns reject non-finite values.
|
|
122
163
|
- Date columns accept `Date` instances or parseable date strings.
|
|
164
|
+
- Text columns escape values that look like formulas by default; set `escapeFormulas: false` to allow formulas intentionally.
|
|
123
165
|
- Grouped export objects must use `{ value?, children: [...] }` for configured `dataGroup` columns.
|
|
124
166
|
- Image columns require `imageFetcher`.
|
|
125
167
|
|
|
@@ -138,8 +180,9 @@ These advanced features are supported and considered part of the public API:
|
|
|
138
180
|
## Browser/runtime support
|
|
139
181
|
|
|
140
182
|
- Browser ESM runtimes are the primary target.
|
|
141
|
-
- `downloadExcelTemplate`, `exportExcel`, and `
|
|
183
|
+
- `downloadExcelTemplate`, `exportExcel`, `importExcel`, and `importExcelDynamic` require DOM/browser APIs.
|
|
142
184
|
- `fromExcel`, `toExcel`, and `generateExcelTemplate` can be used in other runtimes when browser-compatible globals are available.
|
|
185
|
+
- `fromExcelDynamic` can also be used in other runtimes when browser-compatible globals are available.
|
|
143
186
|
- `initializeWasm` can be used to pre-initialize the runtime with caller-managed `source`, `bytes`, or `module`.
|
|
144
187
|
|
|
145
188
|
## Examples
|
package/README.zh.md
CHANGED
|
@@ -15,7 +15,7 @@ pnpm add @senlinz/import-export
|
|
|
15
15
|
### 默认模式(推荐)
|
|
16
16
|
|
|
17
17
|
- 无需额外初始化。
|
|
18
|
-
- `importExcel`、`exportExcel`、`fromExcel`、`toExcel`、`downloadExcelTemplate`、`generateExcelTemplate` 会自动初始化内置 WASM 运行时。
|
|
18
|
+
- `importExcel`、`importExcelDynamic`、`exportExcel`、`fromExcel`、`fromExcelDynamic`、`toExcel`、`downloadExcelTemplate`、`generateExcelTemplate` 会自动初始化内置 WASM 运行时。
|
|
19
19
|
- 适合绝大多数浏览器场景。
|
|
20
20
|
|
|
21
21
|
### 高级模式
|
|
@@ -51,12 +51,14 @@ const workbook = await toExcel({
|
|
|
51
51
|
- `columns`:表头、校验和行映射使用的稳定列定义
|
|
52
52
|
- `author`:可选的工作簿作者元数据
|
|
53
53
|
- `createTime`:可选的工作簿创建时间,支持 `Date` 或日期字符串
|
|
54
|
+
- `maxFileSizeBytes`:浏览器上传的字节大小上限(默认 25 MiB),在读取前先校验
|
|
54
55
|
- `title`、`titleHeight`、`titleFormat`:可选的合并标题行及其高度、样式
|
|
55
56
|
- `defaultRowHeight`、`headerRowHeight`:导出时数据行和表头行的行高
|
|
56
57
|
- `dx`、`dy`:表头写入前的横向、纵向偏移量
|
|
57
58
|
- `isHeaderFreeze`:冻结表头区域,滚动时仍保持列标题可见
|
|
58
59
|
- `progressCallback`:长时间导入 / 导出过程中的进度回调
|
|
59
60
|
- `imageFetcher`:导出 `image` 列时必需的图片数据解析回调
|
|
61
|
+
- `escapeFormulas`:导出时自动转义类似公式的文本,默认开启,可显式关闭以允许公式
|
|
60
62
|
|
|
61
63
|
### 稳定的列字段
|
|
62
64
|
|
|
@@ -107,6 +109,36 @@ await exportExcel(definition, [
|
|
|
107
109
|
const rows = await importExcel(definition);
|
|
108
110
|
```
|
|
109
111
|
|
|
112
|
+
## 动态导入
|
|
113
|
+
|
|
114
|
+
当你不想预先定义 `columns`,并希望直接使用浏览器文件选择框时,可使用 `importExcelDynamic(options?)`。
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
import { importExcelDynamic } from '@senlinz/import-export';
|
|
118
|
+
|
|
119
|
+
const result = await importExcelDynamic({
|
|
120
|
+
sheetName: 'sheet1',
|
|
121
|
+
headerRow: 1,
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
当你已经拿到工作簿字节数据时,可继续使用 `fromExcelDynamic(buffer, options?)`。
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { fromExcelDynamic } from '@senlinz/import-export';
|
|
129
|
+
|
|
130
|
+
const result = await fromExcelDynamic(fileBytes, {
|
|
131
|
+
sheetName: 'sheet1',
|
|
132
|
+
headerRow: 1,
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
- `importExcelDynamic(...)` 依赖 DOM / 浏览器文件上传 API。
|
|
137
|
+
- `sheetName` 可选,未提供或不存在时会回退到第一个工作表。
|
|
138
|
+
- `headerRow` 可选,使用从 `1` 开始的行号,默认会自动选择首个非空行。
|
|
139
|
+
- `maxFileSizeBytes` 可选,与 schema 导入相同,默认 25 MiB。
|
|
140
|
+
- 返回值结构为 `{ sheetName, headers, rows }`。
|
|
141
|
+
|
|
110
142
|
## 导入行为
|
|
111
143
|
|
|
112
144
|
- 表头必须与 `columns[].name` 完全一致。
|
|
@@ -114,12 +146,15 @@ const rows = await importExcel(definition);
|
|
|
114
146
|
- 空的 `number` / `date` 单元格导入后返回 `null`。
|
|
115
147
|
- `date` 列导入后返回格式化字符串。
|
|
116
148
|
- 非法或不完整的 schema 会在读取工作簿前立即失败。
|
|
149
|
+
- 浏览器上传会在读取前按 `maxFileSizeBytes` 校验文件大小(默认 25 MiB)。
|
|
150
|
+
- 无 schema 导入时,可使用 `importExcelDynamic(...)`(浏览器上传)或 `fromExcelDynamic(...)`(字节缓冲)。
|
|
117
151
|
|
|
118
152
|
## 导出行为
|
|
119
153
|
|
|
120
154
|
- `null` 和 `undefined` 会导出为空单元格。
|
|
121
155
|
- 数字列会拒绝非有限值。
|
|
122
156
|
- 日期列支持 `Date` 实例或日期字符串。
|
|
157
|
+
- 文本列默认会转义看起来像公式的值;如需导出公式,可设置 `escapeFormulas: false`。
|
|
123
158
|
- 分组导出对象必须使用 `{ value?, children: [...] }` 结构。
|
|
124
159
|
- 图片列必须提供 `imageFetcher`。
|
|
125
160
|
|
|
@@ -138,8 +173,8 @@ const rows = await importExcel(definition);
|
|
|
138
173
|
## 浏览器 / 运行时支持
|
|
139
174
|
|
|
140
175
|
- 主要面向浏览器 ESM 运行时。
|
|
141
|
-
- `downloadExcelTemplate`、`exportExcel`、`importExcel` 依赖 DOM / 浏览器 API。
|
|
142
|
-
- 当运行时提供兼容的浏览器全局对象时,也可以使用 `fromExcel`、`toExcel`、`generateExcelTemplate`。
|
|
176
|
+
- `downloadExcelTemplate`、`exportExcel`、`importExcel`、`importExcelDynamic` 依赖 DOM / 浏览器 API。
|
|
177
|
+
- 当运行时提供兼容的浏览器全局对象时,也可以使用 `fromExcel`、`fromExcelDynamic`、`toExcel`、`generateExcelTemplate`。
|
|
143
178
|
- `initializeWasm` 可用于使用调用方提供的 `source`、`bytes` 或 `module` 预初始化运行时。
|
|
144
179
|
|
|
145
180
|
## 示例
|