read-excel-file 5.4.0 → 5.4.3
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/CHANGELOG.md +7 -0
- package/README.md +46 -0
- package/bundle/read-excel-file.min.js +1 -1
- package/bundle/read-excel-file.min.js.map +1 -1
- package/commonjs/read/schema/convertToJson.js +7 -5
- package/commonjs/read/schema/convertToJson.js.map +1 -1
- package/commonjs/read/schema/convertToJson.test.js.map +1 -1
- package/index.d.ts +4 -1
- package/modules/read/schema/convertToJson.js +7 -5
- package/modules/read/schema/convertToJson.js.map +1 -1
- package/modules/read/schema/convertToJson.test.js.map +1 -1
- package/node/index.d.ts +4 -1
- package/package.json +1 -1
- package/types.d.ts +2 -0
- package/web-worker/index.d.ts +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
5.5.0 / 20.07.2022
|
|
2
|
+
==================
|
|
3
|
+
|
|
4
|
+
* [Added](https://gitlab.com/catamphetamine/read-excel-file/-/issues/7) `ignoreEmptyRows: false` option when parsing using a `schema`.
|
|
5
|
+
|
|
6
|
+
* Changed `errors.row` property when parsing using a `schema`: from "object number" to "spreadsheet row number". Example: was `1` for the first row of data, now is `2` for the first row of data (because `1` now is the header row).
|
|
7
|
+
|
|
1
8
|
5.4.0 / 04.07.2022
|
|
2
9
|
==================
|
|
3
10
|
|
package/README.md
CHANGED
|
@@ -115,6 +115,19 @@ Each property of a JSON object should be described by an "entry" in the `schema`
|
|
|
115
115
|
|
|
116
116
|
Sidenote: When converting cell values to object properties, by default, it skips all `null` values (skips all empty cells). That's for simplicity. In some edge cases though, it may be required to keep all `null` values for all the empty cells. For example, that's the case when updating data in an SQL database from an XLSX spreadsheet using Sequelize ORM library that requires a property to explicitly be `null` in order to clear it during an `UPDATE` operation. To keep all `null` values, pass `includeNullValues: true` option when calling `readXlsxFile()`.
|
|
117
117
|
|
|
118
|
+
#### `errors`
|
|
119
|
+
|
|
120
|
+
If there were any errors while converting spreadsheet data to JSON objects, the `errors` property returned from the function will be a non-empty array. An element of the `errors` property contains properties:
|
|
121
|
+
|
|
122
|
+
* `error: string` — The error code. Examples: `"required"`, `"invalid"`.
|
|
123
|
+
* If a custom `validate()` function is defined and it throws a `new Error(message)` then the `error` property will be the same as the `message` value.
|
|
124
|
+
* If a custom `type()` function is defined and it throws a `new Error(message)` then the `error` property will be the same as the `message` value.
|
|
125
|
+
* `reason?: string` — An optional secondary error code providing more details about the error. Currently, it's only returned for "built-in" `type`s. Example: `{ error: "invalid", reason: "not_a_number" }` for `type: Number` means that "the cell value is _invalid_ because it's _not a number_".
|
|
126
|
+
* `row: number` — The row number in the original file. `1` means the first row, etc.
|
|
127
|
+
* `column: string` — The column title.
|
|
128
|
+
* `value?: any` — The cell value.
|
|
129
|
+
* `type?: any` — The schema `type` for this column.
|
|
130
|
+
|
|
118
131
|
#### An example of using a `schema`
|
|
119
132
|
|
|
120
133
|
```js
|
|
@@ -223,6 +236,21 @@ readXlsxFile(file, { schema }).then(({ rows, errors }) => {
|
|
|
223
236
|
|
|
224
237
|
<!-- A schema entry for a column may also define an optional `validate(value)` function for validating the parsed value: in that case, it must `throw` an `Error` if the `value` is invalid. The `validate(value)` function is only called when `value` is not empty (not `null` / `undefined`). -->
|
|
225
238
|
|
|
239
|
+
<details>
|
|
240
|
+
<summary><strong>Ignoring empty rows</strong>.</summary>
|
|
241
|
+
|
|
242
|
+
#####
|
|
243
|
+
|
|
244
|
+
By default, it ignores any empty rows. To disable that behavior, pass `ignoreEmptyRows: false` option.
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
readXlsxFile(file, {
|
|
248
|
+
schema,
|
|
249
|
+
ignoreEmptyRows: false
|
|
250
|
+
})
|
|
251
|
+
```
|
|
252
|
+
</details>
|
|
253
|
+
|
|
226
254
|
<details>
|
|
227
255
|
<summary>How to fix spreadsheet data before <code>schema</code> parsing. For example, <strong>how to ignore empty rows</strong>.</summary>
|
|
228
256
|
|
|
@@ -379,6 +407,24 @@ By default, it automatically trims all string values. To disable this feature, p
|
|
|
379
407
|
readXlsxFile(file, { trim: false })
|
|
380
408
|
```
|
|
381
409
|
|
|
410
|
+
## Transform
|
|
411
|
+
|
|
412
|
+
Sometimes, a spreadsheet doesn't exactly have the structure required by this library's `schema` parsing feature: for example, it may be missing a header row, or contain some purely presentational / empty / "garbage" rows that should be removed. To fix that, one could pass an optional `transformData(data)` function that would modify the spreadsheet contents as required.
|
|
413
|
+
|
|
414
|
+
```js
|
|
415
|
+
readXlsxFile(file, {
|
|
416
|
+
schema,
|
|
417
|
+
transformData(data) {
|
|
418
|
+
// Add a missing header row.
|
|
419
|
+
return [['ID', 'NAME', ...]].concat(data)
|
|
420
|
+
// Remove empty rows.
|
|
421
|
+
return data.filter(row => row.filter(column => column !== null).length > 0)
|
|
422
|
+
}
|
|
423
|
+
})
|
|
424
|
+
```
|
|
425
|
+
</details>
|
|
426
|
+
|
|
427
|
+
|
|
382
428
|
## Limitations
|
|
383
429
|
|
|
384
430
|
### Performance
|