read-excel-file 4.0.8 → 4.1.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 CHANGED
@@ -9,6 +9,17 @@
9
9
  * Removed undocumented `convertToJson()` export.
10
10
  -->
11
11
 
12
+ 4.1.0 / 09.11.2020
13
+ ==================
14
+
15
+ * Renamed schema entry `parse()` function: now it's called `type`. This way, `type` could be both a built-in type and a custom type.
16
+
17
+ * Changed the built-in `"Integer"`, `"URL"` and `"Email"` types: now they're exported functions again instead of strings. Strings still work.
18
+
19
+ * Added `map` parameter: similar to `schema` but doesn't perform any parsing or validation. Can be used to map an Excel file to an array of objects that could be parsed/validated using [`yup`](https://github.com/jquense/yup).
20
+
21
+ * `type` of a schema entry is no longer required: if no `type` is specified, then the cell value is returned "as is" (string, or number, or boolean, or `Date`).
22
+
12
23
  4.0.8 / 08.11.2020
13
24
  ==================
14
25
 
package/README.md CHANGED
@@ -110,7 +110,7 @@ const schema = {
110
110
  'CONTACT': {
111
111
  prop: 'contact',
112
112
  required: true,
113
- parse(value) {
113
+ type: (value) => {
114
114
  const number = parsePhoneNumber(value)
115
115
  if (!number) {
116
116
  throw new Error('invalid')
@@ -146,26 +146,69 @@ readXlsxFile(file, { schema }).then(({ rows, errors }) => {
146
146
  })
147
147
  ```
148
148
 
149
+ If no `type` is specified then the cell value is returned "as is".
150
+
149
151
  There are also some additional exported `type`s:
150
152
 
151
- * `"Integer"` for parsing integer `Number`s.
152
- * `"URL"` for parsing URLs.
153
- * `"Email"` for parsing email addresses.
153
+ * `Integer` for parsing integer `Number`s.
154
+ * `URL` for parsing URLs.
155
+ * `Email` for parsing email addresses.
156
+
157
+ 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.
158
+
159
+ #### Map
160
+
161
+ Sometimes, a developer might want to use some other (more advanced) solution for schema parsing and validation (like [`yup`](https://github.com/jquense/yup)). If a developer passes a `map` instead of a `schema` to `readXlsxFile()`, then it would just map each data row to a JSON object without doing any parsing or validation.
162
+
163
+ ```js
164
+ // An example *.xlsx document:
165
+ // -----------------------------------------------------------------------------------------
166
+ // | START DATE | NUMBER OF STUDENTS | IS FREE | COURSE TITLE | CONTACT | STATUS |
167
+ // -----------------------------------------------------------------------------------------
168
+ // | 03/24/2018 | 123 | true | Chemistry | (123) 456-7890 | SCHEDULED |
169
+ // -----------------------------------------------------------------------------------------
170
+
171
+ const map = {
172
+ 'START DATE': 'date',
173
+ 'NUMBER OF STUDENTS': 'numberOfStudents',
174
+ 'COURSE': {
175
+ 'course': {
176
+ 'IS FREE': 'isFree',
177
+ 'COURSE TITLE': 'title'
178
+ }
179
+ },
180
+ 'CONTACT': 'contact',
181
+ 'STATUS': 'status'
182
+ }
183
+
184
+ readXlsxFile(file, { map }).then(({ rows }) => {
185
+ rows === [{
186
+ date: new Date(2018, 2, 24),
187
+ numberOfStudents: 123,
188
+ course: {
189
+ isFree: true,
190
+ title: 'Chemistry'
191
+ },
192
+ contact: '(123) 456-7890',
193
+ status: 'SCHEDULED'
194
+ }]
195
+ })
196
+ ```
154
197
 
155
- A schema entry for a column can also have a `validate(value)` function for validating the parsed value. It must `throw` an `Error` if the value is invalid.
198
+ #### Displaying schema errors
156
199
 
157
- A React component for displaying error info could look like this:
200
+ A React component for displaying schema parsing/validation errors could look like this:
158
201
 
159
202
  ```js
160
203
  import { parseExcelDate } from 'read-excel-file'
161
204
 
162
205
  function ParseExcelError({ children: error }) {
163
- // Human-readable value.
206
+ // Get a human-readable value.
164
207
  let value = error.value
165
208
  if (error.type === Date) {
166
209
  value = parseExcelDate(value).toString()
167
210
  }
168
- // Error summary.
211
+ // Render error summary.
169
212
  return (
170
213
  <div>
171
214
  <code>"{error.error}"</code>
@@ -182,6 +225,8 @@ function ParseExcelError({ children: error }) {
182
225
  }
183
226
  ```
184
227
 
228
+ #### Transforming rows/columns before schema is applied
229
+
185
230
  When using a `schema` there's also an optional `transformData(data)` parameter which can be used for the cases when the spreadsheet rows/columns aren't in the correct format. For example, the heading row may be missing, or there may be some purely presentational or empty rows. Example:
186
231
 
187
232
  ```js