datainout
Version:
Import and reports data
109 lines (108 loc) • 3.95 kB
JavaScript
// src/helpers/exceljs-exporter-helper.ts
var ExceljsExporterHelper = class {
constructor(templateManager, useStyle) {
this.templateManager = templateManager;
this.useStyle = useStyle != null ? useStyle : false;
}
filterGroupCellDesc(opts, sheetIndex) {
return this.templateManager.GroupCells[opts];
}
getSheetInformation() {
return this.templateManager.SheetInformation;
}
setCell(rowData, cellOpt, cell) {
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;
}
setTitleTable(cellsOpts, workSheet) {
const groupTitles = cellsOpts.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;
}, {});
const titelTables = [];
for (const [rowIndex, footers] of Object.entries(groupTitles)) {
const row = workSheet.addRow([]);
footers.forEach((footer) => this.setCell({}, footer, row.getCell(footer.fullAddress.col)));
titelTables.push(row);
}
return titelTables;
}
addRow(value, cellsOpts, workSheet) {
const row = workSheet.addRow([]);
for (let i = 0; i < cellsOpts.length; i++) {
const cellsOpt = cellsOpts[i];
const cell = row.getCell(cellsOpt.fullAddress.col);
this.setCell(value, cellsOpt, cell);
}
return row;
}
addRows(values, cellsOpts, workSheet) {
const rows = values.map((value) => {
const row = workSheet.addRow([]);
for (let i = 0; i < cellsOpts.length; i++) {
const cellsOpt = cellsOpts[i];
const cell = row.getCell(cellsOpt.fullAddress.col);
this.setCell(value, cellsOpt, cell);
}
if (!this.useStyle) row.commit();
return row;
});
return rows;
}
setHeader(header, cellsOpts, beginTableAt, workSheet) {
const rowHeaders = [];
for (let i = 1; i <= beginTableAt; i++) {
const row = workSheet.addRow([]);
const formats = cellsOpts.filter((e) => e.fullAddress.row === i);
formats.forEach((format) => this.setCell(header != null ? header : {}, format, row.getCell(format.fullAddress.col)));
rowHeaders.push(row);
}
return rowHeaders;
}
setFooter(value, cellsOpts, workSheet) {
const groupFooter = cellsOpts.sort((a, b) => b.fullAddress.row - a.fullAddress.row).reduce((acc, value2) => {
const row = value2.fullAddress.row;
acc[row] = acc[row] ? [...acc[row], value2] : [value2];
return acc;
}, {});
const _footers = [];
for (const [rowIndex, footers] of Object.entries(groupFooter)) {
const row = workSheet.addRow([]);
footers.forEach((footer) => this.setCell(value != null ? value : {}, footer, row.getCell(footer.fullAddress.col)));
_footers.push(row);
}
return _footers;
}
mergeCells(sheet, sheetFormat) {
if (sheetFormat.merges) {
const merges = sheetFormat.merges;
Object.keys(sheetFormat.merges).forEach((masterCell) => {
const { top, left, right, bottom } = merges[masterCell].model;
sheet.mergeCells(top, left, bottom, right);
});
}
}
setWidthsAndHeights(sheet, sheetFormat) {
var _a;
(_a = sheetFormat.columnWidths) == null ? void 0 : _a.forEach((colW, i) => {
if ((sheet == null ? void 0 : sheet.columns) && (sheet == null ? void 0 : sheet.columns[i]) && colW) sheet.columns[i].width = colW;
});
const rowHeights = sheetFormat.rowHeights;
Object.keys(rowHeights).forEach((rowIndex, i) => {
if (rowHeights[rowIndex]) sheet.getRow(rowHeights[i]).height = rowHeights[rowIndex];
});
}
};
export {
ExceljsExporterHelper
};