@koalarx/ui
Version:
Koala UI is a Design System developed in Angular whose objective is to facilitate and make your development faster and simpler, making this framework your greatest ally.
173 lines (167 loc) • 7.83 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable } from '@angular/core';
import * as i1 from 'ngx-papaparse';
import * as Excel from 'exceljs/dist/exceljs.min.js';
import * as fs from 'file-saver';
import { koala } from '@koalarx/utils';
class KoalaCsvService {
constructor(papa) {
this.papa = papa;
}
convertJsonToCsv(json, filename = 'export') {
this.downloadCsv(new Blob([
this.papa.unparse(json, {
header: true,
delimiter: ";",
newline: "\r\n"
})
], {
type: 'text/csv;charset=utf-8;'
}), filename);
}
downloadCsv(blob, filename = 'export') {
let link = document.createElement('a');
let url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename + '.csv');
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
KoalaCsvService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: KoalaCsvService, deps: [{ token: i1.Papa }], target: i0.ɵɵFactoryTarget.Injectable });
KoalaCsvService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: KoalaCsvService, providedIn: "any" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: KoalaCsvService, decorators: [{
type: Injectable,
args: [{ providedIn: "any" }]
}], ctorParameters: function () { return [{ type: i1.Papa }]; } });
class KoalaXlsxService {
async convertJsonToXlsx(json, config) {
const title = config.title;
const header = [];
Object.keys(json[0]).forEach(name => {
header.push((config.normalizeHeader === true || config.normalizeHeader === null || config.normalizeHeader === undefined ?
koala(name).string().normalize().getValue().toUpperCase() :
name));
});
const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet(config.sheetName);
if (config.password) {
await worksheet.protect(config.password, {
selectLockedCells: false,
selectUnlockedCells: false,
scenarios: false
});
}
if (config.title) {
const titleRow = worksheet.addRow([title]);
titleRow.alignment = { horizontal: 'center' };
if (config.titleFontColor) {
titleRow.font = { bold: true, color: { argb: config.titleFontColor.replace('#', '') } };
}
if (config.titleBackgroundColor) {
titleRow.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: config.titleBackgroundColor.replace('#', '') },
bgColor: { argb: config.titleBackgroundColor.replace('#', '') }
};
}
worksheet.mergeCells(`A1:${worksheet.getCell(1, Object.keys(json[0]).length).address}`);
}
const headerRow = worksheet.addRow(header);
headerRow.eachCell((cell, number) => {
if (config.headerBackgroundColor) {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: config.headerBackgroundColor.replace('#', '') },
bgColor: { argb: config.headerBackgroundColor.replace('#', '') }
};
}
if (config.headerFontColor) {
cell.font = {
color: { argb: config.headerFontColor.replace('#', '') }
};
}
});
json.forEach(item => {
const data = [];
Object.values(item).forEach(value => data.push(value));
worksheet.addRow(data);
});
workbook.xlsx.writeBuffer().then((data) => {
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
fs.saveAs(blob, config.filename + '.xlsx');
});
}
async convertJsonToXlsxWithTabs(tabs, config) {
const header = [];
const workbook = new Excel.Workbook();
tabs.forEach(tab => {
const title = tab?.titleName ?? config.title;
Object.keys(tab.json[0]).forEach(name => {
header.push((config.normalizeHeader === true || config.normalizeHeader === null || config.normalizeHeader === undefined ?
koala(name).string().normalize().getValue().toUpperCase() :
name));
});
const worksheet = workbook.addWorksheet(tab?.sheetName ?? config.sheetName);
if (config.title) {
const titleRow = worksheet.addRow([title]);
titleRow.alignment = { horizontal: 'center' };
if (config.titleFontColor) {
titleRow.font = { bold: true, color: { argb: config.titleFontColor.replace('#', '') } };
}
if (config.titleBackgroundColor) {
titleRow.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: config.titleBackgroundColor.replace('#', '') },
bgColor: { argb: config.titleBackgroundColor.replace('#', '') }
};
}
worksheet.mergeCells(`A1:${worksheet.getCell(1, Object.keys(tab.json[0]).length).address}`);
}
const headerRow = worksheet.addRow(header);
headerRow.eachCell((cell, number) => {
if (config.headerBackgroundColor) {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: config.headerBackgroundColor.replace('#', '') },
bgColor: { argb: config.headerBackgroundColor.replace('#', '') }
};
}
if (config.headerFontColor) {
cell.font = {
color: { argb: config.headerFontColor.replace('#', '') }
};
}
});
tab.json.forEach(item => {
const data = [];
Object.values(item).forEach(value => data.push(value));
worksheet.addRow(data);
});
});
workbook.xlsx.writeBuffer().then((data) => {
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
fs.saveAs(blob, config.filename + '.xlsx');
});
}
}
KoalaXlsxService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: KoalaXlsxService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
KoalaXlsxService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: KoalaXlsxService, providedIn: "any" });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.3.0", ngImport: i0, type: KoalaXlsxService, decorators: [{
type: Injectable,
args: [{ providedIn: "any" }]
}] });
const maskOptions = {
validation: false
};
/**
* Generated bundle index. Do not edit.
*/
export { KoalaCsvService, KoalaXlsxService, maskOptions };
//# sourceMappingURL=koalarx-ui-common.mjs.map