@senlinz/import-export 0.1.0-beta.8 → 0.1.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.
Files changed (46) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +158 -61
  3. package/README.zh.md +158 -0
  4. package/dist/index.js +2 -0
  5. package/dist/types/ExcelDefinition.d.ts +102 -0
  6. package/dist/types/index.d.ts +11 -6
  7. package/dist/types/runtime.d.ts +20 -0
  8. package/dist/types/utils.d.ts +6 -7
  9. package/package.json +22 -23
  10. package/dist/components/editable-cell.d.ts +0 -11
  11. package/dist/components/editable-cell.js +0 -2
  12. package/dist/components/import-export-definition.d.ts +0 -11
  13. package/dist/components/import-export-definition.js +0 -2
  14. package/dist/components/import-export-studio.d.ts +0 -11
  15. package/dist/components/import-export-studio.js +0 -2
  16. package/dist/components/import-export-table.d.ts +0 -11
  17. package/dist/components/import-export-table.js +0 -2
  18. package/dist/components/index.d.ts +0 -33
  19. package/dist/components/index.js +0 -2
  20. package/dist/components/p-3c6b6bbf.js +0 -2
  21. package/dist/components/p-c9904f6a.js +0 -2
  22. package/dist/components/p-dd1bf849.js +0 -2
  23. package/dist/components/p-ddbe189c.js +0 -2
  24. package/dist/components/p-f7586df1.js +0 -2
  25. package/dist/tests/dist/editable-cell.d.ts +0 -11
  26. package/dist/tests/dist/imexport-table.d.ts +0 -11
  27. package/dist/tests/dist/import-export-definition.d.ts +0 -11
  28. package/dist/tests/dist/import-export-definiton.d.ts +0 -11
  29. package/dist/tests/dist/import-export-studio.d.ts +0 -11
  30. package/dist/tests/dist/import-export-table.d.ts +0 -11
  31. package/dist/tests/dist/index.d.ts +0 -33
  32. package/dist/tests/dist/table-definition.d.ts +0 -11
  33. package/dist/types/components/editable-cell/editable-cell.d.ts +0 -18
  34. package/dist/types/components/imexport-table/imexport-table.d.ts +0 -19
  35. package/dist/types/components/import-export-definition/import-export-definition.d.ts +0 -7
  36. package/dist/types/components/import-export-definition/import-export-definiton.d.ts +0 -6
  37. package/dist/types/components/import-export-studio/import-export-studio.d.ts +0 -14
  38. package/dist/types/components/import-export-studio/import-export-table.d.ts +0 -5
  39. package/dist/types/components/import-export-table/import-export-table.d.ts +0 -6
  40. package/dist/types/components/table-definition/table-definition.d.ts +0 -0
  41. package/dist/types/components.d.ts +0 -116
  42. package/dist/types/declarations/ExcelDefinition.d.ts +0 -17
  43. package/dist/types/declarations/ExcelDefintion.d.ts +0 -17
  44. package/dist/types/stencil-public-runtime.d.ts +0 -1674
  45. package/dist/types/types/index.d.ts +0 -19
  46. 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,158 @@
1
- # @senlinz/import-export
2
- example for import and export excel file
3
-
4
- ```html
5
- <!DOCTYPE html>
6
- <html lang="en">
7
-
8
- <head>
9
- <meta charset="UTF-8">
10
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
11
- <title>IMPORT-EXPORT-TEST</title>
12
- </head>
13
- <button id="btnImport">Import</button>
14
- <button id="btnExport">Export</button>
15
- <button id="btnDownloadTemplate">Download Template</button>
16
- <div id="importOutput"></div>
17
-
18
- <body>
19
- <script type="module">
20
- import {
21
- importExcel,
22
- exportExcel,
23
- downloadExcelTemplate } from 'https://cdn.jsdelivr.net/npm/@senlinz/import-export/+esm';
24
-
25
- const excelInfo = {
26
- name: 'TomAndJerry',
27
- sheetName: 'sheet1',
28
- columns: [
29
- { key: 'name', name: 'Name', dataType: 0, width: 20, note: 'required' },
30
- { key: 'age', name: 'Age', dataType: 1, width: 10 },
31
- { key: 'category', name: 'Category', dataType: 0, allowedValues: ['Cat', 'Mouse'] }
32
- ],
33
- author: 'senlinz',
34
- createTime: '2024-11-01T09:00:00'
35
- };
36
-
37
- btnImport.addEventListener('click', importClick);
38
- btnExport.addEventListener('click', exportClick);
39
- btnDownloadTemplate.addEventListener('click', downloadTemplateClick);
40
-
41
- function exportClick() {
42
- exportExcel(excelInfo, [
43
- { name: 'Tom', age: 12, category: 'Cat' },
44
- { name: 'Jerry', age: 13, category: 'Mouse' }
45
- ]);
46
- }
47
-
48
- function importClick() {
49
- importExcel(excelInfo).then(data => {
50
- importOutput.innerText = JSON.stringify(data, null);
51
- });
52
- }
53
-
54
- function downloadTemplateClick() {
55
- downloadExcelTemplate(excelInfo);
56
- }
57
- </script>
58
- </body>
59
-
60
- </html>
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`, `exportExcel`, `fromExcel`, `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
+ ### Stable column fields
62
+
63
+ - `key` - unique programmatic field key
64
+ - `name` - visible worksheet header label
65
+ - `width` - exported column width
66
+ - `note` - header note/comment shown in Excel
67
+ - `dataType` - scalar cell type
68
+ - `allowedValues` - validation list for allowed cell values
69
+ - `parent` - parent header key for multi-row headers
70
+ - `format` - base cell format for the column
71
+ - `valueFormat` - direct or conditional format overrides for exported values
72
+ - `dataGroup` - logical group identifier for nested export data
73
+ - `dataGroupParent` - parent group identifier for nested export data
74
+
75
+ ### Supported `dataType` values
76
+
77
+ - `text` - default text cells
78
+ - `number` - finite numeric values
79
+ - `date` - `Date` instances or parseable date strings on export
80
+ - `image` - image URLs or identifiers resolved through `imageFetcher`
81
+
82
+ ## Import/export flow
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
+ ## Import behavior
111
+
112
+ - Headers must match the configured `columns[].name` values exactly.
113
+ - Imported `number` columns are returned as numbers.
114
+ - Empty imported `number` and `date` cells are returned as `null`.
115
+ - Imported `date` values are returned as formatted strings.
116
+ - Unknown or malformed schemas fail fast before any workbook operation starts.
117
+
118
+ ## Export behavior
119
+
120
+ - `null` and `undefined` values are exported as blank cells.
121
+ - Number columns reject non-finite values.
122
+ - Date columns accept `Date` instances or parseable date strings.
123
+ - Grouped export objects must use `{ value?, children: [...] }` for configured `dataGroup` columns.
124
+ - Image columns require `imageFetcher`.
125
+
126
+ ## Advanced supported features
127
+
128
+ These advanced features are supported and considered part of the public API:
129
+
130
+ - merged title rows
131
+ - frozen headers
132
+ - column notes
133
+ - allowed value validation lists
134
+ - conditional `valueFormat`
135
+ - nested grouped export data via `dataGroup` and `dataGroupParent`
136
+ - image export via `imageFetcher`
137
+
138
+ ## Browser/runtime support
139
+
140
+ - Browser ESM runtimes are the primary target.
141
+ - `downloadExcelTemplate`, `exportExcel`, and `importExcel` require DOM/browser APIs.
142
+ - `fromExcel`, `toExcel`, and `generateExcelTemplate` can be used in other runtimes when browser-compatible globals are available.
143
+ - `initializeWasm` can be used to pre-initialize the runtime with caller-managed `source`, `bytes`, or `module`.
144
+
145
+ ## Examples
146
+
147
+ - [Basic browser example](./examples/basic-browser.html)
148
+ - [Manual WASM browser example](./examples/manual-wasm-browser.html)
149
+ - [Grouped export example](./examples/grouped-export.html)
150
+ - [Definition validation example](./examples/definition-errors.html)
151
+
152
+ These examples are covered by Playwright tests in [`./tests/import-export.spec.ts`](./tests/import-export.spec.ts).
153
+
154
+ ## Known limitations
155
+
156
+ - Parent headers and data groups must be declared before dependent columns.
157
+ - Date imports normalize to strings instead of `Date` instances.
158
+ - Import currently validates a single worksheet at a time.
package/README.zh.md ADDED
@@ -0,0 +1,158 @@
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`、`exportExcel`、`fromExcel`、`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[].name` 完全一致。
113
+ - `number` 列导入后返回数字。
114
+ - 空的 `number` / `date` 单元格导入后返回 `null`。
115
+ - `date` 列导入后返回格式化字符串。
116
+ - 非法或不完整的 schema 会在读取工作簿前立即失败。
117
+
118
+ ## 导出行为
119
+
120
+ - `null` 和 `undefined` 会导出为空单元格。
121
+ - 数字列会拒绝非有限值。
122
+ - 日期列支持 `Date` 实例或日期字符串。
123
+ - 分组导出对象必须使用 `{ value?, children: [...] }` 结构。
124
+ - 图片列必须提供 `imageFetcher`。
125
+
126
+ ## 已支持的高级能力
127
+
128
+ 以下高级能力已纳入公开 API:
129
+
130
+ - 合并标题行
131
+ - 冻结表头
132
+ - 列备注
133
+ - 下拉候选值
134
+ - 条件 `valueFormat`
135
+ - 使用 `dataGroup` / `dataGroupParent` 的嵌套分组导出
136
+ - 通过 `imageFetcher` 导出图片
137
+
138
+ ## 浏览器 / 运行时支持
139
+
140
+ - 主要面向浏览器 ESM 运行时。
141
+ - `downloadExcelTemplate`、`exportExcel`、`importExcel` 依赖 DOM / 浏览器 API。
142
+ - 当运行时提供兼容的浏览器全局对象时,也可以使用 `fromExcel`、`toExcel`、`generateExcelTemplate`。
143
+ - `initializeWasm` 可用于使用调用方提供的 `source`、`bytes` 或 `module` 预初始化运行时。
144
+
145
+ ## 示例
146
+
147
+ - [基础浏览器示例](./examples/basic-browser.html)
148
+ - [手动初始化 WASM 的浏览器示例](./examples/manual-wasm-browser.html)
149
+ - [分组导出示例](./examples/grouped-export.html)
150
+ - [Definition 校验示例](./examples/definition-errors.html)
151
+
152
+ 这些示例由 [`./tests/import-export.spec.ts`](./tests/import-export.spec.ts) 中的 Playwright 用例覆盖。
153
+
154
+ ## 已知限制
155
+
156
+ - 父级表头和数据分组必须先于依赖它们的列声明。
157
+ - `date` 导入结果会规范化为字符串,而不是 `Date` 实例。
158
+ - 当前一次只校验一个工作表。