read-excel-file
read-excel-file
Read .xlsx files in a browser or Node.js.
It also supports parsing spreadsheet rows into JSON objects using a schema.
Also check out write-excel-file for writing .xlsx files.
Migrating from 6.x to 7.x
- Renamed the default export
"read-excel-file"to"read-excel-file/browser", and it uses Web Workers now.- Old:
import readExcelFile from "read-excel-file" - New:
import readExcelFile from "read-excel-file/browser"
- Old:
- The minimum required Node.js version is 18.
Migrating from 7.x to 8.x
If you were using the default exported function:
- Renamed the default exported function to a named exported function
readSheet.- Old:
import readExcelFile from "read-excel-file/browser" - New:
import { readSheet } from "read-excel-file/browser" - And same for other exports like
"read-excel-file/node", etc.
- Old:
- 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']] }, ...]. - The default exported function used to return sheet names when passed
getSheets: trueparameter. Now, instead of that, the default exported function just returns all available sheets, from which one could get the sheet names.
- Renamed the default exported function to a named exported function
If you were using
readSheetNames()function:- Removed exported function
readSheetNames(). Use the default exported function instead. The default exported function now returns all sheets.
- Removed exported function
If you were using
parseExcelDate()function:- Removed exported function
parseExcelDate()because there seems to be no need to have it exported.
- Removed exported function
If you were using
schemaparameter:- Removed
schemaparameter. Instead, use exported functionparseData(data, schema)to map data to an array of objects.- Old:
import readXlsxFile from "read-excel-file"and thenconst { rows, errors } = await readXlsxFile(..., { schema }) - New:
import { readSheet, parseData } from "read-excel-file/browser"and thenconst result = parseData(await readSheet(...), schema)- The
resultof the function is an array where each element represents a "data row" and has shape{ object, errors }.- Depending on whether there were any errors when parsing a given "data row", either
objectorerrorsproperty will beundefined. - The
errorsdon't have arowproperty anymore because it could be derived from "data row" number.- In version
9.x, therowproperty has been re-added, so consider migrating straight to9.x.
- In version
- In version
9.x, the returned result ofparseData()has been changed back to{ errors, objects }, so consider migrating straight to9.x. In that case, if there're no errors,errorswill beundefined; otherwise,errorswill be a non-empty array andobjectswill beundefined. - In version
9.x, theschemaparameter was re-added toreadSheet()function, so consider migrating straight to9.x.
- Depending on whether there were any errors when parsing a given "data row", either
- The
- Old:
- Renamed some
schema-related parameters:schemaPropertyValueForMissingColumn→propertyValueWhenColumnIsMissingschemaPropertyValueForMissingValue→propertyValueWhenCellIsEmptyschemaPropertyShouldSkipRequiredValidationForMissingColumn→ (removed)getEmptyObjectValue→transformEmptyObject- The leading
.character is now removed from thepathparameter.
- The leading
getEmptyArrayValue→transformEmptyArray- The leading
.character is now removed from thepathparameter.
- The leading
- Previously, when using a
schemato parse comma-separated values, it used to ignore any commas that're surrounded by quotes, similar to how it's done in.csvfiles. Now it no longer does that. - Previously, when using a
schemato 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" }. - Previously, when using a
schemato parsetype: Dateproperties, it used to support bothDateobjects and numeric timestamps as the input data for the property value. In the latter case, it simply force-converted those numeric timestamps to correspondingDateobjects. NowparseData()function no longer does that, and demands the input data fortype: Dateschema properties to only beDateobjects, i.e. it shifts the responsibility to interpret date cell values correctly ontoreadSheet()andreadExcelFile()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". - Previously, when using a
schemato parse sheet data, and a given row of data was completely empty, it didn't run anyrequiredproperty validations. Now it no longer does that and it will run allrequiredproperty validations regardless of whether it's a completely empty row of data or not.
- Removed
If you were using
transformDataparameter:- Removed
transformDataparameter because theschemaparameter was extracted into a separate function calledparseData(). Now, if required, a developer could transform thedatamanually and then pass it toparseData()function.
- Removed
If you were using
isColumnOrientedparameter:- Removed
isColumnOrientedparameter because it seemed to be of no use.
- Removed
If you were using
ignoreEmptyRowsparameter:- Removed
ignoreEmptyRowsparameter. PassingignoreEmptyRows: trueparameter no longer makes it skip empty rows in the middle of a sheet. Now it's always the default behavior, as it used to be: only empty rows at the end of a sheet are ignored.
- Removed
If you were using TypeScript:
- Renamed some of the exported types:
Type→ParseDataCustomTypeErrororSchemaParseCellValueError→ParseDataErrorCellValueRequiredError→ParseDataValueRequiredErrorParsedObjectsResult→ParseDataResult
- Renamed some of the exported types:
Migrating from 8.x to 9.x
- If you were using
parseData()function:- Rewrote the code of the
parseData()function and renamed it toparseSheetData(). - The result of
parseSheetData()function is now{ errors, objects }. If there're no errors,errorswill beundefined. Otherwise,errorswill be a non-empty array andobjectswill beundefined.- Previously the result of
parseSheetData()function was[{ errors, object }, ...], i.e. theerrorswere split between each particular data row. Now theerrorsare combined for all data rows. The rationale is that it's simpler to handle the result of the function this way. - Re-added
row: numberproperty to theerrorobject. It's the number of the data row that caused the error, starting from1. - Added
columnIndex: numberproperty to theerrorobject.
- Previously the result of
- Renamed some of the exported TypeScript types:
ParseDataCustomType→ParseSheetDataCustomTypeParseDataCustomTypeErrorMessage→ParseSheetDataCustomTypeErrorMessageParseDataCustomTypeErrorReason→ParseSheetDataCustomTypeErrorReasonParseDataError→ParseSheetDataErrorParseDataValueRequiredError→ParseSheetDataValueRequiredErrorParseDataResult→ParseSheetDataResult
- In a
schema, a nested object could be declared as:{ required: true/false, schema: { ... } }. This is still true but therequiredflag is now only allowed to be eitherundefinedorfalse, sotruevalue is not allowed. The reason is quite simple. If a nested object as a whole is marked asrequired: true, and then it happens to be empty, a"required"error should be returned for it. But that error would also have to include acolumntitle, 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 withrequired: true, mark the specific required properties of it. - Re-added
schemaparameter toreadSheet()function.const { objects, errors } = readSheet(data, { schema })
- Rewrote the code of the
Install
npm install read-excel-file --save
Alternatively, it could be included on a web page directly via a <script/> tag.
Use
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.
For example, consider the following .xlsx file:
| Name | Date of Birth | Married | Kids |
|---|---|---|---|
| John Smith | 1/1/1995 | TRUE | 3 |
| Kate Brown | 3/1/2010 | FALSE | 0 |
Here's how to read it using readSheet() function:
import { readSheet } from 'read-excel-file/node'
await readSheet(file) ===
[
['Name', 'Date of Birth', 'Married', 'Kids'],
['John Smith', 1995-01-01T00:00:00.000Z, true, 3],
['Kate Brown', 2010-03-01T00:00:00.000Z, false, 0]
]
The result is an array of rows. Each row is an array of values — string, number, boolean or Date.
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.
But if you need to read all available "sheets" in a file, use the default exported function:
import readExcelFile from 'read-excel-file/node'
await readExcelFile(file) ===
[{
sheet: 'Sheet1',
data: [
['Name', 'Age'],
['John Smith', 30],
['Kate Brown', 15]
]
}, {
sheet: 'Sheet2',
data: ...
}]
The result is a non-empty array of "sheets". Each "sheet" is an object with properties:
sheet— Sheet name.- Example:
"Sheet1"
- Example:
data— Sheet data. An array of rows. Each row is an array of values —string,number,booleanorDate.- Example:
[ ['Name','Age'], ['John Smith',30], ['Kate Brown',15] ]
- Example:
Also, a very common use case is to read a list of JSON objects from an .xlsx file. To do that, pass a schema parameter to readSheet() function.
Import
This package provides a separate import path for each different environment, as described below.
Browser
read-excel-file/browser
It can read from a File, a Blob or an ArrayBuffer.
Example 1: Read from a selected file.
<input type="file" id="input" />
import { readSheet } from 'read-excel-file/browser'
const input = document.getElementById('input')
input.addEventListener('change', () => {
const data = await readSheet(event.target.files[0])
})
Example 2: Read from a URL.
import { readSheet } from 'read-excel-file/browser'
const response = await fetch('https://example.com/spreadsheet.xlsx')
const blob = await response.blob()
const data = await readSheet(blob)
Node.js
read-excel-file/node
It can read from a file path, a Stream, a Buffer or a Blob.
Example 1: Read from a file path.
import { readSheet } from 'read-excel-file/node'
const data = await readSheet('/path/to/file')
Example 2: Read from a Stream
import { readSheet } from 'read-excel-file/node'
const data = await readSheet(fs.createReadStream('/path/to/file'))
Universal
read-excel-file/universal
This one works both in a web browser and Node.js. It can only read from a Blob or an ArrayBuffer, which could be a bit less convenient for general use.
import { readSheet } from 'read-excel-file/universal'
const data = await readSheet(blob)
Note: the /universal export can't use workers so it's inherently "single-threaded" and "blocking".
Worker
When reading extremely large .xlsx files — say, starting from a few megabytes in size — there's a slight inconvenience of freezing the application during the "XML parsing" phase or "schema parsing" phase while reading the file.
To work around this minor issue in a web browser, one could read .xlsx files in a separate Web Worker using read-excel-file/web-worker export.
Example: Read a file using read-excel-file/web-worker in a Web Worker in a browser.
const worker = new Worker(new URL('worker.js', import.meta.url))
worker.onmessage = (event) => {
// File has been read.
console.log('Sheet data', event.data)
}
worker.onerror = (event) => {
// Handle errors here.
console.error(event.error)
}
// "Choose file" button.
const input = document.getElementById('input')
// When user chooses a file, send it to the Web Worker.
input.addEventListener('change', async () => {
const file = await event.target.files[0].arrayBuffer()
// Send the `.xlsx` file to the worker.
// (advanced) One could also pass `transferList` argument here.
worker.postMessage(file)
})
./worker.js
import { readSheet } from 'read-excel-file/web-worker'
onmessage = async (event) => {
postMessage(await readSheet(event.data))
}
Strings
By default, it automatically trims all string values. To disable this behavior, pass trim: false option.
readExcelFile(file, { trim: false })
Dates
Because .xlsx file format has no type for dates, it stores them as regular numbers but with a date-specific formatting template. By looking at the template, one could guess if it's a number or a date. This package seems to guess correctly.
Numbers
When reading an .xlsx file, any numeric values are parsed from a string to a javascript number. And that works for everyone, except when you work in science or finance or banking where numbers absolutely need to be 100% precise, in which case this section is for you, otherwise don't even bother reading it.
Why javascript numbers aren't 100% precise
"So aren't javascript numbers already 100% precise?", you ask. Here're some rather contrived examples:
1.0000000000000001becomes188259496234518.57becomes88259496234518.5699999999999999999999becomes100000000000000000000
You see, javascript numbers inherently come with a limited floating-point precision, which is apparently not enough in the examples shown above.
So what can one do then? For values that you know absolutely need to be 100% precise, use a custom implementation of "decimal" data type such as decimal.js. Specifically, pass a custom parseNumber(string) function as an option when reading an .xlsx file, and it will parse any number from string exactly the way you tell it.
Example 1: Parse any numbers as "decimals", exactly as they are specified in the .xlsx file.
import Decimal from 'decimal.js'
readExcelFile(file, {
parseNumber: (string) => new Decimal(string)
})
Example 2: Don't parse any numbers and just leave them as strings.
import Decimal from 'decimal.js'
readExcelFile(file, {
parseNumber: (string) => string
})
Formulas
When reading cells that use formulas to calculate their value, it doesn't really calculate the formula. Instead, it "cheats" by returning the value that is already pre-computed by the spreadsheet editor application. And that works for everyone.
Although I could hypothetically imagine a situation when a file is created not by a spreadsheet editor application, but rather by some hand-made script that doesn't bother pre-computing formulas, which is totally allowed by the specification, in which case such cells will simply be interpreted as empty ones.
Also, sometimes formulas can't be precomputed by a spreadsheet editor application due to an error, such as invalid syntax, or division by zero, or trying to add text to a number, or referenced row or column not found, etc. Such errors will be silently ignored and the cells will be interpreted as empty ones.
Errors
InvalidInputError
Sometimes people confuse .xlsx files with legacy binary .xls ones. The difference might be tricky to spot, so this package explicitly throws an InvalidInputError in such (and some other) cases.
name—"InvalidInputError"code— One of:"INPUT_TYPE_NOT_SUPPORTED"— The input argument is not of a supported type."XLS_FILE_NOT_SUPPORTED"— The input is a legacy binary.xlsfile (OLE2 Compound File Binary format), which is not supported. Such files should be re-saved in.xlsxformat in order to be readable by this package."FILE_NOT_SUPPORTED"— The input is neither.xlsxnor.xlsfile."INVALID_ZIP"— The input seems to be an.xlsxfile, and an.xlsxfile must be a valid ZIP archive, which it isn't."NO_DATA"— The input is empty.
InvalidSpreadsheetError
Will be thrown if there's something wrong with the .xlsx file contents while attempting to parse it.
name—"InvalidSpreadsheetError"
SheetNotFoundError
Will be thrown if a requested sheet doesn't exist.
name—"SheetNotFoundError"sheet— Sheet name or sheet numbersheets— Available sheet names
Performance
Here're the results of reading sample .xlsx files of different size:
| File Size | Browser | Node.js |
|---|---|---|
| 1 MB | 0.1 sec. | 0.1 sec. |
| 10 MB | 0.5 sec. | 0.5 sec. |
| 50 MB | 2.5 sec. | 2.5 sec. |
To run the benchmark in Node.js, clone the repository, download sample .xlsx files to ./test/benchmark folder, run npm install and then npm run test:benchmark:node.
To run the benchmark in a web browser, open the demo page, open the console and choose an .xlsx file.
Performance tips
Reading an .xlsx file is performed in 3 steps:
- Step 1. Unzip an
.xlsxfile into a tree of.xmlfiles. - Step 2. Parse sheet data from those
.xmlfiles. - Step 3. If
schemaoption was passed, use it to transform sheet data rows into JSON objects.
When running in Node.js, the unzip step is outsourced to unzipper-esm and is "asynchronous" — it uses Node.js "native" zlib module which unzips data in a separate thread.
When running in a web browser, the unzip step is outsourced to fflate which does it "asynchronously" only for .xlsx files larger than 512 KB (the threshold is hardcoded in fflate code).
The XML parsing step is written using saxen which is a SAX parser. This step is "synchronous".
The last step of converting sheet rows to JSON objects is only performed when schema option is passed. It is also "synchronous".
Schema
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 provides an easy way to do that — just pass a schema parameter when calling readSheet() function and it will automatically parse sheet data into an array of JSON objects according to that schema (which basically describes all properties of the object and which column should be mapped to which property).
The only requirement is that the sheet data 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.
| Name | Date of Birth | Married | Kids |
|---|---|---|---|
| John Smith | 1/1/1995 | TRUE | 3 |
| Kate Brown | 3/1/2010 | FALSE | 0 |
import { readSheet } from 'read-excel-file/node'
const schema = {
name: {
column: 'Name',
type: String
},
dateOfBirth: {
column: 'Date of Birth',
type: Date
},
married: {
column: 'Married',
type: Boolean
},
kids: {
column: 'Kids',
type: Number
}
}
const { objects, errors } = await readSheet(file, { schema })
if (errors) {
console.error(errors)
} else {
objects === [
{
name: 'John Smith',
dateOfBirth: 1995-01-01T00:00:00.000Z,
married: true,
kids: 3
},
{
name: 'Kate Brown',
dateOfBirth: 2010-03-01T00:00:00.000Z,
married: false,
kids: 0
}
]
}
The result is { objects, errors }
- If there were any errors,
objectswill beundefinedanderrorswill be a list of errors. - If there were no errors,
errorswill beundefinedandobjectswill be a list of objects.
schema should describe the structure of the resulting JSON objects. A slightly more complex example of a schema is provided at the end of this section.
Specifically, a schema should be an object having the same keys as a resulting JSON object, with values being nested objects having the following properties:
column— The title of the column to read the value from.- If the column does not exist, the property value will be
undefined.- This can be overridden by passing
propertyValueWhenColumnIsMissingoption. Isundefinedby default.
- This can be overridden by passing
- If the column exists but is empty, the property value will be
null.- This can be overridden by passing
propertyValueWhenCellIsEmptyoption. Isnullby default.
- This can be overridden by passing
- If the column does not exist, the property value will be
required— (optional) Is the value required? Could be one of:true— The column must exist and the cell value must not be empty.false— The column can be missing and the cell value can be empty.(object) => boolean— A function returningtrueorfalsedepending on the other properties.
validate(value)— (optional) Validates the value. Is only called for non-empty cells. If the value is invalid, this function should throw an error.schema— (optional) If the value is going to be a nested object,schemashould describe that nested object.- If when parsing such nested object, all of its properties are parsed as
undefinedornullthen the nested object itself will be set tonull.- This can be overridden by passing
transformEmptyObject(object, { path? })function as an option. By default, it returnsnull. - This applies both to nested objects and to the top-level object itself.
- This can be overridden by passing
- A nested object could be marked as
required: false— this will allow it to be completely absent from the spreadsheet, even if some of its properties are defined withrequired: trueflag. But if at least one property of such object is found in the spreadsheet then therequired: falseflag on the object has no longer any effect and anyrequired: trueproperties of the object are now required to exist.- Any other value except
falseis not allowed.
- Any other value except
- If when parsing such nested object, all of its properties are parsed as
type— (optional) If the value is not going to be a nested object, the expected type of the value could be specified in thetypeproperty, and then it will parse/validate the value according to that type.- Valid
types:- Standard types:
StringNumberBooleanDate
- One of the "utility" types that're exported from this package:
IntegerEmailURL
- Custom type:
- A function that receives a cell value and returns any kind of a parsed value. Returning
undefinedwill have same effect as returningnull. If the value is invalid, it should throw an error.
- A function that receives a cell value and returns any kind of a parsed value. Returning
- Standard types:
- If the cell value is comprised of comma-separated values (example:
"a, b, c") and if it should be parsed as an array of such values, then the propertytypecould be specified as an array —type: [elementType]— whereelementTypecould be any validtypedescribed above. For example, if a property is defined as{ type: [String] }and the cell value is"a, b, c"then the property value will be parsed as["a", "b", "c"].- If the cell is empty, or if every element of the parsed array is
nullorundefined, then the property value itself will be set tonull.- This can be overridden by passing
transformEmptyArray(array, { path })function as an option. By default, it returnsnull.
- This can be overridden by passing
- The separator could be specified by passing
arrayValueSeparatoroption. By default, it's",". - The separated parts of a cell value will be trimmed.
- If the cell is empty, or if every element of the parsed array is
- Valid
If there're any errors during the conversion process, the errors property returned from the function will be a non-empty array (by default, it's an empty array). Each error object has properties:
error(string) — Error code. Examples:"required","invalid".- If a custom
validate()function is defined and it throws anew Error(message)then theerrorproperty will be the same as themessageargument. - If a custom
type()function is defined and it throws anew Error(message)then theerrorproperty will be the same as themessageargument.
- If a custom
reason?: string— An optional secondary error code providing more details about the error. I.e. "error.errorhappened specifically because oferror.reason". Currently, it could only be returned for the standardtypes.- Example:
{ error: "invalid", reason: "not_a_number" }for atype: Numberproperty means that "the cell value is invalid because it's not a number".
- Example:
row(number) — Data row number, starting from1.row: 1means "first row of data", etc.- The header row is ignored.
column(string) — Column title.columnIndex(number) — Column index.columnIndex: 0means "first column", etc.
value— Cell value, when present.type— Thetypeof the property, as defined in theschema.
Example:
// An example .xlsx document:
// --------------------------------------------------------------------------------------------------------
// | START DATE | SEATS | STATUS | CONTACT | COURSE TITLE | COURSE CATEGORY | COURSE IS FREE |
// --------------------------------------------------------------------------------------------------------
// | 03/24/2018 | 10 | SCHEDULED | (123) 456-7890 | Basic Algebra | Math, Arithmetic | TRUE |
// --------------------------------------------------------------------------------------------------------
const schema = {
startDate: {
column: 'START DATE',
type: Date
},
seats: {
column: 'SEATS',
type: Number,
required: true
},
status: {
column: 'STATUS',
type: String,
// An example of using `oneOf`
oneOf: [
'SCHEDULED',
'STARTED',
'FINISHED'
]
},
contact: {
column: 'CONTACT',
required: true,
// An example of using a custom `type`
type: PhoneNumber
},
// Nested object example
course: {
// A nested object could be declared as completely optional by specifying `required: false`.
// In that case, when all of its properties are missing from the input data, it wouldn't throw any error
// regardless of whether some of its properties are declared as `required: true` or not.
required: false,
schema: {
title: {
column: 'COURSE TITLE',
type: String,
// When course data is present, the course title must be specified.
required: true
},
categories: {
column: 'COURSE CATEGORY',
// An example of parsing comma-separated values.
type: [String]
},
isFree: {
column: 'COURSE IS FREE',
type: Boolean
}
}
}
}
// If this code was written in TypeScript, `schema` would've been declared as:
// const schema: Schema<Object, ColumnTitle> = { ... }
// Read `data` from an `.xlsx` file and parse it using a `schema`.
const { objects, errors } = await readSheet(file, { schema })
// There have been no errors when parsing the sheet data, so `errors` is `undefined`.
// Should there have been any errors when parsing the sheet data, `errors` would've been
// an array of items having shape: `{ row, column, error, reason?, value?, type? }`.
errors === undefined
// There's one data row in the `.xlsx` file.
objects.length === 1
// The parsed data row.
objects[0] === {
startDate: new Date(Date.UTC(2018, 3 - 1, 24)),
seats: 10,
status: 'SCHEDULED',
contact: '+11234567890',
course: {
title: 'Basic Algebra',
categories: ['Math', 'Arithmetic']
isFree: true
}
}
// An example of a custom `type` parser function.
// It will parse the cell value when it's not empty.
function PhoneNumber(value) {
const number = parsePhoneNumber(value)
if (!number) {
throw new Error('invalid')
}
return number
}
Also, for convenience, this package exports the same feature as a separate function — parseSheetData(sheetData, schema).
import { readSheet, parseSheetData } from 'read-excel-file/node'
const schema = { ... }
const sheetData = await readSheet(file)
const { objects, errors } = parseSheetData(sheetData, schema)
if (errors) {
console.error(errors)
} else {
console.log(objects)
}
An example of defining a custom type in TypeScript
import type {
Schema,
CellValue,
ParseSheetDataError,
ParseSheetDataCustomType,
ParseSheetDataCustomTypeErrorMessage
} from 'read-excel-file/node'
type ColumnTitle = 'COLUMN TITLE 1' | 'COLUMN TITLE 2'
type CustomTypeValue = string
function CustomType(value: CellValue): CustomTypeValue {
if (typeof value !== 'string') {
throw new Error('not_a_string')
}
return '~' + value + '~'
}
type CustomTypeErrorMessage<Type extends ParseSheetDataCustomType<unknown>> =
Type extends typeof CustomType
? 'not_a_string'
: never
// type CustomTypeErrorReason<
// Type extends ParseSheetDataCustomType<unknown>,
// ErrorMessage extends ParseSheetDataCustomTypeErrorMessage<Type>
// > =
// Type extends typeof CustomType
// ? (ErrorMessage extends 'not_a_string' ? undefined : never)
// : never
type PossibleError = ParseSheetDataError<
ColumnTitle,
typeof CustomType,
CustomTypeErrorMessage<typeof CustomType>
// CustomTypeErrorReason<typeof CustomType, CustomTypeErrorMessage<typeof CustomType>>
>
interface Object {
property1: CustomTypeValue;
property2?: string;
}
const schema: Schema<Object, ColumnTitle> = {
property1: {
column: 'COLUMN TITLE 1',
type: CustomType,
required: true
},
property2: {
column: 'COLUMN TITLE 2',
type: String
}
}
const { objects, errors } = parseSheetData<Object, ColumnTitle, PossibleError>([
['COLUMN TITLE 1', 'COLUMN TITLE 2'],
['Value 1', 'Value 2']
], schema)
if (errors) {
for (const error of errors) {
console.error('Error in data row', error.row, 'column', error.column, ':', error.error, error.reason || '')
}
} else {
console.log('Objects', objects)
}
An example of a React component to output errors
function ErrorsList({ errors }) {
return (
<ul>
{errors.map((error, i) => (
<li key={i}>
<ErrorItem error={error}>
</li>
))}
</ul>
)
}
function ErrorItem({ error }) {
const {
error: errorMessage,
reason,
row,
column,
columnIndex,
value,
type
} = error
// Error summary.
return (
<div>
<code>"{errorMessage}"</code>
{reason && ' '}
{reason && <code>("{reason}")</code>}
{' for value '}
<code>{stringifyValue(value)}</code>
{' in column '}
<code>"{column}"</code>
{' in data row '}
<code>{row}</code>
{' of the spreadsheet'}
</div>
)
}
function stringifyValue(value) {
// Wrap strings in quotes.
if (typeof value === 'string') {
return '"' + value + '"'
}
return String(value)
}
Browser Support
An .xlsx file is just a .zip archive with an .xslx file extension. This package uses fflate for .zip decompression. See fflate's browser support for further details.
CDN
To include this library directly via a <script/> tag on a page, one can use any npm CDN service, e.g. unpkg.com or jsdelivr.com
<script src="https://unpkg.com/read-excel-file@9.x/bundle/read-excel-file.min.js"></script>
<script>
var input = document.getElementById('input')
input.addEventListener('change', function() {
readXlsxFile(event.target.files[0]).then(function(rows) {
// `rows` is an array of rows
// each row being an array of cells.
})
})
</script>
Dependencies
fflate— Unzips.ziparchives in web browsers.unzipper-esm— Unzips.ziparchives in Node.js usingstreamAPI.saxen— Parses XML in a streaming fashion.
Contributors
- Stian Jensen — Use
fflateunzipper on server side (1, 2) - Etienne Prothon — Reject non
.xlsxfiles, including the legacy binary.xlsfiles (1). Fix parsing of "encoded" characters (1).
GitHub
On March 9th, 2020, GitHub, Inc. silently banned my account (erasing all my repos, issues and comments, even in my employer's private repos) without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The GitHub repo is now only used as a backup (you can star the repo there too), and the primary repo is now the GitLab one. Issues can be reported in any repo.