@senlinz/import-export-wasm 0.0.1-beta.4 → 0.0.1-beta.5
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 +120 -1
- package/package.json +1 -1
- package/pkg/imexport_wasm.d.ts +15 -9
- package/pkg/imexport_wasm.js +35 -10
- package/pkg/imexport_wasm_bg.wasm +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +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.
|
package/README.md
CHANGED
|
@@ -1 +1,120 @@
|
|
|
1
|
-
# @senlinz/import-export-wasm
|
|
1
|
+
# @senlinz/import-export-wasm
|
|
2
|
+
**@senlinz/import-export-wasm** is the Rust WebAssembly core library for the **@senlinz/import-export** library. It provides the core logic for handling Excel data in browser environments.
|
|
3
|
+
|
|
4
|
+
> **Note:** This library is currently in beta. Features and APIs may change.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
- Rust WebAssembly core for efficient Excel data handling.
|
|
8
|
+
- Read using [calamine](https://docs.rs/calamine/).
|
|
9
|
+
- Write using [rust_xlsxwriter](https://docs.rs/rust_xlsxwriter/).
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
```html
|
|
13
|
+
<!DOCTYPE html>
|
|
14
|
+
<html lang="en">
|
|
15
|
+
|
|
16
|
+
<head>
|
|
17
|
+
<meta charset="UTF-8">
|
|
18
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
19
|
+
<title>Demo (@senlin/import-export-wasm)</title>
|
|
20
|
+
</head>
|
|
21
|
+
|
|
22
|
+
<button id="downloadTemplate">Download Template</button>
|
|
23
|
+
<button id="exportWithData">Export With Data</button>
|
|
24
|
+
<button id="importWithData">Import With Data</button>
|
|
25
|
+
<a id="download" style="display: none"></a>
|
|
26
|
+
<input type="file" accept=".xlsx" id="file" style="display: none">
|
|
27
|
+
|
|
28
|
+
<body>
|
|
29
|
+
<script type="module">
|
|
30
|
+
import initAsync, { createTemplate, exportData, importData, ExcelInfo, ExcelColumnInfo, ExcelData, ExcelRowData, ExcelColumnData } from 'https://cdn.jsdelivr.net/npm/@senlinz/import-export-wasm/pkg/imexport_wasm.js';
|
|
31
|
+
await initAsync({ path: 'https://cdn.jsdelivr.net/npm/@senlinz/import-export-wasm/pkg/imexport_wasm_bg.wasm' });
|
|
32
|
+
|
|
33
|
+
const downloadEl = document.getElementById('download');
|
|
34
|
+
const fileEl = document.getElementById('file');
|
|
35
|
+
|
|
36
|
+
fileEl.addEventListener('change', fileChange);
|
|
37
|
+
document.getElementById('downloadTemplate').addEventListener('click', downloadTemplate);
|
|
38
|
+
document.getElementById('exportWithData').addEventListener('click', exportWithData);
|
|
39
|
+
document.getElementById('importWithData').addEventListener('click', importWithData);
|
|
40
|
+
|
|
41
|
+
const res = await fetch('https://cdn.jsdelivr.net/npm/@senlinz/import-export-wasm/pkg/imexport_wasm_bg.wasm');
|
|
42
|
+
const bytes = await res.arrayBuffer();
|
|
43
|
+
|
|
44
|
+
function downloadTemplate() {
|
|
45
|
+
const info = getExcelInfo();
|
|
46
|
+
download(info.name, createTemplate(info));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function exportWithData() {
|
|
50
|
+
const info = getExcelInfo();
|
|
51
|
+
const data = new ExcelData([
|
|
52
|
+
new ExcelRowData([
|
|
53
|
+
new ExcelColumnData("name", "Tom"),
|
|
54
|
+
new ExcelColumnData("age", "12"),
|
|
55
|
+
new ExcelColumnData("category", "Cat")
|
|
56
|
+
]),
|
|
57
|
+
new ExcelRowData([
|
|
58
|
+
new ExcelColumnData("name", "Jerry"),
|
|
59
|
+
new ExcelColumnData("age", "10"),
|
|
60
|
+
new ExcelColumnData("category", "Mouse")
|
|
61
|
+
])
|
|
62
|
+
]);
|
|
63
|
+
download(info.name, exportData(info, data));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function importWithData() {
|
|
67
|
+
fileEl.click();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function fileChange(e) {
|
|
71
|
+
const file = e.target.files[0];
|
|
72
|
+
const reader = new FileReader();
|
|
73
|
+
reader.onload = (e) => {
|
|
74
|
+
const data = new Uint8Array(e.target.result);
|
|
75
|
+
const info = getExcelInfo();
|
|
76
|
+
const excelData = importData(info, data);
|
|
77
|
+
console.log(toJson(excelData));
|
|
78
|
+
};
|
|
79
|
+
reader.readAsArrayBuffer(file);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function download(name, excelData) {
|
|
83
|
+
const blob = new Blob([excelData], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
|
|
84
|
+
const url = URL.createObjectURL(blob);
|
|
85
|
+
downloadEl.download = `${name}.xlsx`;
|
|
86
|
+
downloadEl.href = url;
|
|
87
|
+
downloadEl.click();
|
|
88
|
+
URL.revokeObjectURL(url);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getExcelInfo() {
|
|
92
|
+
const excelName = "TomAndJerry";
|
|
93
|
+
const columns = [
|
|
94
|
+
new ExcelColumnInfo("name", "Name", 20),
|
|
95
|
+
new ExcelColumnInfo("age", "Age"),
|
|
96
|
+
new ExcelColumnInfo("category", "Category")
|
|
97
|
+
];
|
|
98
|
+
const info = new ExcelInfo(excelName, "sheet1", columns);
|
|
99
|
+
return info;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function toJson(excelData) {
|
|
103
|
+
const data = [];
|
|
104
|
+
const info = getExcelInfo();
|
|
105
|
+
for (let i = 0; i < excelData.rows.length; i++) {
|
|
106
|
+
const item = {};
|
|
107
|
+
let row = excelData.rows[i];
|
|
108
|
+
for (let j = 0; j < row.columns.length; j++) {
|
|
109
|
+
let column = row.columns[j];
|
|
110
|
+
item[column.key] = column.value;
|
|
111
|
+
}
|
|
112
|
+
data.push(item);
|
|
113
|
+
}
|
|
114
|
+
return data;
|
|
115
|
+
}
|
|
116
|
+
</script>
|
|
117
|
+
</body>
|
|
118
|
+
|
|
119
|
+
</html>
|
|
120
|
+
```
|
package/package.json
CHANGED
package/pkg/imexport_wasm.d.ts
CHANGED
|
@@ -40,14 +40,18 @@ export class ExcelColumnInfo {
|
|
|
40
40
|
/**
|
|
41
41
|
* @param {string} key
|
|
42
42
|
* @param {string} name
|
|
43
|
+
* @param {number | undefined} [width]
|
|
43
44
|
*/
|
|
44
|
-
constructor(key: string, name: string);
|
|
45
|
+
constructor(key: string, name: string, width?: number);
|
|
45
46
|
/**
|
|
46
47
|
*/
|
|
47
48
|
key: string;
|
|
48
49
|
/**
|
|
49
50
|
*/
|
|
50
51
|
name: string;
|
|
52
|
+
/**
|
|
53
|
+
*/
|
|
54
|
+
width?: number;
|
|
51
55
|
}
|
|
52
56
|
/**
|
|
53
57
|
*/
|
|
@@ -112,10 +116,11 @@ export interface InitOutput {
|
|
|
112
116
|
readonly __wbg_get_excelcolumndata_value: (a: number, b: number) => void;
|
|
113
117
|
readonly __wbg_set_excelcolumndata_value: (a: number, b: number, c: number) => void;
|
|
114
118
|
readonly excelcolumndata_new: (a: number, b: number, c: number, d: number) => number;
|
|
115
|
-
readonly createTemplate: (a: number, b: number) => void;
|
|
116
|
-
readonly importData: (a: number, b: number, c: number) => number;
|
|
117
|
-
readonly exportData: (a: number, b: number, c: number) => void;
|
|
118
119
|
readonly __wbg_excelinfo_free: (a: number, b: number) => void;
|
|
120
|
+
readonly __wbg_get_excelinfo_name: (a: number, b: number) => void;
|
|
121
|
+
readonly __wbg_set_excelinfo_name: (a: number, b: number, c: number) => void;
|
|
122
|
+
readonly __wbg_get_excelinfo_sheet_name: (a: number, b: number) => void;
|
|
123
|
+
readonly __wbg_set_excelinfo_sheet_name: (a: number, b: number, c: number) => void;
|
|
119
124
|
readonly __wbg_get_excelinfo_columns: (a: number, b: number) => void;
|
|
120
125
|
readonly __wbg_set_excelinfo_columns: (a: number, b: number, c: number) => void;
|
|
121
126
|
readonly excelinfo_new: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
|
|
@@ -124,11 +129,12 @@ export interface InitOutput {
|
|
|
124
129
|
readonly __wbg_set_excelcolumninfo_key: (a: number, b: number, c: number) => void;
|
|
125
130
|
readonly __wbg_get_excelcolumninfo_name: (a: number, b: number) => void;
|
|
126
131
|
readonly __wbg_set_excelcolumninfo_name: (a: number, b: number, c: number) => void;
|
|
127
|
-
readonly
|
|
128
|
-
readonly
|
|
129
|
-
readonly
|
|
130
|
-
readonly
|
|
131
|
-
readonly
|
|
132
|
+
readonly __wbg_get_excelcolumninfo_width: (a: number, b: number) => void;
|
|
133
|
+
readonly __wbg_set_excelcolumninfo_width: (a: number, b: number, c: number) => void;
|
|
134
|
+
readonly excelcolumninfo_new: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
|
|
135
|
+
readonly createTemplate: (a: number, b: number) => void;
|
|
136
|
+
readonly importData: (a: number, b: number, c: number) => number;
|
|
137
|
+
readonly exportData: (a: number, b: number, c: number) => void;
|
|
132
138
|
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
|
|
133
139
|
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
134
140
|
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
package/pkg/imexport_wasm.js
CHANGED
|
@@ -132,6 +132,10 @@ function passStringToWasm0(arg, malloc, realloc) {
|
|
|
132
132
|
return ptr;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
function isLikeNone(x) {
|
|
136
|
+
return x === undefined || x === null;
|
|
137
|
+
}
|
|
138
|
+
|
|
135
139
|
function _assertClass(instance, klass) {
|
|
136
140
|
if (!(instance instanceof klass)) {
|
|
137
141
|
throw new Error(`expected instance of ${klass.name}`);
|
|
@@ -397,15 +401,36 @@ export class ExcelColumnInfo {
|
|
|
397
401
|
wasm.__wbg_set_excelcolumninfo_name(this.__wbg_ptr, ptr0, len0);
|
|
398
402
|
}
|
|
399
403
|
/**
|
|
404
|
+
* @returns {number | undefined}
|
|
405
|
+
*/
|
|
406
|
+
get width() {
|
|
407
|
+
try {
|
|
408
|
+
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
409
|
+
wasm.__wbg_get_excelcolumninfo_width(retptr, this.__wbg_ptr);
|
|
410
|
+
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
411
|
+
var r2 = getDataViewMemory0().getFloat64(retptr + 8 * 1, true);
|
|
412
|
+
return r0 === 0 ? undefined : r2;
|
|
413
|
+
} finally {
|
|
414
|
+
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* @param {number | undefined} [arg0]
|
|
419
|
+
*/
|
|
420
|
+
set width(arg0) {
|
|
421
|
+
wasm.__wbg_set_excelcolumninfo_width(this.__wbg_ptr, !isLikeNone(arg0), isLikeNone(arg0) ? 0 : arg0);
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
400
424
|
* @param {string} key
|
|
401
425
|
* @param {string} name
|
|
426
|
+
* @param {number | undefined} [width]
|
|
402
427
|
*/
|
|
403
|
-
constructor(key, name) {
|
|
428
|
+
constructor(key, name, width) {
|
|
404
429
|
const ptr0 = passStringToWasm0(key, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
405
430
|
const len0 = WASM_VECTOR_LEN;
|
|
406
431
|
const ptr1 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
407
432
|
const len1 = WASM_VECTOR_LEN;
|
|
408
|
-
const ret = wasm.excelcolumninfo_new(ptr0, len0, ptr1, len1);
|
|
433
|
+
const ret = wasm.excelcolumninfo_new(ptr0, len0, ptr1, len1, !isLikeNone(width), isLikeNone(width) ? 0 : width);
|
|
409
434
|
this.__wbg_ptr = ret >>> 0;
|
|
410
435
|
ExcelColumnInfoFinalization.register(this, this.__wbg_ptr, this);
|
|
411
436
|
return this;
|
|
@@ -501,7 +526,7 @@ export class ExcelInfo {
|
|
|
501
526
|
let deferred1_1;
|
|
502
527
|
try {
|
|
503
528
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
504
|
-
wasm.
|
|
529
|
+
wasm.__wbg_get_excelinfo_name(retptr, this.__wbg_ptr);
|
|
505
530
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
506
531
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
507
532
|
deferred1_0 = r0;
|
|
@@ -518,7 +543,7 @@ export class ExcelInfo {
|
|
|
518
543
|
set name(arg0) {
|
|
519
544
|
const ptr0 = passStringToWasm0(arg0, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
520
545
|
const len0 = WASM_VECTOR_LEN;
|
|
521
|
-
wasm.
|
|
546
|
+
wasm.__wbg_set_excelinfo_name(this.__wbg_ptr, ptr0, len0);
|
|
522
547
|
}
|
|
523
548
|
/**
|
|
524
549
|
* @returns {string}
|
|
@@ -528,7 +553,7 @@ export class ExcelInfo {
|
|
|
528
553
|
let deferred1_1;
|
|
529
554
|
try {
|
|
530
555
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
531
|
-
wasm.
|
|
556
|
+
wasm.__wbg_get_excelinfo_sheet_name(retptr, this.__wbg_ptr);
|
|
532
557
|
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
|
|
533
558
|
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
|
|
534
559
|
deferred1_0 = r0;
|
|
@@ -545,7 +570,7 @@ export class ExcelInfo {
|
|
|
545
570
|
set sheet_name(arg0) {
|
|
546
571
|
const ptr0 = passStringToWasm0(arg0, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
547
572
|
const len0 = WASM_VECTOR_LEN;
|
|
548
|
-
wasm.
|
|
573
|
+
wasm.__wbg_set_excelinfo_sheet_name(this.__wbg_ptr, ptr0, len0);
|
|
549
574
|
}
|
|
550
575
|
/**
|
|
551
576
|
* @returns {(ExcelColumnInfo)[]}
|
|
@@ -694,14 +719,14 @@ async function __wbg_load(module, imports) {
|
|
|
694
719
|
function __wbg_get_imports() {
|
|
695
720
|
const imports = {};
|
|
696
721
|
imports.wbg = {};
|
|
697
|
-
imports.wbg.__wbg_excelcolumndata_new = function(arg0) {
|
|
698
|
-
const ret = ExcelColumnData.__wrap(arg0);
|
|
699
|
-
return addHeapObject(ret);
|
|
700
|
-
};
|
|
701
722
|
imports.wbg.__wbg_excelcolumninfo_new = function(arg0) {
|
|
702
723
|
const ret = ExcelColumnInfo.__wrap(arg0);
|
|
703
724
|
return addHeapObject(ret);
|
|
704
725
|
};
|
|
726
|
+
imports.wbg.__wbg_excelcolumndata_new = function(arg0) {
|
|
727
|
+
const ret = ExcelColumnData.__wrap(arg0);
|
|
728
|
+
return addHeapObject(ret);
|
|
729
|
+
};
|
|
705
730
|
imports.wbg.__wbg_excelrowdata_new = function(arg0) {
|
|
706
731
|
const ret = ExcelRowData.__wrap(arg0);
|
|
707
732
|
return addHeapObject(ret);
|
|
Binary file
|