excellentexport 3.9.10 → 3.9.15
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/.editorconfig +9 -0
- package/.github/FUNDING.yml +1 -1
- package/.github/dependabot.yml +7 -7
- package/.github/workflows/build.yml +50 -0
- package/LICENSE.txt +21 -21
- package/README.md +401 -391
- package/dist/excellentexport.d.ts +1 -9
- package/dist/excellentexport.js +132 -2
- package/dist/excellentexport.js.LICENSE.txt +206 -2
- package/dist/utils.d.ts +27 -0
- package/index.bigtable.html +55 -55
- package/index.filters.html +63 -63
- package/index.html +155 -127
- package/index.noanchor.html +31 -31
- package/index.rtl.html +68 -68
- package/package.json +14 -18
- package/src/excellentexport.ts +264 -254
- package/src/utils.ts +69 -5
- package/test/checkversion.test.ts +17 -16
- package/test/convert-filters.test.ts +2 -2
- package/test/convert-table.test.ts +94 -2
- package/test/convert.format.ts +56 -56
- package/test/convert.test.ts +60 -59
- package/test/fixdata.test.ts +5 -5
- package/test/negative.test.ts +130 -129
- package/test/simple.test.ts +11 -10
- package/test/tsconfig.json +19 -0
- package/test/utils.test.ts +73 -1
- package/test/utils_fixdata.test.ts +2 -1
- package/test/utils_removeColumns.test.ts +1 -1
- package/tsconfig.json +33 -15
- package/vite.config.ts +37 -0
- package/vitest.config.ts +26 -0
- package/.babelrc +0 -8
- package/.github/workflows/webpack.yml +0 -36
- package/bower.json +0 -32
- package/jest.config.ts +0 -203
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import
|
|
2
|
+
import { describe, expect, test, beforeEach, it, assert } from 'vitest'
|
|
3
3
|
|
|
4
4
|
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
5
5
|
|
|
@@ -12,7 +12,7 @@ describe('convert() API', function() {
|
|
|
12
12
|
|
|
13
13
|
document.body.innerHTML = '';
|
|
14
14
|
const element = document.createElement("div");
|
|
15
|
-
element.innerHTML =
|
|
15
|
+
element.innerHTML =
|
|
16
16
|
'<table id="sometable"><tr><th>first</th><th>second</th></tr><tr><td>1234</td><td>123.56</td></tr></table>' +
|
|
17
17
|
'<a id="anchor">Link</a>';
|
|
18
18
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
|
-
import
|
|
2
|
+
import { describe, expect, test, beforeEach, it, assert } from 'vitest'
|
|
3
3
|
|
|
4
|
+
import * as XLSX from 'xlsx';
|
|
4
5
|
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
5
6
|
|
|
6
7
|
|
|
@@ -12,7 +13,7 @@ describe('convert() API', function() {
|
|
|
12
13
|
|
|
13
14
|
document.body.innerHTML = '';
|
|
14
15
|
const element = document.createElement("div");
|
|
15
|
-
element.innerHTML =
|
|
16
|
+
element.innerHTML =
|
|
16
17
|
'<table id="sometable"><tr><th>first</th><th>second</th></tr><tr><td>1234</td><td>123.56</td></tr></table>' +
|
|
17
18
|
'<a id="anchor">Link</a>';
|
|
18
19
|
|
|
@@ -67,5 +68,96 @@ describe('convert() API', function() {
|
|
|
67
68
|
expect(anchor.href).toMatch(/blob:/);
|
|
68
69
|
});
|
|
69
70
|
});
|
|
71
|
+
|
|
72
|
+
describe('convert from HTML table with colspan and rowspan', function() {
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
window.URL.createObjectURL = () => "blob:fake_URL";
|
|
76
|
+
document.body.innerHTML = '';
|
|
77
|
+
const div = document.createElement('div');
|
|
78
|
+
div.innerHTML = '<a id="anchor">Link</a>';
|
|
79
|
+
document.body.appendChild(div);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
function buildWorksheet(tableHtml: string): XLSX.WorkSheet {
|
|
83
|
+
const div = document.createElement('div');
|
|
84
|
+
div.id = 'wrapper';
|
|
85
|
+
div.innerHTML = tableHtml;
|
|
86
|
+
document.body.appendChild(div);
|
|
87
|
+
|
|
88
|
+
const wbBinary = ExcellentExport.convert(
|
|
89
|
+
{ anchor: 'anchor', filename: 'test', format: 'xlsx' } as ConvertOptions,
|
|
90
|
+
[{ name: 'Sheet1', from: { table: div.querySelector('table') as HTMLTableElement } }] as SheetOptions[]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const wb = XLSX.read(wbBinary, { type: 'binary' });
|
|
94
|
+
return wb.Sheets['Sheet1'];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
test('should produce merged cells for colspan=2', function() {
|
|
98
|
+
const ws = buildWorksheet(
|
|
99
|
+
'<table>' +
|
|
100
|
+
'<tr><td colspan="2">Header</td></tr>' +
|
|
101
|
+
'<tr><td>A</td><td>B</td></tr>' +
|
|
102
|
+
'</table>'
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// Cell A1 should contain "Header"
|
|
106
|
+
expect(ws['A1'].v).toBe('Header');
|
|
107
|
+
// Merged region should cover A1:B1
|
|
108
|
+
expect(ws['!merges']).toBeDefined();
|
|
109
|
+
expect(ws['!merges']).toHaveLength(1);
|
|
110
|
+
expect(ws['!merges']![0]).toEqual({ s: { r: 0, c: 0 }, e: { r: 0, c: 1 } });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('should produce merged cells for rowspan=2', function() {
|
|
114
|
+
const ws = buildWorksheet(
|
|
115
|
+
'<table>' +
|
|
116
|
+
'<tr><td rowspan="2">Side</td><td>Top</td></tr>' +
|
|
117
|
+
'<tr><td>Bottom</td></tr>' +
|
|
118
|
+
'</table>'
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
expect(ws['A1'].v).toBe('Side');
|
|
122
|
+
expect(ws['B1'].v).toBe('Top');
|
|
123
|
+
expect(ws['B2'].v).toBe('Bottom');
|
|
124
|
+
expect(ws['!merges']).toBeDefined();
|
|
125
|
+
expect(ws['!merges']).toHaveLength(1);
|
|
126
|
+
expect(ws['!merges']![0]).toEqual({ s: { r: 0, c: 0 }, e: { r: 1, c: 0 } });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('should produce merged cells for combined colspan=2 rowspan=2', function() {
|
|
130
|
+
const ws = buildWorksheet(
|
|
131
|
+
'<table>' +
|
|
132
|
+
'<tr><td colspan="2" rowspan="2">Big</td><td>R0C2</td></tr>' +
|
|
133
|
+
'<tr><td>R1C2</td></tr>' +
|
|
134
|
+
'<tr><td>R2C0</td><td>R2C1</td><td>R2C2</td></tr>' +
|
|
135
|
+
'</table>'
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
expect(ws['A1'].v).toBe('Big');
|
|
139
|
+
expect(ws['C1'].v).toBe('R0C2');
|
|
140
|
+
expect(ws['C2'].v).toBe('R1C2');
|
|
141
|
+
expect(ws['A3'].v).toBe('R2C0');
|
|
142
|
+
expect(ws['!merges']).toBeDefined();
|
|
143
|
+
expect(ws['!merges']).toHaveLength(1);
|
|
144
|
+
expect(ws['!merges']![0]).toEqual({ s: { r: 0, c: 0 }, e: { r: 1, c: 1 } });
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('should produce no merges for a table without spans', function() {
|
|
148
|
+
const ws = buildWorksheet(
|
|
149
|
+
'<table>' +
|
|
150
|
+
'<tr><td>A</td><td>B</td></tr>' +
|
|
151
|
+
'<tr><td>C</td><td>D</td></tr>' +
|
|
152
|
+
'</table>'
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
expect(ws['A1'].v).toBe('A');
|
|
156
|
+
expect(ws['B2'].v).toBe('D');
|
|
157
|
+
// No merges
|
|
158
|
+
const merges = ws['!merges'];
|
|
159
|
+
expect(merges ?? []).toHaveLength(0);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
70
162
|
});
|
|
71
163
|
|
package/test/convert.format.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
4
|
-
import { PredefinedFormat } from '../src/format';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
describe('convert() API with column formats', function() {
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
window.URL.createObjectURL = () => "blob:fake_URL";
|
|
11
|
-
|
|
12
|
-
document.body.innerHTML = '';
|
|
13
|
-
const element = document.createElement("div");
|
|
14
|
-
element.innerHTML = '<a id="anchor">Link</a>';
|
|
15
|
-
|
|
16
|
-
document.body.appendChild(element);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
it('should create a XLSX with types', function() {
|
|
20
|
-
const options = {
|
|
21
|
-
anchor: 'anchor',
|
|
22
|
-
filename: 'data_from_array',
|
|
23
|
-
format: 'xlsx'
|
|
24
|
-
} as ConvertOptions;
|
|
25
|
-
|
|
26
|
-
const sheets = [
|
|
27
|
-
{
|
|
28
|
-
name: 'People',
|
|
29
|
-
from: {
|
|
30
|
-
array: [
|
|
31
|
-
["ID", "Name", "Birthdate", "Active", "Salary"],
|
|
32
|
-
[11, "John", "1980-01-01", true, 1000.98],
|
|
33
|
-
[22, "Mary", "1985-02-02", false, 2000.88],
|
|
34
|
-
[33, "Peter", "1990-03-03", true, 3000.32],
|
|
35
|
-
]
|
|
36
|
-
},
|
|
37
|
-
formats: [
|
|
38
|
-
{ range: 'A2:A10', format: PredefinedFormat.INTEGER },
|
|
39
|
-
{ range: 'C2:C10', format: PredefinedFormat.DATE },
|
|
40
|
-
{ range: 'D2:D10', format: PredefinedFormat.BOOLEAN },
|
|
41
|
-
{ range: 'E2:E10', format: PredefinedFormat.DECIMAL },
|
|
42
|
-
]
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
] as SheetOptions[];
|
|
46
|
-
|
|
47
|
-
const workbook = ExcellentExport.convert(options, sheets);
|
|
48
|
-
|
|
49
|
-
assert.ok(workbook, 'Result must not be null');
|
|
50
|
-
|
|
51
|
-
const anchor = document.getElementById('anchor') as HTMLAnchorElement;
|
|
52
|
-
assert.ok(anchor.href, 'Element must have href');
|
|
53
|
-
assert.ok(anchor.href.indexOf('blob:') === 0, 'Element href myst be a blob:');
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
1
|
+
import { describe, expect, test, beforeEach, it, assert } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
4
|
+
import { PredefinedFormat } from '../src/format';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
describe('convert() API with column formats', function() {
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
window.URL.createObjectURL = () => "blob:fake_URL";
|
|
11
|
+
|
|
12
|
+
document.body.innerHTML = '';
|
|
13
|
+
const element = document.createElement("div");
|
|
14
|
+
element.innerHTML = '<a id="anchor">Link</a>';
|
|
15
|
+
|
|
16
|
+
document.body.appendChild(element);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should create a XLSX with types', function() {
|
|
20
|
+
const options = {
|
|
21
|
+
anchor: 'anchor',
|
|
22
|
+
filename: 'data_from_array',
|
|
23
|
+
format: 'xlsx'
|
|
24
|
+
} as ConvertOptions;
|
|
25
|
+
|
|
26
|
+
const sheets = [
|
|
27
|
+
{
|
|
28
|
+
name: 'People',
|
|
29
|
+
from: {
|
|
30
|
+
array: [
|
|
31
|
+
["ID", "Name", "Birthdate", "Active", "Salary"],
|
|
32
|
+
[11, "John", "1980-01-01", true, 1000.98],
|
|
33
|
+
[22, "Mary", "1985-02-02", false, 2000.88],
|
|
34
|
+
[33, "Peter", "1990-03-03", true, 3000.32],
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
formats: [
|
|
38
|
+
{ range: 'A2:A10', format: PredefinedFormat.INTEGER },
|
|
39
|
+
{ range: 'C2:C10', format: PredefinedFormat.DATE },
|
|
40
|
+
{ range: 'D2:D10', format: PredefinedFormat.BOOLEAN },
|
|
41
|
+
{ range: 'E2:E10', format: PredefinedFormat.DECIMAL },
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
] as SheetOptions[];
|
|
46
|
+
|
|
47
|
+
const workbook = ExcellentExport.convert(options, sheets);
|
|
48
|
+
|
|
49
|
+
assert.ok(workbook, 'Result must not be null');
|
|
50
|
+
|
|
51
|
+
const anchor = document.getElementById('anchor') as HTMLAnchorElement;
|
|
52
|
+
assert.ok(anchor.href, 'Element must have href');
|
|
53
|
+
assert.ok(anchor.href.indexOf('blob:') === 0, 'Element href myst be a blob:');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
package/test/convert.test.ts
CHANGED
|
@@ -1,59 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
element
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
[
|
|
33
|
-
['
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
[
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
assert.ok(anchor.href
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
});
|
|
59
|
-
|
|
1
|
+
import { describe, expect, test, beforeEach, it, assert } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
describe('convert() API', function() {
|
|
8
|
+
describe('convert from array', function() {
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
window.URL.createObjectURL = () => "blob:fake_URL";
|
|
12
|
+
|
|
13
|
+
document.body.innerHTML = '';
|
|
14
|
+
const element = document.createElement("div");
|
|
15
|
+
element.innerHTML = '<a id="anchor">Link</a>';
|
|
16
|
+
|
|
17
|
+
document.body.appendChild(element);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should create a XLSX from array', function() {
|
|
21
|
+
const options = {
|
|
22
|
+
anchor: 'anchor',
|
|
23
|
+
filename: 'data_from_array',
|
|
24
|
+
format: 'xlsx'
|
|
25
|
+
} as ConvertOptions;
|
|
26
|
+
|
|
27
|
+
const sheets = [
|
|
28
|
+
{
|
|
29
|
+
name: 'Sheet Name Here 1',
|
|
30
|
+
from: {
|
|
31
|
+
array: [
|
|
32
|
+
[1, 2, 3],
|
|
33
|
+
['hello', '2200', 'bye'],
|
|
34
|
+
['quo"te', 'dobl"e qu"ote', 'singl\'e quote']
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'Sheet Number 2',
|
|
40
|
+
from: {
|
|
41
|
+
array: [
|
|
42
|
+
[6666, 7777, 8888],
|
|
43
|
+
['lorem', 'ipsum', 'dolor']
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
] as SheetOptions[];
|
|
49
|
+
|
|
50
|
+
const workbook = ExcellentExport.convert(options, sheets);
|
|
51
|
+
|
|
52
|
+
assert.ok(workbook, 'Result must not be null');
|
|
53
|
+
|
|
54
|
+
const anchor = document.getElementById('anchor') as HTMLAnchorElement;
|
|
55
|
+
assert.ok(anchor.href, 'Element must have href');
|
|
56
|
+
assert.ok(anchor.href.indexOf('blob:') === 0, 'Element href myst be a blob:');
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
package/test/fixdata.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import { describe, expect, test, beforeEach, it, assert } from 'vitest'
|
|
2
2
|
|
|
3
3
|
import ExcellentExport, { ConvertOptions } from '../src/excellentexport';
|
|
4
4
|
|
|
@@ -29,7 +29,7 @@ describe('Fix data', function() {
|
|
|
29
29
|
['hello', '<td>hello</td>', 'bye'],
|
|
30
30
|
]
|
|
31
31
|
},
|
|
32
|
-
fixValue: (value, row, col) => {
|
|
32
|
+
fixValue: (value: any, row: number, col: number) => {
|
|
33
33
|
let v = value.replace(/<br>/gi, "\n");
|
|
34
34
|
let strippedString = v.replace(/(<([^>]+)>)/gi, "");
|
|
35
35
|
return strippedString;
|
|
@@ -56,9 +56,9 @@ describe('Fix data', function() {
|
|
|
56
56
|
['hello', '<td>hello</td>', 'bye'],
|
|
57
57
|
]
|
|
58
58
|
},
|
|
59
|
-
|
|
60
|
-
return array.map(r => {
|
|
61
|
-
return r.map(v => {
|
|
59
|
+
fixArray: (array: any[][]) => {
|
|
60
|
+
return array.map((r: any[]) => {
|
|
61
|
+
return r.map((v: any) => {
|
|
62
62
|
return "fixed-" + v;
|
|
63
63
|
})
|
|
64
64
|
});
|
package/test/negative.test.ts
CHANGED
|
@@ -1,129 +1,130 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
element
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
});
|
|
129
|
-
|
|
1
|
+
|
|
2
|
+
import { describe, expect, test, beforeEach, it, assert } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import ExcellentExport, { ConvertOptions, SheetOptions } from '../src/excellentexport';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
describe('convert() API', function() {
|
|
8
|
+
describe('Negative tests', function() {
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
window.URL.createObjectURL = () => "blob:fake_URL";
|
|
12
|
+
|
|
13
|
+
const element = document.createElement("div");
|
|
14
|
+
element.innerHTML = '<a id="anchor">Link</a>';
|
|
15
|
+
|
|
16
|
+
document.body.appendChild(element);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should fail if CSV has more than one sheet', function() {
|
|
20
|
+
const options = {
|
|
21
|
+
anchor: 'anchor',
|
|
22
|
+
filename: 'data_from_array',
|
|
23
|
+
format: 'csv'
|
|
24
|
+
} as ConvertOptions;
|
|
25
|
+
|
|
26
|
+
const sheets = [{
|
|
27
|
+
name: 'Sheet Name Here 1',
|
|
28
|
+
from: {}
|
|
29
|
+
}, {
|
|
30
|
+
name: 'Sheet Number 2',
|
|
31
|
+
from: {}
|
|
32
|
+
}];
|
|
33
|
+
|
|
34
|
+
assert.throws(() => {
|
|
35
|
+
ExcellentExport.convert(options, sheets)
|
|
36
|
+
}, Error);
|
|
37
|
+
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should fail if sheet does not have name', function() {
|
|
41
|
+
const options = {
|
|
42
|
+
anchor: 'anchor',
|
|
43
|
+
filename: 'data_from_array',
|
|
44
|
+
format: 'csv'
|
|
45
|
+
} as ConvertOptions;
|
|
46
|
+
|
|
47
|
+
const sheets = [{
|
|
48
|
+
// name: 'Sheet Name Here 1',
|
|
49
|
+
from: {}
|
|
50
|
+
}] as SheetOptions[];
|
|
51
|
+
|
|
52
|
+
assert.throws(() => {
|
|
53
|
+
ExcellentExport.convert(options, sheets)
|
|
54
|
+
}, Error);
|
|
55
|
+
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should fail if sheet does not have data', function() {
|
|
59
|
+
const options = {
|
|
60
|
+
anchor: 'anchor',
|
|
61
|
+
filename: 'data_from_array',
|
|
62
|
+
format: 'csv'
|
|
63
|
+
} as ConvertOptions;
|
|
64
|
+
|
|
65
|
+
const sheets = [{
|
|
66
|
+
name: 'Sheet Name Here 1',
|
|
67
|
+
// from: {}
|
|
68
|
+
}] as SheetOptions[];
|
|
69
|
+
|
|
70
|
+
assert.throws(() => {
|
|
71
|
+
ExcellentExport.convert(options, sheets)
|
|
72
|
+
}, Error);
|
|
73
|
+
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should fail if there is not format defined', function() {
|
|
77
|
+
const options = {
|
|
78
|
+
anchor: 'anchor',
|
|
79
|
+
filename: 'data_from_array',
|
|
80
|
+
//format: 'csv'
|
|
81
|
+
} as ConvertOptions;
|
|
82
|
+
|
|
83
|
+
const sheets = [{
|
|
84
|
+
name: 'Sheet Name Here 1',
|
|
85
|
+
from: {}
|
|
86
|
+
}];
|
|
87
|
+
|
|
88
|
+
assert.throws(() => {
|
|
89
|
+
ExcellentExport.convert(options, sheets)
|
|
90
|
+
}, Error);
|
|
91
|
+
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should fail if anchor is not defined/valid', function() {
|
|
95
|
+
const options = {
|
|
96
|
+
anchor: 'anchor1235d5d5d5d_invalid',
|
|
97
|
+
filename: 'data_from_array',
|
|
98
|
+
format: 'csv'
|
|
99
|
+
} as ConvertOptions;
|
|
100
|
+
|
|
101
|
+
const sheets = [{
|
|
102
|
+
name: 'Sheet Name Here 1',
|
|
103
|
+
from: {}
|
|
104
|
+
}];
|
|
105
|
+
|
|
106
|
+
assert.throws(() => {
|
|
107
|
+
ExcellentExport.convert(options, sheets)
|
|
108
|
+
}, Error);
|
|
109
|
+
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should fail if no anchor and not openAsDownload', function() {
|
|
113
|
+
const options = {
|
|
114
|
+
filename: 'data_from_array',
|
|
115
|
+
format: 'csv'
|
|
116
|
+
} as ConvertOptions;
|
|
117
|
+
|
|
118
|
+
const sheets = [{
|
|
119
|
+
name: 'Sheet Name Here 1',
|
|
120
|
+
from: {}
|
|
121
|
+
}];
|
|
122
|
+
|
|
123
|
+
assert.throws(() => {
|
|
124
|
+
ExcellentExport.convert(options, sheets)
|
|
125
|
+
}, Error);
|
|
126
|
+
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|