@senlinz/import-export 0.1.1 → 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/README.md CHANGED
@@ -10,29 +10,14 @@ High-level browser API for Excel template generation, export, and import.
10
10
  pnpm add @senlinz/import-export
11
11
  ```
12
12
 
13
- ## Choose your mode
13
+ ## WASM loading
14
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.
15
+ - `importExcel`, `importExcelDynamic`, `exportExcel`, `fromExcel`, `fromExcelDynamic`, `toExcel`, `downloadExcelTemplate`, and `generateExcelTemplate` auto-load and initialize the bundled WASM asset on first use.
16
+ - In Vite and other browser ESM bundlers, no manual `initializeWasm(...)` call or `?url` import is required when using the high-level core package.
17
+ - If you need to manage the WASM module yourself, use `@senlinz/import-export-wasm` directly instead of this package.
27
18
 
28
19
  ```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 });
20
+ import { toExcel } from '@senlinz/import-export';
36
21
 
37
22
  const workbook = await toExcel({
38
23
  name: 'TomAndJerry',
@@ -40,8 +25,6 @@ const workbook = await toExcel({
40
25
  }, [{ name: 'Tom' }]);
41
26
  ```
42
27
 
43
- If you provide invalid manual input, `initializeWasm(...)` throws a clear error immediately.
44
-
45
28
  ## Supported API
46
29
 
47
30
  ### Stable definition fields
@@ -51,12 +34,14 @@ If you provide invalid manual input, `initializeWasm(...)` throws a clear error
51
34
  - `columns` - stable schema for headers, validation, and row mapping
52
35
  - `author` - optional workbook author metadata
53
36
  - `createTime` - optional workbook creation time as a `Date` or date string
37
+ - `maxFileSizeBytes` - browser upload limit in bytes (default 25 MiB) enforced before reading a file
54
38
  - `title`, `titleHeight`, `titleFormat` - optional merged title row and its layout/format
55
39
  - `defaultRowHeight`, `headerRowHeight` - row heights for exported data rows and header rows
56
40
  - `dx`, `dy` - horizontal and vertical offsets before the header starts
57
41
  - `isHeaderFreeze` - freezes the header area so column labels stay visible while scrolling
58
42
  - `progressCallback` - progress hook for long-running import/export work
59
43
  - `imageFetcher` - required resolver for `image` columns during export
44
+ - `escapeFormulas` - escapes formula-like text during export to block Excel formula injection (default: enabled)
60
45
 
61
46
  ### Schema-less import
62
47
 
@@ -91,6 +76,7 @@ console.log(result.rows);
91
76
  - `importExcelDynamic(...)` depends on DOM/browser file upload APIs.
92
77
  - `sheetName` is optional and falls back to the first worksheet when missing.
93
78
  - `headerRow` is optional, 1-based, and defaults to the first non-empty row in the selected sheet.
79
+ - `maxFileSizeBytes` is optional and uses the same default 25 MiB browser upload limit as schema-based import.
94
80
  - The result shape is `{ sheetName, headers, rows }`.
95
81
  - Dynamic import requires non-empty, unique header names in the selected header row.
96
82
 
@@ -150,6 +136,7 @@ const rows = await importExcel(definition);
150
136
  - Empty imported `number` and `date` cells are returned as `null`.
151
137
  - Imported `date` values are returned as formatted strings.
152
138
  - Unknown or malformed schemas fail fast before any workbook operation starts.
139
+ - Browser uploads enforce `maxFileSizeBytes` before reading the file (default 25 MiB).
153
140
  - Use `importExcelDynamic(...)` for browser uploads without a schema, or `fromExcelDynamic(...)` when you already have workbook bytes.
154
141
 
155
142
  ## Export behavior
@@ -157,6 +144,7 @@ const rows = await importExcel(definition);
157
144
  - `null` and `undefined` values are exported as blank cells.
158
145
  - Number columns reject non-finite values.
159
146
  - Date columns accept `Date` instances or parseable date strings.
147
+ - Text columns escape values that look like formulas by default; set `escapeFormulas: false` to allow formulas intentionally.
160
148
  - Grouped export objects must use `{ value?, children: [...] }` for configured `dataGroup` columns.
161
149
  - Image columns require `imageFetcher`.
162
150
 
@@ -178,12 +166,11 @@ These advanced features are supported and considered part of the public API:
178
166
  - `downloadExcelTemplate`, `exportExcel`, `importExcel`, and `importExcelDynamic` require DOM/browser APIs.
179
167
  - `fromExcel`, `toExcel`, and `generateExcelTemplate` can be used in other runtimes when browser-compatible globals are available.
180
168
  - `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`.
169
+ - The core package initializes its emitted bundled WASM asset asynchronously before the first workbook operation.
182
170
 
183
171
  ## Examples
184
172
 
185
173
  - [Basic browser example](./examples/basic-browser.html)
186
- - [Manual WASM browser example](./examples/manual-wasm-browser.html)
187
174
  - [Grouped export example](./examples/grouped-export.html)
188
175
  - [Definition validation example](./examples/definition-errors.html)
189
176
 
package/README.zh.md CHANGED
@@ -10,29 +10,14 @@
10
10
  pnpm add @senlinz/import-export
11
11
  ```
12
12
 
13
- ## 选择使用模式
13
+ ## WASM 加载
14
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 / 测试中显式走手动初始化流程。
15
+ - `importExcel`、`importExcelDynamic`、`exportExcel`、`fromExcel`、`fromExcelDynamic`、`toExcel`、`downloadExcelTemplate`、`generateExcelTemplate` 会在首次调用时自动加载并初始化内置 WASM 资源。
16
+ - 在 Vite 和其他浏览器 ESM bundler 中,使用高阶 core 包时不再需要手动调用 `initializeWasm(...)` 或自己处理 `?url`。
17
+ - 如果你需要自行管理 WASM 模块,请直接使用 `@senlinz/import-export-wasm`,而不是这个高阶包。
27
18
 
28
19
  ```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 });
20
+ import { toExcel } from '@senlinz/import-export';
36
21
 
37
22
  const workbook = await toExcel({
38
23
  name: 'TomAndJerry',
@@ -40,8 +25,6 @@ const workbook = await toExcel({
40
25
  }, [{ name: 'Tom' }]);
41
26
  ```
42
27
 
43
- 如果手动传入的 WASM 输入无效,`initializeWasm(...)` 会立即抛出清晰的错误信息。
44
-
45
28
  ## 支持的 API
46
29
 
47
30
  ### 稳定的 definition 字段
@@ -51,12 +34,14 @@ const workbook = await toExcel({
51
34
  - `columns`:表头、校验和行映射使用的稳定列定义
52
35
  - `author`:可选的工作簿作者元数据
53
36
  - `createTime`:可选的工作簿创建时间,支持 `Date` 或日期字符串
37
+ - `maxFileSizeBytes`:浏览器上传的字节大小上限(默认 25 MiB),在读取前先校验
54
38
  - `title`、`titleHeight`、`titleFormat`:可选的合并标题行及其高度、样式
55
39
  - `defaultRowHeight`、`headerRowHeight`:导出时数据行和表头行的行高
56
40
  - `dx`、`dy`:表头写入前的横向、纵向偏移量
57
41
  - `isHeaderFreeze`:冻结表头区域,滚动时仍保持列标题可见
58
42
  - `progressCallback`:长时间导入 / 导出过程中的进度回调
59
43
  - `imageFetcher`:导出 `image` 列时必需的图片数据解析回调
44
+ - `escapeFormulas`:导出时自动转义类似公式的文本,默认开启,可显式关闭以允许公式
60
45
 
61
46
  ### 稳定的列字段
62
47
 
@@ -134,6 +119,7 @@ const result = await fromExcelDynamic(fileBytes, {
134
119
  - `importExcelDynamic(...)` 依赖 DOM / 浏览器文件上传 API。
135
120
  - `sheetName` 可选,未提供或不存在时会回退到第一个工作表。
136
121
  - `headerRow` 可选,使用从 `1` 开始的行号,默认会自动选择首个非空行。
122
+ - `maxFileSizeBytes` 可选,与 schema 导入相同,默认 25 MiB。
137
123
  - 返回值结构为 `{ sheetName, headers, rows }`。
138
124
 
139
125
  ## 导入行为
@@ -143,6 +129,7 @@ const result = await fromExcelDynamic(fileBytes, {
143
129
  - 空的 `number` / `date` 单元格导入后返回 `null`。
144
130
  - `date` 列导入后返回格式化字符串。
145
131
  - 非法或不完整的 schema 会在读取工作簿前立即失败。
132
+ - 浏览器上传会在读取前按 `maxFileSizeBytes` 校验文件大小(默认 25 MiB)。
146
133
  - 无 schema 导入时,可使用 `importExcelDynamic(...)`(浏览器上传)或 `fromExcelDynamic(...)`(字节缓冲)。
147
134
 
148
135
  ## 导出行为
@@ -150,6 +137,7 @@ const result = await fromExcelDynamic(fileBytes, {
150
137
  - `null` 和 `undefined` 会导出为空单元格。
151
138
  - 数字列会拒绝非有限值。
152
139
  - 日期列支持 `Date` 实例或日期字符串。
140
+ - 文本列默认会转义看起来像公式的值;如需导出公式,可设置 `escapeFormulas: false`。
153
141
  - 分组导出对象必须使用 `{ value?, children: [...] }` 结构。
154
142
  - 图片列必须提供 `imageFetcher`。
155
143
 
@@ -170,12 +158,11 @@ const result = await fromExcelDynamic(fileBytes, {
170
158
  - 主要面向浏览器 ESM 运行时。
171
159
  - `downloadExcelTemplate`、`exportExcel`、`importExcel`、`importExcelDynamic` 依赖 DOM / 浏览器 API。
172
160
  - 当运行时提供兼容的浏览器全局对象时,也可以使用 `fromExcel`、`fromExcelDynamic`、`toExcel`、`generateExcelTemplate`。
173
- - `initializeWasm` 可用于使用调用方提供的 `source`、`bytes` 或 `module` 预初始化运行时。
161
+ - core 包会在首次执行工作簿操作前,异步初始化构建产物中的内置 WASM 资源。
174
162
 
175
163
  ## 示例
176
164
 
177
165
  - [基础浏览器示例](./examples/basic-browser.html)
178
- - [手动初始化 WASM 的浏览器示例](./examples/manual-wasm-browser.html)
179
166
  - [分组导出示例](./examples/grouped-export.html)
180
167
  - [Definition 校验示例](./examples/definition-errors.html)
181
168