2.0.8 • Published 2 years ago

excels-loader v2.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

This is a WebPack5 Loader library for converting Excel files into JSON objects

npm install --save-dev excels-loader

webpack.config.js

module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader'
                }
            }
        ]
    }
}
// Parse to line 3
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                        readOptions: {
                            sheetRows: 3
                        }
                    }
                }
            }
        ]
    }
}
// Parsing ’sheet1‘ table
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                        readOptions: {
                            sheets: 'sheet1',
                        }
                    }
                }
            }
        ]
    }
}
// Parsing first table
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                        readOptions: {
                            sheets: 0,
                        }
                    }
                }
            }
        ]
    }
}
// Parsing 'Sheet2' table
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                        readOptions: {
                            sheets: ['Sheet2'],
                        }
                    }
                }
            }
        ]
    }
}
// Parsing 'sheet1' table, after 2 line and ['id', 'name', 'age'] column
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                        readOptions: {
                            sheets: 'sheet1',
                        },
                        parseOptions: { range: 2, header: ['id', 'name', 'age'] }
                    }
                }
            }
        ]
    }
}
// AfterParseCallback function processes data
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                       readOptions: {
                            sheets: 'sheet1',
                        },
                        parseOptions: {
                            range: 2,
                            header: ['id', 'name', 'age'],
                            afterParseCallback: (data) => {
                                return data.reduce((result, cur) => {
                                    result[cur.id] = { name: cur.name, age: cur.age };
                                    return result;
                                }, {})
                            }
                        }
                    }
                }
            }
        ]
    }
}
// The parseOptions array corresponds to the sheet array
module.exports = {
  module: {
    rules: [
            {
                test: /\.xls.?$/,
                use: {
                    loader: 'excels-loader',
                    {
                       parseOptions: [{ range: 2, header: ['id', 'name', 'age'] }]
                    }
                }
            }
        ]
    }
}
NameTypeDefaultDescription
ReadOptions{Object}undefinedread excel file option
parseOptions{Object、Array}undefinedparse json option

ReadOptions

Option NameDefaultDescription
typeInput data encoding (see Input Type below)
rawfalseIf true, plain text parsing will not parse values **
codepageIf specified, use code page when appropriate **
cellFormulatrueSave formulae to the .f field
cellHTMLtrueParse rich text and save HTML to the .h field
cellNFfalseSave number format string to the .z field
cellStylesfalseSave style/theme info to the .s field
cellTexttrueGenerated formatted text to the .w field
cellDatesfalseStore dates as type d (default is n)
dateNFIf specified, use the string for date code 14 **
sheetStubsfalseCreate cell objects of type z for stub cells
sheetRows0If >0, read the first sheetRows rows **
bookDepsfalseIf true, parse calculation chains
bookFilesfalseIf true, add raw files to book object **
bookPropsfalseIf true, only parse enough to get book metadata **
bookSheetsfalseIf true, only parse enough to get the sheet names
bookVBAfalseIf true, copy VBA blob to vbaraw field **
password""If defined and file is encrypted, use password **
WTFfalseIf true, throw errors on unexpected file features **
sheetsIf specified, only parse specified sheets **
PRNfalseIf true, allow parsing of PRN files **
xlfnfalseIf true, preserve _xlfn. prefixes in formulae **

https://docs.sheetjs.com/docs/api/parse-options

parseOptions

Option NameDefaultDescription
afterParseCallbackfunctionProvides the callback after the sheet is parsed into JSON. The parameter is the parsed JSON data. You can edit the returned JSON result here
rawtrueUse raw values (true) or formatted strings (false)
rangefrom WSOverride Range (see table below)
headerControl output format (see table below)
dateNFFMT 14Use specified date format in string output
defvalUse specified value in place of null or undefined
blankrows**Include blank lines in the output **
  • raw only affects cells which have a format code (.z) field or a formatted text (.w) field.
  • If header is specified, the first row is considered a data row; if header is not specified, the first row is the header row and not considered data.
  • When header is not specified, the conversion will automatically disambiguate header entries by affixing _ and a count starting at 1. For example, if three columns have header foo the output fields are foo, foo_1, foo_2
  • null values are returned when raw is true but are skipped when false.
  • If defval is not specified, null and undefined values are skipped normally. If specified, all null and undefined points will be filled with defval
  • When header is 1, the default is to generate blank rows. blankrows must be set to false to skip blank rows.
  • When header is not 1, the default is to skip blank rows. blankrows must be true to generate blank rows

range is expected to be one of:

rangeDescription
(number)Use worksheet range but set starting row to the value
(string)Use specified range (A1-style bounded range string)
(default)Use worksheet range (ws['!ref'])

header is expected to be one of:

headerDescription
1Generate an array of arrays ("2D Array")
"A"Row object keys are literal column labels
array of stringsUse specified strings as keys in row objects
(default)Read and disambiguate first row as keys

If header is not 1, the row object will contain the non-enumerable property __rowNum__ that represents the row of the sheet corresponding to the entry.

For the example sheet:

> XLSX.utils.sheet_to_json(ws);
[ { S: 1, h: 2, e: 3, e_1: 4, t: 5, J: 6, S_1: 7 },
  { S: 2, h: 3, e: 4, e_1: 5, t: 6, J: 7, S_1: 8 } ]

> XLSX.utils.sheet_to_json(ws, {header:"A"});
[ { A: 'S', B: 'h', C: 'e', D: 'e', E: 't', F: 'J', G: 'S' },
  { A: '1', B: '2', C: '3', D: '4', E: '5', F: '6', G: '7' },
  { A: '2', B: '3', C: '4', D: '5', E: '6', F: '7', G: '8' } ]

> XLSX.utils.sheet_to_json(ws, {header:["A","E","I","O","U","6","9"]});
[ { '6': 'J', '9': 'S', A: 'S', E: 'h', I: 'e', O: 'e', U: 't' },
  { '6': '6', '9': '7', A: '1', E: '2', I: '3', O: '4', U: '5' },
  { '6': '7', '9': '8', A: '2', E: '3', I: '4', O: '5', U: '6' } ]

> XLSX.utils.sheet_to_json(ws, {header:1});
[ [ 'S', 'h', 'e', 'e', 't', 'J', 'S' ],
  [ '1', '2', '3', '4', '5', '6', '7' ],
  [ '2', '3', '4', '5', '6', '7', '8' ] ]

Example showing the effect of raw:

> ws['A2'].w = "3";                          // set A2 formatted string value

> XLSX.utils.sheet_to_json(ws, {header:1, raw:false});
[ [ 'S', 'h', 'e', 'e', 't', 'J', 'S' ],
  [ '3', '2', '3', '4', '5', '6', '7' ],     // <-- A2 uses the formatted string
  [ '2', '3', '4', '5', '6', '7', '8' ] ]

> XLSX.utils.sheet_to_json(ws, {header:1});
[ [ 'S', 'h', 'e', 'e', 't', 'J', 'S' ],
  [ 1, 2, 3, 4, 5, 6, 7 ],                   // <-- A2 uses the raw value
  [ 2, 3, 4, 5, 6, 7, 8 ] ]

https://docs.sheetjs.com/docs/api/utilities/#array-of-objects-input