added translations for headers in excel report files
#60
Merged
naeem.ullah
merged 3 commits from aconnect-UX/reporting-translation-updates into dev-pending-20-01-2026 6 days ago
@ -1,28 +1,66 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { saveAs } from 'file-saver';
|
import { saveAs } from 'file-saver';
|
||||||
import * as XLSX from 'xlsx';
|
import * as XLSX from 'xlsx';
|
||||||
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
import { EXCEL_FILE_EXTENSION, EXCEL_FILE_TYPE } from '../../utils/app.constants';
|
import { EXCEL_FILE_EXTENSION, EXCEL_FILE_TYPE } from '../../utils/app.constants';
|
||||||
|
import { StorageService } from './storage.service';
|
||||||
|
import { supportedLanguages } from '../../utils/enums';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class ExcelExportService {
|
export class ExcelExportService {
|
||||||
|
|
||||||
constructor() { }
|
|
||||||
|
|
||||||
private fileType = EXCEL_FILE_TYPE;
|
private fileType = EXCEL_FILE_TYPE;
|
||||||
private fileExtension = EXCEL_FILE_EXTENSION;
|
private fileExtension = EXCEL_FILE_EXTENSION;
|
||||||
|
|
||||||
|
constructor(private translate: TranslateService, private storageService: StorageService) { }
|
||||||
|
|
||||||
public exportExcel(jsonData: any[], fileName: string): void {
|
public exportExcel(jsonData: any[], fileName: string): void {
|
||||||
|
|
||||||
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(jsonData);
|
const translatedData = this.translateHeaders(jsonData);
|
||||||
const wb: XLSX.WorkBook = { Sheets: { 'data': ws }, SheetNames: ['data'] };
|
|
||||||
|
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(translatedData);
|
||||||
|
const wb: XLSX.WorkBook = { Sheets: { data: ws }, SheetNames: ['data'] };
|
||||||
const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
|
const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
|
||||||
|
|
||||||
this.saveExcelFile(excelBuffer, fileName);
|
this.saveExcelFile(excelBuffer, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private translateHeaders(data: any[]): any[] {
|
||||||
|
if (!data?.length) return data;
|
||||||
|
|
||||||
|
return data.map(row => {
|
||||||
|
const newRow: any = {};
|
||||||
|
|
||||||
|
Object.keys(row).forEach(key => {
|
||||||
|
const candidates = [
|
||||||
|
key,
|
||||||
|
key.toLowerCase(),
|
||||||
|
key.toUpperCase(),
|
||||||
|
key.replace(/([A-Z])/g, '_$1').toUpperCase()
|
||||||
|
];
|
||||||
|
|
||||||
|
let translatedKey = key;
|
||||||
|
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
const value = this.translate.instant(candidate);
|
||||||
|
if (value !== candidate) {
|
||||||
|
translatedKey = value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newRow[translatedKey] = row[key];
|
||||||
|
});
|
||||||
|
|
||||||
|
return newRow;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private saveExcelFile(buffer: any, fileName: string): void {
|
private saveExcelFile(buffer: any, fileName: string): void {
|
||||||
const data: Blob = new Blob([buffer], { type: this.fileType });
|
const data: Blob = new Blob([buffer], { type: this.fileType });
|
||||||
saveAs.saveAs(data, fileName + this.fileExtension);
|
saveAs(data, fileName + this.fileExtension);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue