@leadinvr/excel-parser
v1.0.6
Published
Parser for MS Excel
Readme
excel-parser
Features
- Parse excel files
Install
npm i @leadinvr/excel-parserQuick Start
Parse an xlsx file
try {
const file = new ExcelFile();
file.open(path);
return file;
} catch (err) {
logger.error(path, err);
throw err;
}Select a book
excel.use(0); // or excel.use("sheet name")Get all keys
const keys = excel.getSheetKeys();Iterate rows
while (excel.next()) {
// get next row, and iterate all keys
for (const key of keys) {
const v = excel.getCell(key, "");
if (isNullOrEmpty(v)) {
continue;
}
if (!map.has(key)) {
map.set(key, []);
}
map.get(key)!.push(v ?? "");
}
}Decorators
- When call parseTo, parser use decorator to get excel cell from column
/**
* Example Data Class
*/
export class TestData {
// name
@ExcelColumn("name")
name: string = "";
@ExcelColumn("district")
district: string = "";
@ExcelColumn("type")
type: string = "";
@ExcelColumn("subtype")
subtype: string = "";
@ExcelColumnAll()
properties: unknown = {};
}- ExcelColumn
The field value will be cell under the argument key
- ExcelColumnAll
The field value will be a key-value object for all key in excel
Parse row to an object
- It dependent on decorators
while (excel.next()) {
const obj = new TestData(); // Object class is any typescript class
// the obj should has decorators on it's field
excel.parseTo(obj);
}