read-excel-file 9.0.2 → 9.0.4
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 +64 -41
- package/README.md +98 -69
- package/browser/index.cjs +5 -5
- package/browser/index.d.ts +20 -20
- package/browser/index.js +5 -5
- package/commonjs/parseSheetData/InvalidError.js.map +1 -0
- package/commonjs/{parseData/parseData.js → parseSheetData/parseSheetData.js} +6 -6
- package/commonjs/parseSheetData/parseSheetData.js.map +1 -0
- package/commonjs/parseSheetData/parseSheetData.test.js.map +1 -0
- package/commonjs/parseSheetData/types/Boolean.js.map +1 -0
- package/commonjs/parseSheetData/types/Date.js.map +1 -0
- package/commonjs/parseSheetData/types/Number.js.map +1 -0
- package/commonjs/parseSheetData/types/String.js.map +1 -0
- package/commonjs/parseSheetData/types/additional/Email.js.map +1 -0
- package/commonjs/parseSheetData/types/additional/Email.test.js.map +1 -0
- package/commonjs/parseSheetData/types/additional/Integer.js.map +1 -0
- package/commonjs/parseSheetData/types/additional/Integer.test.js.map +1 -0
- package/commonjs/parseSheetData/types/additional/URL.js.map +1 -0
- package/commonjs/parseSheetData/types/additional/URL.test.js.map +1 -0
- package/modules/parseSheetData/InvalidError.js.map +1 -0
- package/modules/{parseData/parseData.js → parseSheetData/parseSheetData.js} +4 -4
- package/modules/parseSheetData/parseSheetData.js.map +1 -0
- package/modules/parseSheetData/parseSheetData.test.js.map +1 -0
- package/modules/parseSheetData/types/Boolean.js.map +1 -0
- package/modules/parseSheetData/types/Date.js.map +1 -0
- package/modules/parseSheetData/types/Number.js.map +1 -0
- package/modules/parseSheetData/types/String.js.map +1 -0
- package/modules/parseSheetData/types/additional/Email.js.map +1 -0
- package/modules/parseSheetData/types/additional/Email.test.js.map +1 -0
- package/modules/parseSheetData/types/additional/Integer.js.map +1 -0
- package/modules/parseSheetData/types/additional/Integer.test.js.map +1 -0
- package/modules/parseSheetData/types/additional/URL.js.map +1 -0
- package/modules/parseSheetData/types/additional/URL.test.js.map +1 -0
- package/node/index.cjs +5 -5
- package/node/index.d.ts +20 -20
- package/node/index.js +5 -5
- package/package.json +1 -1
- package/types/{parseData/parseData.d.ts → parseSheetData/parseSheetData.d.ts} +9 -9
- package/types/parseSheetData/parseSheetDataError.d.ts +292 -0
- package/types/{parseData/parseDataSchema.d.ts → parseSheetData/parseSheetDataSchema.d.ts} +2 -2
- package/types/{parseData/parseDataValueType.d.ts → parseSheetData/parseSheetDataValueType.d.ts} +7 -7
- package/universal/index.cjs +5 -5
- package/universal/index.d.ts +20 -20
- package/universal/index.js +5 -5
- package/web-worker/index.cjs +5 -5
- package/web-worker/index.d.ts +20 -20
- package/web-worker/index.js +5 -5
- package/commonjs/parseData/InvalidError.js.map +0 -1
- package/commonjs/parseData/parseData.js.map +0 -1
- package/commonjs/parseData/parseData.test.js.map +0 -1
- package/commonjs/parseData/types/Boolean.js.map +0 -1
- package/commonjs/parseData/types/Date.js.map +0 -1
- package/commonjs/parseData/types/Number.js.map +0 -1
- package/commonjs/parseData/types/String.js.map +0 -1
- package/commonjs/parseData/types/additional/Email.js.map +0 -1
- package/commonjs/parseData/types/additional/Email.test.js.map +0 -1
- package/commonjs/parseData/types/additional/Integer.js.map +0 -1
- package/commonjs/parseData/types/additional/Integer.test.js.map +0 -1
- package/commonjs/parseData/types/additional/URL.js.map +0 -1
- package/commonjs/parseData/types/additional/URL.test.js.map +0 -1
- package/modules/parseData/InvalidError.js.map +0 -1
- package/modules/parseData/parseData.js.map +0 -1
- package/modules/parseData/parseData.test.js.map +0 -1
- package/modules/parseData/types/Boolean.js.map +0 -1
- package/modules/parseData/types/Date.js.map +0 -1
- package/modules/parseData/types/Number.js.map +0 -1
- package/modules/parseData/types/String.js.map +0 -1
- package/modules/parseData/types/additional/Email.js.map +0 -1
- package/modules/parseData/types/additional/Email.test.js.map +0 -1
- package/modules/parseData/types/additional/Integer.js.map +0 -1
- package/modules/parseData/types/additional/Integer.test.js.map +0 -1
- package/modules/parseData/types/additional/URL.js.map +0 -1
- package/modules/parseData/types/additional/URL.test.js.map +0 -1
- package/types/parseData/parseDataError.d.ts +0 -292
- /package/commonjs/{parseData → parseSheetData}/InvalidError.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/Boolean.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/Date.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/Number.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/String.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/additional/Email.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/additional/Integer.js +0 -0
- /package/commonjs/{parseData → parseSheetData}/types/additional/URL.js +0 -0
- /package/modules/{parseData → parseSheetData}/InvalidError.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/Boolean.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/Date.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/Number.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/String.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/additional/Email.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/additional/Integer.js +0 -0
- /package/modules/{parseData → parseSheetData}/types/additional/URL.js +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,51 +1,74 @@
|
|
|
1
1
|
9.0.0 / 18.04.2026
|
|
2
2
|
==================
|
|
3
3
|
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
*
|
|
4
|
+
* If you were using `parseData()` function:
|
|
5
|
+
* Rewrote the code of the `parseData()` function and renamed it to `parseSheetData()`.
|
|
6
|
+
* The result of `parseSheetData()` function is now `{ errors, objects }`. If there're no errors, `errors` will be `undefined`. Otherwise, `errors` will be a non-empty array and `objects` will be `undefined`.
|
|
7
|
+
* Previously the result of `parseSheetData()` function was `[{ errors, object }, ...]`, i.e. the `errors` were split between each particular data row. Now the `errors` are combined for all data rows. The rationale is that it's simpler to handle the result of the function this way.
|
|
8
|
+
* Re-added `row: number` property to the `error` object.
|
|
9
|
+
* Renamed some of the exported TypeScript types:
|
|
10
|
+
* `ParseDataCustomType` → `ParseSheetDataCustomType`
|
|
11
|
+
* `ParseDataCustomTypeErrorMessage` → `ParseSheetDataCustomTypeErrorMessage`
|
|
12
|
+
* `ParseDataCustomTypeErrorReason` → `ParseSheetDataCustomTypeErrorReason`
|
|
13
|
+
* `ParseDataError` → `ParseSheetDataError`
|
|
14
|
+
* `ParseDataValueRequiredError` → `ParseSheetDataValueRequiredError`
|
|
15
|
+
* `ParseDataResult` → `ParseSheetDataResult`
|
|
16
|
+
* In a `schema`, a nested object could be declared as: `{ required: true/false, schema: { ... } }`. This is still true but the `required` flag is now only allowed to be either `undefined` or `false`, so `true` value is not allowed. The reason is quite simple. If a nested object as a whole is marked as `required: true`, and then it happens to be empty, a `"required"` error should be returned for it. But that error would also have to include a `column` title, and a nested object simply can't be pinned down to a single column in a sheet because it is by definition spread over multiple columns. So instead of marking a nested object as a whole with `required: true`, mark the specific required properties of it.
|
|
9
17
|
|
|
10
18
|
8.0.0 / 11.03.2026
|
|
11
19
|
==================
|
|
12
20
|
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* Removed `
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* `
|
|
46
|
-
* `
|
|
47
|
-
* `
|
|
48
|
-
|
|
21
|
+
* If you were using the default exported function:
|
|
22
|
+
* Renamed the default exported function to a named exported function `readSheet`.
|
|
23
|
+
* Old: `import readExcelFile from "read-excel-file/browser"`
|
|
24
|
+
* New: `import { readSheet } from "read-excel-file/browser"`
|
|
25
|
+
* And same for other exports like `"read-excel-file/node"`, etc.
|
|
26
|
+
* The default exported function now returns a different kind of result. Specifically, now it returns all available sheets — an array of objects: `[{ sheet: "Sheet 1", data: [['a1','b1','c1'],['a2','b2','c2']] }, ...]`.
|
|
27
|
+
* The default exported function used to return sheet names when passed `getSheets: true` parameter. Now, instead of that, the default exported function just returns all available sheets, from which one could get the sheet names.
|
|
28
|
+
|
|
29
|
+
* If you were using `readSheetNames()` function:
|
|
30
|
+
* Removed exported function `readSheetNames()`. Use the default exported function instead. The default exported function now returns all sheets.
|
|
31
|
+
|
|
32
|
+
* If you were using `parseExcelDate()` function:
|
|
33
|
+
* Removed exported function `parseExcelDate()` because there seems to be no need to have it exported.
|
|
34
|
+
|
|
35
|
+
* If you were using `schema` parameter:
|
|
36
|
+
* Removed `schema` parameter. Instead, use exported function `parseData(data, schema)` to map data to an array of objects.
|
|
37
|
+
* Old: `import readXlsxFile from "read-excel-file"` and then `const { rows, errors } = await readXlsxFile(..., { schema })`
|
|
38
|
+
* New: `import { readSheet, parseData } from "read-excel-file/browser"` and then `const result = parseData(await readSheet(...), schema)`
|
|
39
|
+
* The `result` of the function is an array where each element represents a "data row" and has shape `{ object, errors }`.
|
|
40
|
+
* Depending on whether there were any errors when parsing a given "data row", either `object` or `errors` property will be `undefined`.
|
|
41
|
+
* The `errors` don't have a `row` property anymore because it could be derived from "data row" number.
|
|
42
|
+
* In version `9.x`, the `row` property has been re-added, so consider migrating straight to `9.x`.
|
|
43
|
+
* In version `9.x`, the returned result of `parseData()` has been changed back to `{ errors, objects }`, so consider migrating straight to `9.x`. In that case, if there're no errors, `errors` will be `undefined`; otherwise, `errors` will be a non-empty array and `objects` will be `undefined`.
|
|
44
|
+
* Renamed some `schema`-related parameters:
|
|
45
|
+
* `schemaPropertyValueForMissingColumn` → `propertyValueWhenColumnIsMissing`
|
|
46
|
+
* `schemaPropertyValueForMissingValue` → `propertyValueWhenCellIsEmpty`
|
|
47
|
+
* `schemaPropertyShouldSkipRequiredValidationForMissingColumn` → (removed)
|
|
48
|
+
* `getEmptyObjectValue` → `transformEmptyObject`
|
|
49
|
+
* The leading `.` character is now removed from the `path` parameter.
|
|
50
|
+
* `getEmptyArrayValue` → `transformEmptyArray`
|
|
51
|
+
* The leading `.` character is now removed from the `path` parameter.
|
|
52
|
+
* Previously, when using a `schema` to parse comma-separated values, it used to ignore any commas that're surrounded by quotes, similar to how it's done in `.csv` files. Now it no longer does that.
|
|
53
|
+
* Previously, when using a `schema` to parse comma-separated values, it used to allow empty-string elements. Now it no longer does that and such empty-string elements will now result in an error with properties: `{ error: "invalid", reason: "syntax" }`.
|
|
54
|
+
* Previously, when using a `schema` to parse `type: Date` properties, it used to support both `Date` objects and numeric timestamps as the input data for the property value. In the latter case, it simply force-converted those numeric timestamps to corresponding `Date` objects. Now `parseData()` function no longer does that, and demands the input data for `type: Date` schema properties to only be `Date` objects, i.e. it shifts the responsibility to interpret date cell values correctly onto `readSheet()` and `readExcelFile()` functions. And I'd personally assume that in any real-world (i.e. non-contrived) scenario those functions would interpret date cell values correctly, so I personally don't consider this a "breaking change". Still, formally, it is a "breaking change" and therefore should be mentioned. So if, for some strange reason, those two functions happen to not recognize a date cell value correctly, `parseData()` function will return an error for such cell: `"not_a_date"`.
|
|
55
|
+
* Previously, when using a `schema` to parse sheet data, and a given row of data was completely empty, it didn't run any `required` property validations. Now it no longer does that and it will run all `required` property validations regardless of whether it's a completely empty row of data or not.
|
|
56
|
+
|
|
57
|
+
* If you were using `transformData` parameter:
|
|
58
|
+
* Removed `transformData` parameter because the `schema` parameter was extracted into a separate function called `parseData()`. Now, if required, a developer could transform the `data` manually and then pass it to `parseData()` function.
|
|
59
|
+
|
|
60
|
+
* If you were using `isColumnOriented` parameter:
|
|
61
|
+
* Removed `isColumnOriented` parameter because it seemed to be of no use.
|
|
62
|
+
|
|
63
|
+
* If you were using `ignoreEmptyRows` parameter:
|
|
64
|
+
* Removed `ignoreEmptyRows` parameter. Empty rows somewhere in the middle of a sheet are not ignored now. That doesn't concern empty rows at the end of a sheet though — those're still ignored.
|
|
65
|
+
|
|
66
|
+
* If you were using TypeScript:
|
|
67
|
+
* Renamed some of the exported types:
|
|
68
|
+
* `Type` → `ParseDataCustomType`
|
|
69
|
+
* `Error` or `SchemaParseCellValueError` → `ParseDataError`
|
|
70
|
+
* `CellValueRequiredError` → `ParseDataValueRequiredError`
|
|
71
|
+
* `ParsedObjectsResult` → `ParseDataResult`
|
|
49
72
|
|
|
50
73
|
7.0.1 / 04.03.2026
|
|
51
74
|
==================
|
package/README.md
CHANGED
|
@@ -24,42 +24,57 @@ Also check out [`write-excel-file`](https://www.npmjs.com/package/write-excel-fi
|
|
|
24
24
|
|
|
25
25
|
######
|
|
26
26
|
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* Removed `
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* `
|
|
60
|
-
* `
|
|
61
|
-
* `
|
|
62
|
-
|
|
27
|
+
* If you were using the default exported function:
|
|
28
|
+
* Renamed the default exported function to a named exported function `readSheet`.
|
|
29
|
+
* Old: `import readExcelFile from "read-excel-file/browser"`
|
|
30
|
+
* New: `import { readSheet } from "read-excel-file/browser"`
|
|
31
|
+
* And same for other exports like `"read-excel-file/node"`, etc.
|
|
32
|
+
* The default exported function now returns a different kind of result. Specifically, now it returns all available sheets — an array of objects: `[{ sheet: "Sheet 1", data: [['a1','b1','c1'],['a2','b2','c2']] }, ...]`.
|
|
33
|
+
* The default exported function used to return sheet names when passed `getSheets: true` parameter. Now, instead of that, the default exported function just returns all available sheets, from which one could get the sheet names.
|
|
34
|
+
|
|
35
|
+
* If you were using `readSheetNames()` function:
|
|
36
|
+
* Removed exported function `readSheetNames()`. Use the default exported function instead. The default exported function now returns all sheets.
|
|
37
|
+
|
|
38
|
+
* If you were using `parseExcelDate()` function:
|
|
39
|
+
* Removed exported function `parseExcelDate()` because there seems to be no need to have it exported.
|
|
40
|
+
|
|
41
|
+
* If you were using `schema` parameter:
|
|
42
|
+
* Removed `schema` parameter. Instead, use exported function `parseData(data, schema)` to map data to an array of objects.
|
|
43
|
+
* Old: `import readXlsxFile from "read-excel-file"` and then `const { rows, errors } = await readXlsxFile(..., { schema })`
|
|
44
|
+
* New: `import { readSheet, parseData } from "read-excel-file/browser"` and then `const result = parseData(await readSheet(...), schema)`
|
|
45
|
+
* The `result` of the function is an array where each element represents a "data row" and has shape `{ object, errors }`.
|
|
46
|
+
* Depending on whether there were any errors when parsing a given "data row", either `object` or `errors` property will be `undefined`.
|
|
47
|
+
* The `errors` don't have a `row` property anymore because it could be derived from "data row" number.
|
|
48
|
+
* In version `9.x`, the `row` property has been re-added, so consider migrating straight to `9.x`.
|
|
49
|
+
* In version `9.x`, the returned result of `parseData()` has been changed back to `{ errors, objects }`, so consider migrating straight to `9.x`. In that case, if there're no errors, `errors` will be `undefined`; otherwise, `errors` will be a non-empty array and `objects` will be `undefined`.
|
|
50
|
+
* Renamed some `schema`-related parameters:
|
|
51
|
+
* `schemaPropertyValueForMissingColumn` → `propertyValueWhenColumnIsMissing`
|
|
52
|
+
* `schemaPropertyValueForMissingValue` → `propertyValueWhenCellIsEmpty`
|
|
53
|
+
* `schemaPropertyShouldSkipRequiredValidationForMissingColumn` → (removed)
|
|
54
|
+
* `getEmptyObjectValue` → `transformEmptyObject`
|
|
55
|
+
* The leading `.` character is now removed from the `path` parameter.
|
|
56
|
+
* `getEmptyArrayValue` → `transformEmptyArray`
|
|
57
|
+
* The leading `.` character is now removed from the `path` parameter.
|
|
58
|
+
* Previously, when using a `schema` to parse comma-separated values, it used to ignore any commas that're surrounded by quotes, similar to how it's done in `.csv` files. Now it no longer does that.
|
|
59
|
+
* Previously, when using a `schema` to parse comma-separated values, it used to allow empty-string elements. Now it no longer does that and such empty-string elements will now result in an error with properties: `{ error: "invalid", reason: "syntax" }`.
|
|
60
|
+
* Previously, when using a `schema` to parse `type: Date` properties, it used to support both `Date` objects and numeric timestamps as the input data for the property value. In the latter case, it simply force-converted those numeric timestamps to corresponding `Date` objects. Now `parseData()` function no longer does that, and demands the input data for `type: Date` schema properties to only be `Date` objects, i.e. it shifts the responsibility to interpret date cell values correctly onto `readSheet()` and `readExcelFile()` functions. And I'd personally assume that in any real-world (i.e. non-contrived) scenario those functions would interpret date cell values correctly, so I personally don't consider this a "breaking change". Still, formally, it is a "breaking change" and therefore should be mentioned. So if, for some strange reason, those two functions happen to not recognize a date cell value correctly, `parseData()` function will return an error for such cell: `"not_a_date"`.
|
|
61
|
+
* Previously, when using a `schema` to parse sheet data, and a given row of data was completely empty, it didn't run any `required` property validations. Now it no longer does that and it will run all `required` property validations regardless of whether it's a completely empty row of data or not.
|
|
62
|
+
|
|
63
|
+
* If you were using `transformData` parameter:
|
|
64
|
+
* Removed `transformData` parameter because the `schema` parameter was extracted into a separate function called `parseData()`. Now, if required, a developer could transform the `data` manually and then pass it to `parseData()` function.
|
|
65
|
+
|
|
66
|
+
* If you were using `isColumnOriented` parameter:
|
|
67
|
+
* Removed `isColumnOriented` parameter because it seemed to be of no use.
|
|
68
|
+
|
|
69
|
+
* If you were using `ignoreEmptyRows` parameter:
|
|
70
|
+
* Removed `ignoreEmptyRows` parameter. Empty rows somewhere in the middle of a sheet are not ignored now. That doesn't concern empty rows at the end of a sheet though — those're still ignored.
|
|
71
|
+
|
|
72
|
+
* If you were using TypeScript:
|
|
73
|
+
* Renamed some of the exported types:
|
|
74
|
+
* `Type` → `ParseDataCustomType`
|
|
75
|
+
* `Error` or `SchemaParseCellValueError` → `ParseDataError`
|
|
76
|
+
* `CellValueRequiredError` → `ParseDataValueRequiredError`
|
|
77
|
+
* `ParsedObjectsResult` → `ParseDataResult`
|
|
63
78
|
</details>
|
|
64
79
|
|
|
65
80
|
<details>
|
|
@@ -67,11 +82,19 @@ Also check out [`write-excel-file`](https://www.npmjs.com/package/write-excel-fi
|
|
|
67
82
|
|
|
68
83
|
######
|
|
69
84
|
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
|
|
74
|
-
*
|
|
85
|
+
* If you were using `parseData()` function:
|
|
86
|
+
* Rewrote the code of the `parseData()` function and renamed it to `parseSheetData()`.
|
|
87
|
+
* The result of `parseSheetData()` function is now `{ errors, objects }`. If there're no errors, `errors` will be `undefined`. Otherwise, `errors` will be a non-empty array and `objects` will be `undefined`.
|
|
88
|
+
* Previously the result of `parseSheetData()` function was `[{ errors, object }, ...]`, i.e. the `errors` were split between each particular data row. Now the `errors` are combined for all data rows. The rationale is that it's simpler to handle the result of the function this way.
|
|
89
|
+
* Re-added `row: number` property to the `error` object.
|
|
90
|
+
* Renamed some of the exported TypeScript types:
|
|
91
|
+
* `ParseDataCustomType` → `ParseSheetDataCustomType`
|
|
92
|
+
* `ParseDataCustomTypeErrorMessage` → `ParseSheetDataCustomTypeErrorMessage`
|
|
93
|
+
* `ParseDataCustomTypeErrorReason` → `ParseSheetDataCustomTypeErrorReason`
|
|
94
|
+
* `ParseDataError` → `ParseSheetDataError`
|
|
95
|
+
* `ParseDataValueRequiredError` → `ParseSheetDataValueRequiredError`
|
|
96
|
+
* `ParseDataResult` → `ParseSheetDataResult`
|
|
97
|
+
* In a `schema`, a nested object could be declared as: `{ required: true/false, schema: { ... } }`. This is still true but the `required` flag is now only allowed to be either `undefined` or `false`, so `true` value is not allowed. The reason is quite simple. If a nested object as a whole is marked as `required: true`, and then it happens to be empty, a `"required"` error should be returned for it. But that error would also have to include a `column` title, and a nested object simply can't be pinned down to a single column in a sheet because it is by definition spread over multiple columns. So instead of marking a nested object as a whole with `required: true`, mark the specific required properties of it.
|
|
75
98
|
</details>
|
|
76
99
|
|
|
77
100
|
## Install
|
|
@@ -84,19 +107,21 @@ Alternatively, it could be included on a web page [directly](#cdn) via a `<scrip
|
|
|
84
107
|
|
|
85
108
|
## Use
|
|
86
109
|
|
|
87
|
-
If your `.xlsx` file only has a single "sheet", or if you only
|
|
110
|
+
If your `.xlsx` file only has a single "sheet", or if you only need to read a single "sheet", or if you don't care what a "sheet" is, use `readSheet()` function.
|
|
111
|
+
|
|
112
|
+
For example, consider the following `.xlsx` file:
|
|
88
113
|
|
|
89
114
|
| Name | Date of Birth | Married | Kids |
|
|
90
115
|
| ---------- | ------------- | ------- | ---- |
|
|
91
116
|
| John Smith | 1/1/1995 | TRUE | 3 |
|
|
92
117
|
| Kate Brown | 3/1/2010 | FALSE | 0 |
|
|
93
118
|
|
|
119
|
+
Here's how to read it using `readSheet()` function:
|
|
120
|
+
|
|
94
121
|
```js
|
|
95
122
|
import { readSheet } from 'read-excel-file/node'
|
|
96
123
|
|
|
97
|
-
await readSheet(file)
|
|
98
|
-
|
|
99
|
-
// Returns
|
|
124
|
+
await readSheet(file) ===
|
|
100
125
|
[
|
|
101
126
|
['Name', 'Date of Birth', 'Married', 'Kids'],
|
|
102
127
|
['John Smith', 1995-01-01T00:00:00.000Z, true, 3],
|
|
@@ -104,20 +129,18 @@ await readSheet(file)
|
|
|
104
129
|
]
|
|
105
130
|
```
|
|
106
131
|
|
|
107
|
-
|
|
132
|
+
The result is an array of rows. Each row is an array of values — `string`, `number`, `boolean` or `Date`.
|
|
108
133
|
|
|
109
134
|
<!-- 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. -->
|
|
110
135
|
|
|
111
|
-
|
|
136
|
+
It also 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.
|
|
112
137
|
|
|
113
|
-
But if you need to read all "sheets"
|
|
138
|
+
But if you need to read all available "sheets" in a file, use the default exported function:
|
|
114
139
|
|
|
115
140
|
```js
|
|
116
141
|
import readExcelFile from 'read-excel-file/node'
|
|
117
142
|
|
|
118
|
-
await readExcelFile(file)
|
|
119
|
-
|
|
120
|
-
// Returns
|
|
143
|
+
await readExcelFile(file) ===
|
|
121
144
|
[{
|
|
122
145
|
sheet: 'Sheet1',
|
|
123
146
|
data: [
|
|
@@ -131,19 +154,21 @@ await readExcelFile(file)
|
|
|
131
154
|
}]
|
|
132
155
|
```
|
|
133
156
|
|
|
134
|
-
|
|
157
|
+
The result is a non-empty array of "sheets". Each "sheet" is an object with properties:
|
|
135
158
|
* `sheet` — Sheet name.
|
|
136
159
|
* Example: `"Sheet1"`
|
|
137
160
|
* `data` — Sheet data. An array of rows. Each row is an array of values — `string`, `number`, `boolean` or `Date`.
|
|
138
161
|
* Example: `[ ['Name','Age'], ['John Smith',30], ['Kate Brown',15] ]`
|
|
139
162
|
|
|
140
|
-
##
|
|
163
|
+
## Import
|
|
141
164
|
|
|
142
165
|
This package provides a separate `import` path for each different environment, as described below.
|
|
143
166
|
|
|
144
167
|
### Browser
|
|
145
168
|
|
|
146
|
-
|
|
169
|
+
`read-excel-file/browser`
|
|
170
|
+
|
|
171
|
+
It can read from a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File), a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or an [`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer).
|
|
147
172
|
|
|
148
173
|
Example: User chooses a file and the web application reads it.
|
|
149
174
|
|
|
@@ -180,7 +205,7 @@ const data = await readSheet(blob)
|
|
|
180
205
|
|
|
181
206
|
######
|
|
182
207
|
|
|
183
|
-
All exports of `read-excel-file` already use a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) under the hood when reading `.xlsx` file contents. This is in order to avoid freezing the UI when reading large files. So using an additional Web Worker on top of that isn't really necessary. Still, for those who require it, this example shows how a user chooses a file and the web application reads it in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).
|
|
208
|
+
All exports of `read-excel-file` already use a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) under the hood when reading `.xlsx` file contents. This is in order to avoid freezing the UI when reading large files. So using an additional Web Worker on top of that isn't really necessary. Still, for those who require it, this example shows how a user chooses a file and the web application reads it in a [Web Worker](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) using `read-excel-file/web-worker` import path.
|
|
184
209
|
|
|
185
210
|
```js
|
|
186
211
|
// Step 1: Initialize Web Worker.
|
|
@@ -219,7 +244,9 @@ onmessage = async function(event) {
|
|
|
219
244
|
|
|
220
245
|
### Node.js
|
|
221
246
|
|
|
222
|
-
|
|
247
|
+
`read-excel-file/node`
|
|
248
|
+
|
|
249
|
+
It can read from a file path, a [`Stream`](https://nodejs.org/api/stream.html), a [`Buffer`](https://nodejs.org/api/buffer.html) or a [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob).
|
|
223
250
|
|
|
224
251
|
Example 1: Read from a file path.
|
|
225
252
|
|
|
@@ -239,6 +266,8 @@ const data = await readSheet(fs.createReadStream('/path/to/file'))
|
|
|
239
266
|
|
|
240
267
|
### Universal
|
|
241
268
|
|
|
269
|
+
`read-excel-file/universal`
|
|
270
|
+
|
|
242
271
|
This one works both in a web browser and Node.js. It can only read from a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) or an [`ArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), which could be a bit less convenient for general use.
|
|
243
272
|
|
|
244
273
|
```js
|
|
@@ -295,14 +324,14 @@ Here're the results of reading [sample `.xlsx` files](https://examplefile.com/do
|
|
|
295
324
|
|
|
296
325
|
## Schema
|
|
297
326
|
|
|
298
|
-
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 `
|
|
327
|
+
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 `parseSheetData(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.
|
|
299
328
|
|
|
300
329
|
```js
|
|
301
|
-
import { readSheet,
|
|
330
|
+
import { readSheet, parseSheetData } from "read-excel-file/browser"
|
|
302
331
|
|
|
303
332
|
const data = await readSheet(file)
|
|
304
333
|
const schema = { ... }
|
|
305
|
-
const { objects, errors } =
|
|
334
|
+
const { objects, errors } = parseSheetData(data, schema)
|
|
306
335
|
if (errors) {
|
|
307
336
|
console.error(errors)
|
|
308
337
|
} else {
|
|
@@ -310,7 +339,7 @@ if (errors) {
|
|
|
310
339
|
}
|
|
311
340
|
```
|
|
312
341
|
|
|
313
|
-
The `
|
|
342
|
+
The `parseSheetData()` function returns an object — `{ objects, errors }`. Depending on whether there were any errors when parsing the data, either `objects` or `errors` property will be `undefined`.
|
|
314
343
|
|
|
315
344
|
The sheet data that is being parsed should adhere to a simple structure: the first row should be a header row with just column titles, and each following row should specify the values for those columns.
|
|
316
345
|
|
|
@@ -437,7 +466,7 @@ const schema = {
|
|
|
437
466
|
const data = await readSheet(file)
|
|
438
467
|
|
|
439
468
|
// Parse `data` using a `schema`
|
|
440
|
-
const { objects, errors } =
|
|
469
|
+
const { objects, errors } = parseSheetData(data, schema)
|
|
441
470
|
|
|
442
471
|
// There have been no errors when parsing the sheet data, so `errors` is `undefined`.
|
|
443
472
|
// Should there have been any errors when parsing the sheet data, `errors` would've been
|
|
@@ -480,9 +509,9 @@ function PhoneNumber(value) {
|
|
|
480
509
|
import type {
|
|
481
510
|
Schema,
|
|
482
511
|
CellValue,
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
512
|
+
ParseSheetDataError,
|
|
513
|
+
ParseSheetDataCustomType,
|
|
514
|
+
ParseSheetDataCustomTypeErrorMessage
|
|
486
515
|
} from 'read-excel-file/node'
|
|
487
516
|
|
|
488
517
|
type ColumnTitle = 'COLUMN TITLE 1' | 'COLUMN TITLE 2'
|
|
@@ -496,20 +525,20 @@ function CustomType(value: CellValue): CustomTypeValue {
|
|
|
496
525
|
return '~' + value + '~'
|
|
497
526
|
}
|
|
498
527
|
|
|
499
|
-
type CustomTypeErrorMessage<Type extends
|
|
528
|
+
type CustomTypeErrorMessage<Type extends ParseSheetDataCustomType<unknown>> =
|
|
500
529
|
Type extends typeof CustomType
|
|
501
530
|
? 'not_a_string'
|
|
502
531
|
: never
|
|
503
532
|
|
|
504
533
|
// type CustomTypeErrorReason<
|
|
505
|
-
// Type extends
|
|
506
|
-
// ErrorMessage extends
|
|
534
|
+
// Type extends ParseSheetDataCustomType<unknown>,
|
|
535
|
+
// ErrorMessage extends ParseSheetDataCustomTypeErrorMessage<Type>
|
|
507
536
|
// > =
|
|
508
537
|
// Type extends typeof CustomType
|
|
509
538
|
// ? (ErrorMessage extends 'not_a_string' ? undefined : never)
|
|
510
539
|
// : never
|
|
511
540
|
|
|
512
|
-
type PossibleError =
|
|
541
|
+
type PossibleError = ParseSheetDataError<
|
|
513
542
|
ColumnTitle,
|
|
514
543
|
typeof CustomType,
|
|
515
544
|
CustomTypeErrorMessage<typeof CustomType>
|
|
@@ -533,7 +562,7 @@ const schema: Schema<Object, ColumnTitle> = {
|
|
|
533
562
|
}
|
|
534
563
|
}
|
|
535
564
|
|
|
536
|
-
const { objects, errors } =
|
|
565
|
+
const { objects, errors } = parseSheetData<Object, ColumnTitle, PossibleError>([
|
|
537
566
|
['COLUMN TITLE 1', 'COLUMN TITLE 2'],
|
|
538
567
|
['Value 1', 'Value 2']
|
|
539
568
|
], schema)
|
package/browser/index.cjs
CHANGED
|
@@ -3,8 +3,8 @@ exports['default'] = require('../commonjs/export/readXlsxFileBrowser.js').defaul
|
|
|
3
3
|
|
|
4
4
|
exports.readSheet = require('../commonjs/export/readSheetBrowser.js').default
|
|
5
5
|
|
|
6
|
-
// `
|
|
7
|
-
exports.
|
|
8
|
-
exports.Integer = require('../commonjs/
|
|
9
|
-
exports.Email = require('../commonjs/
|
|
10
|
-
exports.URL = require('../commonjs/
|
|
6
|
+
// `parseSheetData()`
|
|
7
|
+
exports.parseSheetData = require('../commonjs/parseSheetData/parseSheetData.js').default
|
|
8
|
+
exports.Integer = require('../commonjs/parseSheetData/types/additional/Integer.js').default
|
|
9
|
+
exports.Email = require('../commonjs/parseSheetData/types/additional/Email.js').default
|
|
10
|
+
exports.URL = require('../commonjs/parseSheetData/types/additional/URL.js').default
|
package/browser/index.d.ts
CHANGED
|
@@ -10,17 +10,17 @@ import {
|
|
|
10
10
|
} from '../types/types.d.js';
|
|
11
11
|
|
|
12
12
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from '../types/
|
|
13
|
+
ParseSheetDataOptions,
|
|
14
|
+
ParseSheetDataResult
|
|
15
|
+
} from '../types/parseSheetData/parseSheetData.d.js';
|
|
16
16
|
|
|
17
17
|
import {
|
|
18
18
|
Schema
|
|
19
|
-
} from '../types/
|
|
19
|
+
} from '../types/parseSheetData/parseSheetDataSchema.d.js';
|
|
20
20
|
|
|
21
21
|
import {
|
|
22
|
-
|
|
23
|
-
} from '../types/
|
|
22
|
+
ParseSheetDataError
|
|
23
|
+
} from '../types/parseSheetData/parseSheetDataError.d.js';
|
|
24
24
|
|
|
25
25
|
export {
|
|
26
26
|
CellValue,
|
|
@@ -30,7 +30,7 @@ export {
|
|
|
30
30
|
} from '../types/types.d.js';
|
|
31
31
|
|
|
32
32
|
export {
|
|
33
|
-
|
|
33
|
+
ParseSheetDataCustomType,
|
|
34
34
|
// Base `type`s when parsing data.
|
|
35
35
|
StringType as String,
|
|
36
36
|
DateType as Date,
|
|
@@ -40,22 +40,22 @@ export {
|
|
|
40
40
|
Integer,
|
|
41
41
|
Email,
|
|
42
42
|
URL
|
|
43
|
-
} from '../types/
|
|
43
|
+
} from '../types/parseSheetData/parseSheetDataValueType.d.js';
|
|
44
44
|
|
|
45
45
|
export {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
} from '../types/
|
|
46
|
+
ParseSheetDataCustomTypeErrorMessage,
|
|
47
|
+
ParseSheetDataCustomTypeErrorReason,
|
|
48
|
+
ParseSheetDataError,
|
|
49
|
+
ParseSheetDataValueRequiredError
|
|
50
|
+
} from '../types/parseSheetData/parseSheetDataError.d.js';
|
|
51
51
|
|
|
52
52
|
export {
|
|
53
|
-
|
|
54
|
-
} from '../types/
|
|
53
|
+
ParseSheetDataResult
|
|
54
|
+
} from '../types/parseSheetData/parseSheetData.d.js';
|
|
55
55
|
|
|
56
56
|
export {
|
|
57
57
|
Schema
|
|
58
|
-
} from '../types/
|
|
58
|
+
} from '../types/parseSheetData/parseSheetDataSchema.d.js';
|
|
59
59
|
|
|
60
60
|
export default function readXlsxFile<ParsedNumber = number>(
|
|
61
61
|
input: Input,
|
|
@@ -73,12 +73,12 @@ export function readSheet<ParsedNumber = number>(
|
|
|
73
73
|
options?: ReadOptions<ParsedNumber>
|
|
74
74
|
): Promise<SheetData<ParsedNumber>>;
|
|
75
75
|
|
|
76
|
-
export function
|
|
76
|
+
export function parseSheetData<
|
|
77
77
|
Object extends object,
|
|
78
78
|
ColumnTitle extends string,
|
|
79
|
-
Error extends
|
|
79
|
+
Error extends ParseSheetDataError
|
|
80
80
|
>(
|
|
81
81
|
data: SheetData,
|
|
82
82
|
schema: Schema<Object, ColumnTitle>,
|
|
83
|
-
options?:
|
|
84
|
-
):
|
|
83
|
+
options?: ParseSheetDataOptions
|
|
84
|
+
): ParseSheetDataResult<Object, Error>;
|
package/browser/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { default as default } from '../modules/export/readXlsxFileBrowser.js'
|
|
2
2
|
export { default as readSheet } from '../modules/export/readSheetBrowser.js'
|
|
3
3
|
|
|
4
|
-
// `
|
|
5
|
-
export { default as
|
|
6
|
-
export { default as Integer } from '../modules/
|
|
7
|
-
export { default as Email } from '../modules/
|
|
8
|
-
export { default as URL } from '../modules/
|
|
4
|
+
// `parseSheetData()`
|
|
5
|
+
export { default as parseSheetData } from '../modules/parseSheetData/parseSheetData.js'
|
|
6
|
+
export { default as Integer } from '../modules/parseSheetData/types/additional/Integer.js'
|
|
7
|
+
export { default as Email } from '../modules/parseSheetData/types/additional/Email.js'
|
|
8
|
+
export { default as URL } from '../modules/parseSheetData/types/additional/URL.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InvalidError.js","names":["InvalidError","exports","_Error","_inherits","_super","_createSuper","reason","_this","_classCallCheck","call","_createClass","_wrapNativeSuper","Error"],"sources":["../../source/parseSheetData/InvalidError.js"],"sourcesContent":["export default class InvalidError extends Error {\r\n constructor(reason) {\r\n super('invalid')\r\n this.reason = reason\r\n }\r\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;IAAqBA,YAAY,GAAAC,OAAA,qCAAAC,MAAA;EAAAC,SAAA,CAAAH,YAAA,EAAAE,MAAA;EAAA,IAAAE,MAAA,GAAAC,YAAA,CAAAL,YAAA;EAC/B,SAAAA,aAAYM,MAAM,EAAE;IAAA,IAAAC,KAAA;IAAAC,eAAA,OAAAR,YAAA;IAClBO,KAAA,GAAAH,MAAA,CAAAK,IAAA,OAAM,SAAS;IACfF,KAAA,CAAKD,MAAM,GAAGA,MAAM;IAAA,OAAAC,KAAA;EACtB;EAAC,OAAAG,YAAA,CAAAV,YAAA;AAAA,gBAAAW,gBAAA,CAJuCC,KAAK"}
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports["default"] =
|
|
6
|
+
exports["default"] = parseSheetData;
|
|
7
7
|
exports.getNextSubstring = getNextSubstring;
|
|
8
|
-
exports.parseDataWithPerRowErrors = parseDataWithPerRowErrors;
|
|
9
8
|
exports.parseSeparatedSubstrings = parseSeparatedSubstrings;
|
|
9
|
+
exports.parseSheetDataWithPerRowErrors = parseSheetDataWithPerRowErrors;
|
|
10
10
|
exports.parseValue = parseValue;
|
|
11
11
|
var _Number = _interopRequireDefault(require("./types/Number.js"));
|
|
12
12
|
var _String = _interopRequireDefault(require("./types/String.js"));
|
|
@@ -56,10 +56,10 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|
|
56
56
|
* @param {string} [options.separatorCharacter] — When specified, string values will be split by this separator to get the array.
|
|
57
57
|
* @return {object} — An object of shape `{ objects, errors }`. Either `objects` or `errors` is going to be `undefined`.
|
|
58
58
|
*/
|
|
59
|
-
function
|
|
59
|
+
function parseSheetData(data, schema, optionsCustom) {
|
|
60
60
|
var objects = [];
|
|
61
61
|
var errors = [];
|
|
62
|
-
var parsedRows =
|
|
62
|
+
var parsedRows = parseSheetDataWithPerRowErrors(data, schema, optionsCustom);
|
|
63
63
|
var parsedRowIndex = 0;
|
|
64
64
|
for (var _iterator = _createForOfIteratorHelperLoose(parsedRows), _step; !(_step = _iterator()).done;) {
|
|
65
65
|
var _step$value = _step.value,
|
|
@@ -86,7 +86,7 @@ function parseData(data, schema, optionsCustom) {
|
|
|
86
86
|
}
|
|
87
87
|
|
|
88
88
|
// This one is only used in tests.
|
|
89
|
-
function
|
|
89
|
+
function parseSheetDataWithPerRowErrors(data, schema, optionsCustom) {
|
|
90
90
|
validateSchema(schema);
|
|
91
91
|
var options = applyDefaultOptions(optionsCustom);
|
|
92
92
|
var _data = _toArray(data),
|
|
@@ -728,4 +728,4 @@ var objectConstructor = {}.constructor;
|
|
|
728
728
|
function isObject(object) {
|
|
729
729
|
return object !== undefined && object !== null && object.constructor === objectConstructor;
|
|
730
730
|
}
|
|
731
|
-
//# sourceMappingURL=
|
|
731
|
+
//# sourceMappingURL=parseSheetData.js.map
|