read-excel-file 5.3.4 → 5.3.5

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 CHANGED
@@ -1,3 +1,8 @@
1
+ 5.3.5 / 26.06.2022
2
+ ==================
3
+
4
+ * Added `includeNullValues: true` option when parsing spreadsheet data using a `schema`. By default, it ignores all `null` values (ignores all empty cells).
5
+
1
6
  5.3.4 / 11.06.2022
2
7
  ==================
3
8
 
package/README.md CHANGED
@@ -93,7 +93,29 @@ onmessage = function(event) {
93
93
 
94
94
  ## JSON
95
95
 
96
- To convert table rows to JSON objects, pass a `schema` option to `readXlsxFile()`. It will return `{ rows, errors }` object instead of just `rows`.
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:
@@ -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
- * `Integer` for parsing integer `Number`s.
181
- * `URL` for parsing URLs.
182
- * `Email` for parsing email addresses.
202
+ <details>
203
+ <summary><strong>Custom <code>type</code></strong> example.</summary>
183
204
 
184
- A custom `type` can be defined as a simple function:
205
+ #####
185
206
 
186
207
  ```js
187
- // This function will only be called for a non-empty cell.
188
- type: (value) => {
189
- try {
190
- return parseValue(value)
191
- } catch (error) {
192
- console.error(error)
193
- throw new Error('invalid')
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>Fixing spreadsheet structure for <code>schema</code> parsing. For example, how to <strong>ignore empty rows</strong>.</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>schema conversion function</strong> can also be imported standalone, if anyone wants it.
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,7 +260,7 @@ const { rows, errors } = convertToJson(data, schema)
234
260
  </details>
235
261
 
236
262
  <details>
237
- <summary>A <strong>React component</strong> for displaying schema parsing/validation <strong>errors</strong>.</summary>
263
+ <summary>A <strong>React component for displaying errors</strong> that occured during schema parsing/validation.</summary>
238
264
 
239
265
  #####
240
266