read-excel-file 8.0.1 → 8.0.2
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 +51 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# `read-excel-file`
|
|
2
2
|
|
|
3
|
-
Read `.xlsx` files in a browser or
|
|
3
|
+
Read `.xlsx` files in a browser or Node.js.
|
|
4
4
|
|
|
5
5
|
It also supports parsing spreadsheet rows into JSON objects using a [schema](#schema).
|
|
6
6
|
|
|
@@ -59,33 +59,42 @@ Also check out [`write-excel-file`](https://www.npmjs.com/package/write-excel-fi
|
|
|
59
59
|
* `ParsedObjectsResult` → `ParseDataResult`
|
|
60
60
|
</details>
|
|
61
61
|
|
|
62
|
-
## Performance
|
|
63
|
-
|
|
64
|
-
Here're the results of reading [sample `.xlsx` files](https://examplefile.com/document/xlsx) of different size:
|
|
65
|
-
|
|
66
|
-
|File Size| Browser | Node.js |
|
|
67
|
-
|---------|---------|-----------|
|
|
68
|
-
| 1 MB | 0.2 sec.| 0.25 sec. |
|
|
69
|
-
| 10 MB | 1.5 sec.| 2 sec. |
|
|
70
|
-
| 50 MB | 8.5 sec.| 14 sec. |
|
|
71
|
-
|
|
72
62
|
## Install
|
|
73
63
|
|
|
74
64
|
```js
|
|
75
65
|
npm install read-excel-file --save
|
|
76
66
|
```
|
|
77
67
|
|
|
78
|
-
Alternatively,
|
|
68
|
+
Alternatively, it could be included on a web page [directly](#cdn) via a `<script/>` tag.
|
|
79
69
|
|
|
80
70
|
## Use
|
|
81
71
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
72
|
+
If your `.xlsx` file only has a single "sheet", or if you only care for a single "sheet", or if you don't know or care what a "sheet" is, use `readSheet()` function.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
import { readSheet } from 'read-excel-file/node'
|
|
76
|
+
|
|
77
|
+
await readSheet(file)
|
|
78
|
+
|
|
79
|
+
// Returns
|
|
80
|
+
[
|
|
81
|
+
['John Smith',35,true,...],
|
|
82
|
+
['Kate Brown',28,false,...],
|
|
83
|
+
...
|
|
84
|
+
]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
It resolves to an array of rows. Each row is an array of values — `string`, `number`, `boolean` or `Date`.
|
|
88
|
+
|
|
89
|
+
<!-- It's same as the default exported function shown above with the only difference that it returns just `data` instead of `[{ name: 'Sheet1', data }]`, so it's just a bit simpler to use. It has an optional second argument — `sheet` — which could be a sheet number (starting from `1`) or a sheet name. By default, it reads the first sheet. -->
|
|
90
|
+
|
|
91
|
+
And it has an optional second argument — `sheet` — which could be a sheet number (starting from `1`) or a sheet name. By default, it reads the first sheet.
|
|
92
|
+
|
|
93
|
+
But if you need to read all "sheets" for some reason, use the default exported function which resolves to an array of "sheets".
|
|
87
94
|
|
|
88
95
|
```js
|
|
96
|
+
import readExcelFile from 'read-excel-file/node'
|
|
97
|
+
|
|
89
98
|
await readExcelFile(file)
|
|
90
99
|
|
|
91
100
|
// Returns
|
|
@@ -102,20 +111,13 @@ await readExcelFile(file)
|
|
|
102
111
|
}]
|
|
103
112
|
```
|
|
104
113
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// Returns
|
|
111
|
-
[
|
|
112
|
-
['John Smith',35,true,...],
|
|
113
|
-
['Kate Brown',28,false,...],
|
|
114
|
-
...
|
|
115
|
-
]
|
|
116
|
-
```
|
|
114
|
+
At least one "sheet" always exists. Each "sheet" is an object with properties:
|
|
115
|
+
* `sheet` — Sheet name.
|
|
116
|
+
* Example: `"Sheet1"`
|
|
117
|
+
* `data` — Sheet data. An array of rows. Each row is an array of values — `string`, `number`, `boolean` or `Date`.
|
|
118
|
+
* Example: `[ ['John Smith',35,true,...], ['Kate Brown',28,false,...], ... ]`
|
|
117
119
|
|
|
118
|
-
|
|
120
|
+
This package provides a separate `import` path for each different environment, as described below.
|
|
119
121
|
|
|
120
122
|
### Browser
|
|
121
123
|
|
|
@@ -259,6 +261,16 @@ readExcelFile(file, {
|
|
|
259
261
|
|
|
260
262
|
This package doesn't support reading cells that use formulas to calculate the value: `SUM`, `AVERAGE`, etc.
|
|
261
263
|
|
|
264
|
+
## Performance
|
|
265
|
+
|
|
266
|
+
Here're the results of reading [sample `.xlsx` files](https://examplefile.com/document/xlsx) of different size:
|
|
267
|
+
|
|
268
|
+
|File Size| Browser | Node.js |
|
|
269
|
+
|---------|---------|-----------|
|
|
270
|
+
| 1 MB | 0.2 sec.| 0.25 sec. |
|
|
271
|
+
| 10 MB | 1.5 sec.| 2 sec. |
|
|
272
|
+
| 50 MB | 8.5 sec.| 14 sec. |
|
|
273
|
+
|
|
262
274
|
## Schema
|
|
263
275
|
|
|
264
276
|
Oftentimes, the task is not just to read the "raw" spreadsheet data but also to convert each row of that data to a JSON object having a certain structure. Because it's such a common task, this package exports a named function `parseData(data, schema)` which does exactly that. It parses sheet data into an array of JSON objects according to a pre-defined `schema` which describes how should a row of data be converted to a JSON object.
|
|
@@ -372,8 +384,8 @@ const schema = {
|
|
|
372
384
|
contact: {
|
|
373
385
|
column: 'CONTACT',
|
|
374
386
|
required: true,
|
|
375
|
-
// A custom `type`
|
|
376
|
-
// It will
|
|
387
|
+
// A custom `type` parsing function can be specified.
|
|
388
|
+
// It will parse the cell value if it's not empty.
|
|
377
389
|
type: (value) => {
|
|
378
390
|
const number = parsePhoneNumber(value)
|
|
379
391
|
if (!number) {
|
|
@@ -395,12 +407,14 @@ const schema = {
|
|
|
395
407
|
|
|
396
408
|
const data = await readSheet(file)
|
|
397
409
|
|
|
398
|
-
const
|
|
410
|
+
const results = parseData(data, schema)
|
|
399
411
|
|
|
400
|
-
|
|
401
|
-
errors.length === 0
|
|
412
|
+
results.length === 1
|
|
402
413
|
|
|
403
|
-
|
|
414
|
+
// `errors` items have shape: `{ column, error, reason?, value?, type? }`.
|
|
415
|
+
results[0].errors === undefined
|
|
416
|
+
|
|
417
|
+
results[0].object === {
|
|
404
418
|
date: new Date(2018, 3 - 1, 24),
|
|
405
419
|
numberOfStudents: 10,
|
|
406
420
|
course: {
|
|
@@ -409,7 +423,7 @@ rows === [{
|
|
|
409
423
|
},
|
|
410
424
|
contact: '+11234567890',
|
|
411
425
|
status: 'SCHEDULED'
|
|
412
|
-
}
|
|
426
|
+
}
|
|
413
427
|
```
|
|
414
428
|
|
|
415
429
|
<!-- #### Schema: Tips and Features -->
|