UNPKG

datainout

Version:

Import and reports data

217 lines (215 loc) 8.52 kB
var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/helpers/excel.helper.ts import * as exceljs from "exceljs"; var SYNTAX = { VARIABLE_TABLE_SYNTAX: "$$", VARIABLE_SYNTAX: "$", INDEX_COLUMN_TABLE_SYNTAX: "$$**", END_TABLE_SYNYAX: "$$br" }; var DEFAULT_BEGIN_TABLE = -1; var DEFAULT_END_TABLE = -1; var DEFAULT_COLUMN_INDEX = 1; var ReaderExceljsHelper = class _ReaderExceljsHelper { constructor(opts) { this.isSampleExcel = true; this.isStop = false; var _a; this.onCell = opts == null ? void 0 : opts.onCell; this.onRow = opts == null ? void 0 : opts.onRow; this.onSheet = opts == null ? void 0 : opts.onSheet; this.isSampleExcel = (_a = opts == null ? void 0 : opts.isSampleExcel) != null ? _a : true; this.templateManager = opts.templateManager; } load(arg) { return __async(this, null, function* () { const workBook = new exceljs.Workbook(); if (arg instanceof Buffer) yield workBook.xlsx.load(arg); else yield workBook.xlsx.readFile(arg); for (let i = 0; !this.isStop && i < workBook.worksheets.length; i++) { const workSheet = workBook.getWorksheet(i + 1); if (workSheet) yield this.eachSheet(workSheet, i + 1); } }); } eachSheet(sheet, sheetIndex) { return __async(this, null, function* () { var _a, _b, _c, _d; this.templateManager.SheetIndex = sheetIndex - 1; const sheetDesc = this.templateManager.SheetTemplate; const trackingRows = []; let columnIndex = DEFAULT_COLUMN_INDEX; let beginTable = DEFAULT_BEGIN_TABLE; let endTable = DEFAULT_END_TABLE; for (let i = 1; !this.isStop && i <= sheet.rowCount; i++) { const row = sheet.getRow(i); this.templateManager.defineActualTableStartRow(_ReaderExceljsHelper.beginTableAt(row, sheetDesc, this.isSampleExcel)); this.templateManager.defineActualTableStartRow(_ReaderExceljsHelper.endTableAt(row, sheetDesc, this.isSampleExcel)); columnIndex = (_a = _ReaderExceljsHelper.columnTableIndex(row, sheetDesc, this.isSampleExcel)) != null ? _a : columnIndex; endTable = (_b = this.templateManager.ActualTableEndRow) != null ? _b : DEFAULT_END_TABLE; beginTable = (_c = this.templateManager.ActualTableStartRow) != null ? _c : DEFAULT_BEGIN_TABLE; const section = _ReaderExceljsHelper.getSection(row, beginTable, endTable); const cells = []; for (let j = 0; j < row.cellCount; j++) { if (!((_d = row.getCell(j + 1)) == null ? void 0 : _d.value)) continue; const cell = this.convertCell(row.getCell(j + 1), section, beginTable, endTable); cells.push(cell); if (this.onCell) yield this.onCell(cell); } if (this.onRow) { const rowDataHelper = this.convertRow(row, section, cells); if (i === 1) trackingRows.push(rowDataHelper); if (i === sheet.rowCount) trackingRows.push(rowDataHelper); yield this.onRow(rowDataHelper); } } if (this.onSheet) yield this.onSheet({ beginTableAt: beginTable != null ? beginTable : 1, columnIndex, endTableAt: endTable, sheetIndex, name: sheet.name, detail: sheet, rowCount: sheet.rowCount, lastestRow: trackingRows[1], firstRow: trackingRows[0] }); }); } convertCell(cell, section, beginTableAt, endTableAt) { var _a; const isVariable = _ReaderExceljsHelper.isVariable(cell.value); return { address: _ReaderExceljsHelper.getAddress(cell.address, section, isVariable), detail: cell, isVariable, rowIndex: +cell.fullAddress.row, section, label: _ReaderExceljsHelper.getLabel(cell.value + "", isVariable), variableValue: isVariable ? _ReaderExceljsHelper.getVariableValue(cell) : void 0, beginTableAt, endTableAt, formula: (_a = cell.formula) != null ? _a : void 0 }; } convertRow(row, section, cells) { var _a, _b, _c, _d; return { detail: row, rowIndex: row.number, section, cells, beginTableAt: (_b = (_a = cells[0]) == null ? void 0 : _a.beginTableAt) != null ? _b : DEFAULT_BEGIN_TABLE, endTableAt: (_d = (_c = cells[0]) == null ? void 0 : _c.endTableAt) != null ? _d : DEFAULT_END_TABLE }; } static getSection(row, beginTable, endTable) { const rowIndex = typeof row === "number" ? row : row.number; let section = "header"; if (beginTable === DEFAULT_BEGIN_TABLE) section = "header"; else if (beginTable !== DEFAULT_BEGIN_TABLE && rowIndex < beginTable) section = "header"; else if (rowIndex > endTable && endTable !== DEFAULT_END_TABLE) section = "footer"; else if (rowIndex > beginTable) section = "table"; return section; } /** Get begin table at by row */ static beginTableAt(row, sheetOpts, isSampleExcel = true) { if (isSampleExcel) for (let i = 0; i < row.cellCount; i++) { const cellValue = row.getCell(i + 1).value; if (cellValue === void 0 || typeof cellValue !== "string") continue; if (cellValue.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return row.number; if (cellValue.includes(SYNTAX.VARIABLE_TABLE_SYNTAX)) return row.number - 1; } else if (sheetOpts) return sheetOpts.beginTableAt; return void 0; } /** Get end table at by row */ static endTableAt(row, sheetOpts, isSampleExcel = true) { var _a; if (isSampleExcel) for (let i = 0; i < row.cellCount; i++) { const cellValue = row.getCell(i + 1).value; if (cellValue === void 0 || typeof cellValue !== "string") continue; if (cellValue.includes(SYNTAX.END_TABLE_SYNYAX)) return row.number - 1; if (!cellValue.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX) && cellValue.includes(SYNTAX.VARIABLE_TABLE_SYNTAX)) return row.number; } else if (sheetOpts) { const cellvalues = (_a = row == null ? void 0 : row.values) != null ? _a : []; if (cellvalues && !cellvalues[sheetOpts.keyTableAt] && row.number > sheetOpts.beginTableAt) return row.number - 1; } return void 0; } static isNullableRow(row) { var _a, _b; return (_b = (_a = row == null ? void 0 : row.values) == null ? void 0 : _a.reduce((acc, val) => acc && !!!val, true)) != null ? _b : false; } /** Get key of table by row */ static columnTableIndex(row, sheetOpts, isSampleExcel = true) { if (isSampleExcel) for (let i = 0; i < row.cellCount; i++) { const cellValue = row.getCell(i + 1).value; if (cellValue === void 0 || typeof cellValue !== "string") continue; if ((cellValue + "").includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return row.number + 1; } else if (sheetOpts) return sheetOpts.keyTableAt; return void 0; } static isVariable(cellValue) { cellValue = cellValue + ""; if (cellValue.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return false; return cellValue.includes(SYNTAX.VARIABLE_SYNTAX); } static getLabel(label, isVariable) { if (isVariable) return void 0; if (label.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return label.split("->").pop(); return label; } static getAddress(address, section, isVariable) { return !isVariable ? address : section !== "header" ? address.split(/\d+/)[0] : address; } static getVariableValue(cell) { const cellValue = (cell.value + "").replace("$$", "$"); let fieldName = ""; let type = "string"; fieldName = cellValue.split("$")[1]; if (fieldName.includes("->")) { const args = fieldName.split("->"); fieldName = args[0]; type = args[1].toLowerCase(); } return { fieldName, type }; } static splitAddress(address) { const col = address.split(/\d+/)[0]; const row = address.split(/[a-zA-Z]/)[1]; return { col, row }; } }; export { DEFAULT_BEGIN_TABLE, DEFAULT_COLUMN_INDEX, DEFAULT_END_TABLE, ReaderExceljsHelper, SYNTAX };