read-excel-file 5.3.3 → 5.4.0
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 +15 -0
- package/README.md +65 -32
- package/bundle/read-excel-file.min.js +1 -1
- package/bundle/read-excel-file.min.js.map +1 -1
- package/commonjs/read/isDateTimestamp.js +112 -0
- package/commonjs/read/isDateTimestamp.js.map +1 -0
- package/commonjs/read/parseCell.js +5 -0
- package/commonjs/read/parseCell.js.map +1 -1
- package/commonjs/read/parseCellValue.js +66 -82
- package/commonjs/read/parseCellValue.js.map +1 -1
- package/commonjs/read/parseDate.js.map +1 -1
- package/commonjs/read/schema/convertToJson.js +99 -31
- package/commonjs/read/schema/convertToJson.js.map +1 -1
- package/commonjs/read/schema/convertToJson.test.js.map +1 -1
- package/commonjs/read/unpackXlsxFileNode.js +14 -0
- package/commonjs/read/unpackXlsxFileNode.js.map +1 -1
- package/commonjs/xml/dom.js +29 -0
- package/commonjs/xml/dom.js.map +1 -1
- package/commonjs/xml/xlsx.js.map +1 -1
- package/commonjs/xml/xml.js +2 -4
- package/commonjs/xml/xml.js.map +1 -1
- package/commonjs/xml/{xlsx-xpath.js → xpath/xlsx-xpath.js} +1 -0
- package/commonjs/xml/xpath/xlsx-xpath.js.map +1 -0
- package/commonjs/xml/{xpathBrowser.js → xpath/xpathBrowser.js} +1 -0
- package/commonjs/xml/xpath/xpathBrowser.js.map +1 -0
- package/commonjs/xml/{xpathNode.js → xpath/xpathNode.js} +1 -1
- package/commonjs/xml/xpath/xpathNode.js.map +1 -0
- package/modules/read/isDateTimestamp.js +104 -0
- package/modules/read/isDateTimestamp.js.map +1 -0
- package/modules/read/parseCell.js +5 -1
- package/modules/read/parseCell.js.map +1 -1
- package/modules/read/parseCellValue.js +64 -80
- package/modules/read/parseCellValue.js.map +1 -1
- package/modules/read/parseDate.js.map +1 -1
- package/modules/read/schema/convertToJson.js +99 -31
- package/modules/read/schema/convertToJson.js.map +1 -1
- package/modules/read/schema/convertToJson.test.js.map +1 -1
- package/modules/read/unpackXlsxFileNode.js +14 -0
- package/modules/read/unpackXlsxFileNode.js.map +1 -1
- package/modules/xml/dom.js +27 -0
- package/modules/xml/dom.js.map +1 -1
- package/modules/xml/xlsx.js.map +1 -1
- package/modules/xml/xml.js +2 -2
- package/modules/xml/xml.js.map +1 -1
- package/modules/xml/{xlsx-xpath.js → xpath/xlsx-xpath.js} +1 -0
- package/modules/xml/xpath/xlsx-xpath.js.map +1 -0
- package/modules/xml/{xpathBrowser.js → xpath/xpathBrowser.js} +1 -0
- package/modules/xml/xpath/xpathBrowser.js.map +1 -0
- package/modules/xml/{xpathNode.js → xpath/xpathNode.js} +1 -1
- package/modules/xml/xpath/xpathNode.js.map +1 -0
- package/package.json +1 -1
- package/types.d.ts +2 -1
- package/commonjs/xml/xlsx-xpath.js.map +0 -1
- package/commonjs/xml/xpathBrowser.js.map +0 -1
- package/commonjs/xml/xpathNode.js.map +0 -1
- package/modules/xml/xlsx-xpath.js.map +0 -1
- package/modules/xml/xpathBrowser.js.map +0 -1
- package/modules/xml/xpathNode.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
5.4.0 / 04.07.2022
|
|
2
|
+
==================
|
|
3
|
+
|
|
4
|
+
* [Fixed](https://gitlab.com/catamphetamine/read-excel-file/-/issues/54) non-ASCII character encoding by forcing Node.js version of the library to read zipped contents of an XLSX file in UTF-8 character encoding. I suppose it won't break the existing code.
|
|
5
|
+
|
|
6
|
+
5.3.5 / 26.06.2022
|
|
7
|
+
==================
|
|
8
|
+
|
|
9
|
+
* Added `includeNullValues: true` option when parsing spreadsheet data using a `schema`. By default, it ignores all `null` values (ignores all empty cells).
|
|
10
|
+
|
|
11
|
+
5.3.4 / 11.06.2022
|
|
12
|
+
==================
|
|
13
|
+
|
|
14
|
+
* Added an optional `reason?: string` property of a with-schema parsing error.
|
|
15
|
+
|
|
1
16
|
5.3.3 / 24.05.2022
|
|
2
17
|
==================
|
|
3
18
|
|
package/README.md
CHANGED
|
@@ -93,7 +93,29 @@ onmessage = function(event) {
|
|
|
93
93
|
|
|
94
94
|
## JSON
|
|
95
95
|
|
|
96
|
-
To convert
|
|
96
|
+
To read spreadsheet data and then convert it to an array of JSON objects, pass a `schema` option when calling `readXlsxFile()`. In that case, instead of returning an array of rows of cells, it will return an object of shape `{ rows, errors }` where `rows` is gonna be an array of JSON objects created from the spreadsheet data according to the `schema`, and `errors` is gonna be an array of errors encountered while converting spreadsheet data to JSON objects.
|
|
97
|
+
|
|
98
|
+
Each property of a JSON object should be described by an "entry" in the `schema`. The key of the entry should be the column's title in the spreadsheet. The value of the entry should be an object with properties:
|
|
99
|
+
|
|
100
|
+
* `property` — The name of the object's property.
|
|
101
|
+
* `required` — (optional) Required properties can be marked as `required: true`.
|
|
102
|
+
* `validate(value)` — (optional) Cell value validation function. Is only called on non-empty cells. If the cell value is invalid, it should throw an error with the error message set to the error code.
|
|
103
|
+
* `type` — (optional) The type of the value. Defines how the cell value will be parsed. If no `type` is specified then the cell value is returned "as is": as a string, number, date or boolean. A `type` could be a:
|
|
104
|
+
* Built-in type:
|
|
105
|
+
* `String`
|
|
106
|
+
* `Number`
|
|
107
|
+
* `Boolean`
|
|
108
|
+
* `Date`
|
|
109
|
+
* "Utility" type exported from the library:
|
|
110
|
+
* `Integer`
|
|
111
|
+
* `Email`
|
|
112
|
+
* `URL`
|
|
113
|
+
* Custom type:
|
|
114
|
+
* A function that receives a cell value and returns a parsed value. If the value is invalid, it should throw an error with the error message set to the error code.
|
|
115
|
+
|
|
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
|
+
|
|
118
|
+
#### An example of using a `schema`
|
|
97
119
|
|
|
98
120
|
```js
|
|
99
121
|
// An example *.xlsx document:
|
|
@@ -157,7 +179,7 @@ const schema = {
|
|
|
157
179
|
}
|
|
158
180
|
|
|
159
181
|
readXlsxFile(file, { schema }).then(({ rows, errors }) => {
|
|
160
|
-
// `errors` list items have shape: `{ row, column, error, value }`.
|
|
182
|
+
// `errors` list items have shape: `{ row, column, error, reason?, value?, type? }`.
|
|
161
183
|
errors.length === 0
|
|
162
184
|
|
|
163
185
|
rows === [{
|
|
@@ -173,32 +195,36 @@ readXlsxFile(file, { schema }).then(({ rows, errors }) => {
|
|
|
173
195
|
})
|
|
174
196
|
```
|
|
175
197
|
|
|
176
|
-
If no `type` is specified then the cell value is returned "as is": as a string, number, date or boolean.
|
|
198
|
+
<!-- If no `type` is specified then the cell value is returned "as is": as a string, number, date or boolean. -->
|
|
177
199
|
|
|
178
|
-
There are also some additional exported `type`s available:
|
|
200
|
+
<!-- There are also some additional exported `type`s available: -->
|
|
179
201
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
* `Email` for parsing email addresses.
|
|
202
|
+
<details>
|
|
203
|
+
<summary><strong>Custom <code>type</code></strong> example.</summary>
|
|
183
204
|
|
|
184
|
-
|
|
205
|
+
#####
|
|
185
206
|
|
|
186
207
|
```js
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
208
|
+
{
|
|
209
|
+
'COLUMN_TITLE': {
|
|
210
|
+
// This function will only be called for a non-empty cell.
|
|
211
|
+
type: (value) => {
|
|
212
|
+
try {
|
|
213
|
+
return parseValue(value)
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.error(error)
|
|
216
|
+
throw new Error('invalid')
|
|
217
|
+
}
|
|
218
|
+
}
|
|
194
219
|
}
|
|
195
220
|
}
|
|
196
221
|
```
|
|
222
|
+
</details>
|
|
197
223
|
|
|
198
|
-
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`).
|
|
224
|
+
<!-- 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`). -->
|
|
199
225
|
|
|
200
226
|
<details>
|
|
201
|
-
<summary>
|
|
227
|
+
<summary>How to fix spreadsheet data before <code>schema</code> parsing. For example, <strong>how to ignore empty rows</strong>.</summary>
|
|
202
228
|
|
|
203
229
|
#####
|
|
204
230
|
|
|
@@ -219,7 +245,7 @@ readXlsxFile(file, {
|
|
|
219
245
|
|
|
220
246
|
<details>
|
|
221
247
|
<summary>
|
|
222
|
-
The <strong>
|
|
248
|
+
The <strong>function for converting data to JSON objects using a schema</strong> is exported from this library too, if anyone wants it.
|
|
223
249
|
</summary>
|
|
224
250
|
|
|
225
251
|
#####
|
|
@@ -234,34 +260,41 @@ const { rows, errors } = convertToJson(data, schema)
|
|
|
234
260
|
</details>
|
|
235
261
|
|
|
236
262
|
<details>
|
|
237
|
-
<summary>A <strong>React component</strong>
|
|
263
|
+
<summary>A <strong>React component for displaying errors</strong> that occured during schema parsing/validation.</summary>
|
|
238
264
|
|
|
239
265
|
#####
|
|
240
266
|
|
|
241
267
|
```js
|
|
242
268
|
import { parseExcelDate } from 'read-excel-file'
|
|
243
269
|
|
|
244
|
-
function ParseExcelError({ children
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
value = parseExcelDate(value).toString()
|
|
249
|
-
}
|
|
250
|
-
// Render error summary.
|
|
270
|
+
function ParseExcelError({ children }) {
|
|
271
|
+
const { type, value, error, reason, row, column } = children
|
|
272
|
+
|
|
273
|
+
// Error summary.
|
|
251
274
|
return (
|
|
252
275
|
<div>
|
|
253
|
-
<code>"{error
|
|
276
|
+
<code>"{error}"</code>
|
|
277
|
+
{reason && ' '}
|
|
278
|
+
{reason && <code>("{reason}")</code>}
|
|
254
279
|
{' for value '}
|
|
255
|
-
<code>
|
|
280
|
+
<code>{stringifyValue(value)}</code>
|
|
256
281
|
{' in column '}
|
|
257
|
-
<code>"{
|
|
258
|
-
{
|
|
259
|
-
{
|
|
282
|
+
<code>"{column}"</code>
|
|
283
|
+
{type && type.name && ' of type '}
|
|
284
|
+
{type && type.name && <code>"{type.name}"</code>}
|
|
260
285
|
{' in row '}
|
|
261
|
-
<code>
|
|
286
|
+
<code>{row}</code>
|
|
262
287
|
</div>
|
|
263
288
|
)
|
|
264
289
|
}
|
|
290
|
+
|
|
291
|
+
function stringifyValue(value) {
|
|
292
|
+
// Wrap strings in quotes.
|
|
293
|
+
if (typeof value === 'string') {
|
|
294
|
+
return '"' + value + '"'
|
|
295
|
+
}
|
|
296
|
+
return String(value)
|
|
297
|
+
}
|
|
265
298
|
```
|
|
266
299
|
</details>
|
|
267
300
|
|