1
1
import { test } from "tap" ;
2
2
import { Workbook } from "../src/xlsx.js" ;
3
+ import ExcelJS from "exceljs" ;
3
4
4
- function mockWorkbook ( contents , overrides = { } ) {
5
- return {
6
- worksheets : Object . keys ( contents ) . map ( ( name ) => ( { name} ) ) ,
7
- getWorksheet ( name ) {
8
- const _rows = contents [ name ] ;
9
- return Object . assign (
10
- {
11
- _rows : _rows . map ( ( row ) => ( {
12
- _cells : row . map ( ( cell ) => ( { value : cell } ) ) ,
13
- hasValues : ! ! row . length ,
14
- } ) ) ,
15
- rowCount : _rows . length ,
16
- columnCount : Math . max ( ..._rows . map ( ( r ) => r . length ) ) ,
17
- } ,
18
- overrides
19
- ) ;
20
- } ,
21
- } ;
5
+ function exceljs ( contents ) {
6
+ const workbook = new ExcelJS . Workbook ( ) ;
7
+ for ( const [ sheet , rows ] of Object . entries ( contents ) ) {
8
+ const ws = workbook . addWorksheet ( sheet ) ;
9
+ for ( const row of rows ) ws . addRow ( row ) ;
10
+ }
11
+ return workbook ;
22
12
}
23
13
24
14
test ( "FileAttachment.xlsx reads sheet names" , ( t ) => {
25
- const workbook = new Workbook ( mockWorkbook ( { Sheet1 : [ ] } ) ) ;
15
+ const workbook = new Workbook ( exceljs ( { Sheet1 : [ ] } ) ) ;
26
16
t . same ( workbook . sheetNames , [ "Sheet1" ] ) ;
27
17
t . end ( ) ;
28
18
} ) ;
29
19
30
20
test ( "FileAttachment.xlsx sheet(name) throws on unknown sheet name" , ( t ) => {
31
- const workbook = new Workbook ( mockWorkbook ( { Sheet1 : [ ] } ) ) ;
21
+ const workbook = new Workbook ( exceljs ( { Sheet1 : [ ] } ) ) ;
32
22
t . throws ( ( ) => workbook . sheet ( "bad" ) ) ;
33
23
t . end ( ) ;
34
24
} ) ;
35
25
36
26
test ( "FileAttachment.xlsx reads sheets" , ( t ) => {
37
27
const workbook = new Workbook (
38
- mockWorkbook ( {
28
+ exceljs ( {
39
29
Sheet1 : [
40
30
[ "one" , "two" , "three" ] ,
41
31
[ 1 , 2 , 3 ] ,
@@ -50,13 +40,15 @@ test("FileAttachment.xlsx reads sheets", (t) => {
50
40
{ A : "one" , B : "two" , C : "three" } ,
51
41
{ A : 1 , B : 2 , C : 3 } ,
52
42
] ) ;
43
+ t . equal ( workbook . sheet ( 0 ) [ 0 ] [ "#" ] , 1 ) ;
44
+ t . equal ( workbook . sheet ( 0 ) [ 1 ] [ "#" ] , 2 ) ;
53
45
t . end ( ) ;
54
46
} ) ;
55
47
56
48
test ( "FileAttachment.xlsx reads sheets with different types" , ( t ) => {
57
49
t . same (
58
50
new Workbook (
59
- mockWorkbook ( {
51
+ exceljs ( {
60
52
Sheet1 : [
61
53
[ ] ,
62
54
[ null , undefined ] ,
@@ -79,7 +71,7 @@ test("FileAttachment.xlsx reads sheets with different types", (t) => {
79
71
) ;
80
72
t . same (
81
73
new Workbook (
82
- mockWorkbook ( {
74
+ exceljs ( {
83
75
Sheet1 : [
84
76
[
85
77
{ richText : [ { text : "two" } , { text : "three" } ] } , // A
@@ -112,7 +104,7 @@ test("FileAttachment.xlsx reads sheets with different types", (t) => {
112
104
) ;
113
105
t . same (
114
106
new Workbook (
115
- mockWorkbook ( {
107
+ exceljs ( {
116
108
Sheet1 : [
117
109
[
118
110
{ formula : "=B2*5" , result : 10 } ,
@@ -131,7 +123,7 @@ test("FileAttachment.xlsx reads sheets with different types", (t) => {
131
123
132
124
test ( "FileAttachment.xlsx reads sheets with headers" , ( t ) => {
133
125
const workbook = new Workbook (
134
- mockWorkbook ( {
126
+ exceljs ( {
135
127
Sheet1 : [
136
128
[ null , "one" , "one" , "two" , "A" , "0" ] ,
137
129
[ 1 , null , 3 , 4 , 5 , "zero" ] ,
@@ -156,9 +148,10 @@ test("FileAttachment.xlsx reads sheets with headers", (t) => {
156
148
} ) ;
157
149
158
150
test ( "FileAttachment.xlsx throws on invalid ranges" , ( t ) => {
159
- const workbook = new Workbook ( mockWorkbook ( { Sheet1 : [ ] } ) ) ;
151
+ const workbook = new Workbook ( exceljs ( { Sheet1 : [ ] } ) ) ;
160
152
const malformed = new Error ( "Malformed range specifier" ) ;
161
153
154
+ t . throws ( ( ) => t . same ( workbook . sheet ( 0 , { range : 0 } ) ) , malformed ) ;
162
155
t . throws ( ( ) => t . same ( workbook . sheet ( 0 , { range : "" } ) ) , malformed ) ;
163
156
t . throws ( ( ) => t . same ( workbook . sheet ( 0 , { range : "-:" } ) ) , malformed ) ;
164
157
t . throws ( ( ) => t . same ( workbook . sheet ( 0 , { range : " :" } ) ) , malformed ) ;
@@ -174,7 +167,7 @@ test("FileAttachment.xlsx throws on invalid ranges", (t) => {
174
167
175
168
test ( "FileAttachment.xlsx reads sheet ranges" , ( t ) => {
176
169
const workbook = new Workbook (
177
- mockWorkbook ( {
170
+ exceljs ( {
178
171
Sheet1 : [
179
172
[ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ,
180
173
[ 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 ] ,
@@ -248,31 +241,22 @@ test("FileAttachment.xlsx reads sheet ranges", (t) => {
248
241
t . end ( ) ;
249
242
} ) ;
250
243
251
- test ( "FileAttachment.xlsx throws on unknown range specifier" , ( t ) => {
252
- const workbook = new Workbook ( mockWorkbook ( { Sheet1 : [ ] } ) ) ;
253
- t . throws ( ( ) => workbook . sheet ( 0 , { range : 0 } ) ) ;
254
- t . end ( ) ;
255
- } ) ;
256
-
257
244
test ( "FileAttachment.xlsx derives column names such as A AA AAA…" , ( t ) => {
258
- const l0 = 26 * 26 * 26 + 26 * 26 + 26 ;
245
+ const l0 = 26 * 26 * 23 ;
259
246
const workbook = new Workbook (
260
- mockWorkbook ( {
247
+ exceljs ( {
261
248
Sheet1 : [ Array . from ( { length : l0 } ) . fill ( 1 ) ] ,
262
249
} )
263
250
) ;
264
251
t . same (
265
- workbook . sheet ( 0 , { headers : false } ) . columns . filter ( ( d ) => d . match ( / ^ A * $ / ) ) ,
252
+ workbook . sheet ( 0 ) . columns . filter ( ( d ) => d . match ( / ^ A + $ / ) ) ,
266
253
[ "A" , "AA" , "AAA" ]
267
254
) ;
268
- const workbook1 = new Workbook (
269
- mockWorkbook ( {
270
- Sheet1 : [ Array . from ( { length : l0 + 1 } ) . fill ( 1 ) ] ,
271
- } )
272
- ) ;
273
- t . same (
274
- workbook1 . sheet ( 0 , { headers : false } ) . columns . filter ( ( d ) => d . match ( / ^ A * $ / ) ) ,
275
- [ "A" , "AA" , "AAA" , "AAAA" ]
276
- ) ;
255
+ t . end ( ) ;
256
+ } ) ;
257
+
258
+ test ( "FileAttachment.sheet headers protects __proto__ of row objects" , ( t ) => {
259
+ const workbook = new Workbook ( exceljs ( { Sheet1 : [ [ "__proto__" ] , [ { a : 1 } ] ] } ) ) ;
260
+ t . not ( workbook . sheet ( 0 , { headers : true } ) [ 0 ] . a , 1 ) ;
277
261
t . end ( ) ;
278
262
} ) ;
0 commit comments