UNPKG

@webilix/nestjs-helper

Version:

Helper library for NestJS

190 lines 8.72 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExportExcelService = void 0; const common_1 = require("@nestjs/common"); const excelJS = __importStar(require("exceljs")); const export_type_1 = require("../export.type"); let ExportExcelService = class ExportExcelService { constructor(config) { this.config = config; } export(path, table) { const fill = { type: 'pattern', pattern: 'solid', fgColor: { argb: this.config.backgroundColor }, }; return new Promise(async (resolve, reject) => { try { const workbook = new excelJS.Workbook(); const sheet = workbook.addWorksheet(table.title.replace(/[\*\?:\\\/\[\]]/gi, '-'), { views: [ { rightToLeft: true, state: 'frozen', xSplit: 0, ySplit: 6 + (table.description ? 1 : 0) + (table.headers || []).length, }, ], pageSetup: { orientation: 'portrait' }, }); // FILL [...Array(3 + (table.description ? 1 : 0) + (table.headers || []).length).keys()].forEach((row) => { [2, 3, 4, 5].forEach((column) => { const cell = sheet.getCell(row + 2, column); cell.style = { alignment: { horizontal: 'right', wrapText: false, vertical: 'middle', readingOrder: 'rtl' }, font: { color: { argb: this.config.foregroundColor }, name: this.config.fontFA, size: 10 }, fill, }; }); }); let rowIndex = 3; // TITLE const title = sheet.getCell(rowIndex, 4); title.merge(sheet.getCell(rowIndex, 3)); title.value = table.title; if (title.style.font) { title.style.font.size = 12; title.style.font.bold = true; } rowIndex++; // DESCRIPTION if (table.description) { const description = sheet.getCell(rowIndex, 4); description.merge(sheet.getCell(rowIndex, 3)); description.value = table.description; if (description.style.font) { description.style.font.size = 9; description.style.font.bold = true; } rowIndex++; } // HEADERS (table.headers || []).map((header) => { const title = sheet.getCell(rowIndex, 3); title.value = `${header.title}:`; const value = sheet.getCell(rowIndex, 4); value.value = header.value; value.style = { font: { color: { argb: this.config.foregroundColor }, name: header.english ? this.config.fontEN : this.config.fontFA, }, fill, }; rowIndex++; }); rowIndex++; // WIDTH table.columns.forEach((column, c) => { const rows = table.rows.map((r) => export_type_1.ExportColumnEnum[column.type].width(r[column.id], column)); const width = Math.max(column.title.length, ...rows); sheet.getColumn(c + 1).width = width < 5 ? 5 : width > 50 ? 50 : width; }); // COLUMNS rowIndex++; table.columns.forEach((column, c) => { const cell = sheet.getCell(rowIndex, c + 1); cell.value = column.title; cell.style = { alignment: { horizontal: 'right', wrapText: false, vertical: 'middle', readingOrder: 'rtl', }, font: { color: { argb: this.config.foregroundColor }, name: this.config.fontFA, size: 10, bold: true, }, fill, }; }); sheet.getRow(rowIndex).height = 25; // ROWS table.rows.forEach((row) => { rowIndex++; table.columns.forEach((column, c) => { const english = export_type_1.ExportColumnEnum[column.type].xlsx.english(column); const cell = sheet.getCell(rowIndex, c + 1); cell.value = export_type_1.ExportColumnEnum[column.type].xlsx.value(row[column.id], column); cell.style = { alignment: { horizontal: english ? 'left' : 'right', wrapText: true, vertical: 'top', readingOrder: 'rtl', }, font: { color: { argb: this.config.textColor }, name: english ? this.config.fontEN : this.config.fontFA, size: 10, }, numFmt: export_type_1.ExportColumnEnum[column.type].xlsx.format, }; }); }); await workbook.xlsx.writeFile(path); resolve(); } catch (e) { reject(); } }); } }; exports.ExportExcelService = ExportExcelService; exports.ExportExcelService = ExportExcelService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)('EXPORT_CONFIG')), __metadata("design:paramtypes", [Object]) ], ExportExcelService); //# sourceMappingURL=excel.service.js.map