2019-03-26 17:32:37 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2019-04-16 13:04:10 +02:00
|
|
|
import { Workbook } from 'exceljs/dist/exceljs.min.js';
|
2019-03-26 17:32:37 +01:00
|
|
|
|
|
|
|
import { InfoToExport } from './motion-pdf.service';
|
2019-04-17 12:10:56 +02:00
|
|
|
import { motionImportExportHeaderOrder } from '../motion-import-export-order';
|
2019-04-03 11:31:08 +02:00
|
|
|
import { MotionRepositoryService } from 'app/core/repositories/motions/motion-repository.service';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2019-03-26 17:32:37 +01:00
|
|
|
import { ViewMotion } from '../models/view-motion';
|
2019-04-16 13:04:10 +02:00
|
|
|
import { XlsxExportServiceService, CellFillingDefinition } from 'app/core/ui-services/xlsx-export-service.service';
|
2019-03-26 17:32:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Service to export motion elements to XLSX
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class MotionXlsxExportService {
|
2019-04-16 13:04:10 +02:00
|
|
|
/**
|
|
|
|
* Defines the head row style
|
|
|
|
*/
|
|
|
|
private headRowFilling: CellFillingDefinition = {
|
|
|
|
type: 'pattern',
|
|
|
|
pattern: 'solid',
|
|
|
|
fgColor: {
|
|
|
|
argb: 'FFFFE699'
|
|
|
|
},
|
|
|
|
bgColor: {
|
|
|
|
argb: 'FFFFE699'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-26 17:32:37 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
2019-04-03 11:31:08 +02:00
|
|
|
*
|
|
|
|
* @param xlsx XlsxExportServiceService
|
|
|
|
* @param translate translationService
|
|
|
|
* @param motionRepo MotionRepositoryService
|
2019-03-26 17:32:37 +01:00
|
|
|
*/
|
2019-04-03 11:31:08 +02:00
|
|
|
public constructor(
|
|
|
|
private xlsx: XlsxExportServiceService,
|
|
|
|
private translate: TranslateService,
|
|
|
|
private motionRepo: MotionRepositoryService
|
|
|
|
) {}
|
2019-03-26 17:32:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export motions as XLSX
|
|
|
|
*
|
|
|
|
* @param motions
|
|
|
|
* @param contentToExport
|
|
|
|
* @param infoToExport
|
|
|
|
*/
|
|
|
|
public exportMotionList(motions: ViewMotion[], infoToExport: InfoToExport[]): void {
|
|
|
|
const workbook = new Workbook();
|
2019-04-17 12:10:56 +02:00
|
|
|
const propertyList = ['identifier', 'title'].concat(infoToExport);
|
|
|
|
// reorders the exported properties according to motionImportExportHeaderOrder
|
|
|
|
const properties = motionImportExportHeaderOrder.filter(property => propertyList.includes(property));
|
2019-04-16 13:04:10 +02:00
|
|
|
const worksheet = workbook.addWorksheet(this.translate.instant('Motions'), {
|
|
|
|
pageSetup: {
|
|
|
|
paperSize: 9,
|
|
|
|
orientation: 'portrait',
|
|
|
|
fitToPage: true,
|
|
|
|
fitToHeight: 5,
|
|
|
|
fitToWidth: properties.length,
|
|
|
|
printTitlesRow: '1:1'
|
|
|
|
}
|
|
|
|
});
|
2019-03-26 17:32:37 +01:00
|
|
|
|
|
|
|
worksheet.columns = properties.map(property => {
|
2019-04-17 12:10:56 +02:00
|
|
|
const propertyHeader =
|
|
|
|
property === 'motion_block'
|
|
|
|
? 'Motion block'
|
|
|
|
: property.charAt(0).toLocaleUpperCase() + property.slice(1);
|
2019-03-26 17:32:37 +01:00
|
|
|
return {
|
2019-04-17 12:10:56 +02:00
|
|
|
header: this.translate.instant(propertyHeader)
|
2019-03-26 17:32:37 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2019-04-16 13:04:10 +02:00
|
|
|
worksheet.getRow(1).eachCell(cell => {
|
|
|
|
cell.font = {
|
|
|
|
underline: true,
|
|
|
|
bold: true
|
|
|
|
};
|
|
|
|
cell.fill = this.headRowFilling;
|
|
|
|
});
|
2019-03-26 17:32:37 +01:00
|
|
|
|
|
|
|
// map motion data to properties
|
|
|
|
const motionData = motions.map(motion =>
|
|
|
|
properties.map(property => {
|
|
|
|
const motionProp = motion[property];
|
|
|
|
if (motionProp) {
|
2019-04-03 11:31:08 +02:00
|
|
|
switch (property) {
|
|
|
|
case 'state':
|
|
|
|
return this.motionRepo.getExtendedStateLabel(motion);
|
|
|
|
case 'recommendation':
|
|
|
|
return this.motionRepo.getExtendedRecommendationLabel(motion);
|
|
|
|
default:
|
|
|
|
return this.translate.instant(motionProp.toString());
|
|
|
|
}
|
2019-03-26 17:32:37 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
// add to sheet
|
|
|
|
for (const motion of motionData) {
|
|
|
|
worksheet.addRow(motion);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.xlsx.autoSize(worksheet, 0);
|
|
|
|
this.xlsx.saveXlsx(workbook, this.translate.instant('Motions'));
|
|
|
|
}
|
|
|
|
}
|