datainout
Version:
Import and reports data
178 lines (176 loc) • 6.38 kB
JavaScript
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/reporters/exporters/proccessor/ExcelProcessor.ts
var ExcelProcessor = class {
constructor(opts) {
this.template = {};
this.style = "use-style";
this.titlesTable = [];
this.columnKeys = [];
var _a;
this.workBook = opts.workBook;
this.headerData = opts.header;
this.footerData = opts.footer;
this.template = opts.template;
this.style = (_a = opts.style) != null ? _a : "use-style";
this.event = opts.event;
this.titlesTable = this.template.GroupCells.table.map((e) => {
var _a2;
return (_a2 = e.value.fieldName) != null ? _a2 : "";
});
this.columnKeys = opts.style !== "no-style-no-header" ? void 0 : this.createColumnKey();
}
createColumnKey() {
const tableCells = this.template.GroupCells.table;
const columns = this.template.GroupCells.header.map((e, i) => {
var _a;
return {
header: e.value.hardValue,
key: (_a = tableCells[i].value.fieldName) != null ? _a : ""
};
});
return columns;
}
getOrCreateWorksheet(name) {
var _a;
return (_a = this.workBook.getWorksheet(name)) != null ? _a : this.workBook.addWorksheet(name);
}
setHeader(headerData, arg) {
if (!this.template.GroupCells.header) return;
const workSheet = typeof arg === "string" ? this.getOrCreateWorksheet(arg) : arg;
for (let i = 1; i <= this.template.SheetInformation.beginTableAt; i++) {
const formats = this.template.GroupCells.header.filter((e) => e.fullAddress.row === i);
this.addRow(headerData, workSheet, formats);
}
this.event.emitEvent("header", workSheet.name);
}
pushData(sheetName, batches, isFinish = false) {
if (!this.workBook.getWorksheet(sheetName)) {
this.event.emitEvent("begin", sheetName);
const workSheet2 = this.workBook.addWorksheet(sheetName);
if (this.columnKeys) workSheet2.columns = this.columnKeys;
else this.setHeader(this.headerData, workSheet2);
}
const workSheet = this.getOrCreateWorksheet(sheetName);
if (isFinish) {
this.setFooter(this.footerData, workSheet);
this.finalizeWorksheet(sheetName);
return;
}
if (Array.isArray(batches)) {
for (let i = 0; i < batches.length; i++) {
if (this.style === "no-style-no-header") workSheet.addRow(batches[i]).commit();
else if (this.style === "use-style") this.addRow(batches[i], workSheet, this.template.GroupCells.table);
else this.addRowWithoutStyle(batches[i], workSheet);
}
} else if (batches !== null) this.addRow(batches, workSheet, this.template.GroupCells.table);
}
setFooter(footerData, arg) {
const workSheet = typeof arg === "string" ? this.getOrCreateWorksheet(arg) : arg;
if (this.template.GroupCells.footer) {
const groupFooter = this.template.GroupCells.footer.sort((a, b) => b.fullAddress.row - a.fullAddress.row).reduce((acc, value) => {
const row = value.fullAddress.row;
acc[row] = acc[row] ? [...acc[row], value] : [value];
return acc;
}, {});
for (const [rowIndex, footers] of Object.entries(groupFooter)) {
this.addRow(footerData, workSheet, footers);
}
}
if (this.style === "use-style") this.mergeCells(workSheet);
if (this.style === "use-style") this.setWidthsAndHeights(workSheet);
this.event.emitEvent("footer", workSheet.name);
}
finalizeWorksheet(sheetName) {
this.event.emitEvent("end", sheetName);
}
addRowWithoutStyle(rowdata, workSheet) {
const row = [];
for (let i = 0; i < this.titlesTable.length; i++) {
row.push(rowdata[this.titlesTable[i]]);
}
workSheet.addRow(row).commit();
}
addRow(data, workSheet, cellDesc) {
const row = workSheet.addRow([]);
for (let i = 0; i < cellDesc.length; i++) {
const cellsOpt = cellDesc[i];
const cell = row.getCell(cellsOpt.fullAddress.col);
this.setCell(data, cellsOpt, cell);
}
return row;
}
setCell(rowData, cellOpt, cell) {
if (this.style === "use-style") cell.style = cellOpt.style;
if (cellOpt.formula) {
cell.value = {
formula: cellOpt.formula
};
} else {
let value = cellOpt.isVariable ? rowData[cellOpt.value.fieldName] : cellOpt.value.hardValue;
if (cellOpt.formatValue) value = cellOpt.formatValue(value);
cell.value = value;
}
return cell;
}
mergeCells(sheet) {
const merges = this.template.SheetTemplate.merges;
if (merges) {
Object.keys(merges).forEach((masterCell) => {
const { top, left, right, bottom } = merges[masterCell].model;
sheet.mergeCells(top, left, bottom, right);
});
}
}
setWidthsAndHeights(sheet) {
const columnWidths = this.template.SheetTemplate.columnWidths;
const rowHeights = this.template.SheetTemplate.rowHeights;
columnWidths == null ? void 0 : columnWidths.forEach((colW, i) => {
if ((sheet == null ? void 0 : sheet.columns) && (sheet == null ? void 0 : sheet.columns[i]) && colW) sheet.columns[i].width = colW;
});
if (rowHeights)
Object.keys(rowHeights).forEach((rowIndex, i) => {
if (rowHeights[rowIndex]) sheet.getRow(rowHeights[i]).height = rowHeights[rowIndex];
});
}
};
var ExcelStreamProcessor = class extends ExcelProcessor {
finalizeWorksheet(sheetName) {
const worksheet = this.getOrCreateWorksheet(sheetName);
worksheet.commit();
this.event.emitEvent("end", sheetName);
}
addRow(data, workSheet, cellDesc) {
const row = super.addRow(data, workSheet, cellDesc);
if (this.style !== "use-style") row.commit();
return row;
}
finalizeWorkbook() {
return __async(this, null, function* () {
yield this.workBook.commit();
this.event.emitEvent("finish");
});
}
};
export {
ExcelProcessor,
ExcelStreamProcessor
};