pab-results-table.component.ts 4.45 KiB
import { Component, ViewChild, ElementRef } from "@angular/core";
import { PabResults } from "../../results/pab-results";
import * as XLSX from "xlsx";
import { ApplicationSetupService } from "../../services/app-setup/app-setup.service";
import { I18nService } from "../../services/internationalisation/internationalisation.service";
import { ResultsComponent } from "../fixedvar-results/results.component";
@Component({
    selector: "pab-results-table",
    templateUrl: "./pab-results-table.component.html",
    styleUrls: [
        "./pab-results-table.component.scss"
export class PabResultsTableComponent extends ResultsComponent {
    /** résultats non mis en forme */
    private _pabResults: PabResults;
    /** entêtes des colonnes */
    private _headers: string[];
    /** résultats mis en forme */
    private _dataSet: any[];
    @ViewChild("tableContainer") table: ElementRef;
    constructor(
        protected appSetupService: ApplicationSetupService,
        protected intlService: I18nService
    ) {
        super();
    public set results(r: PabResults) {
        this._pabResults = r;
        this._dataSet = [];
        if (
            this._pabResults
            && this._pabResults.cloisonsResults
            && this._pabResults.cloisonsResults.length > 0
            && ! this._pabResults.hasError()
        ) {
            const pr = this._pabResults;
            const nDigits = this.appSetupService.displayDigits;
            // when a parameter is variating, index of the variating parameter
            // values to build the data from
            const vi = pr.variableIndex;
            // refresh headers here if language changed
            this._headers = pr.headers;
            // line 1
            if (pr.cloisonsResults[0].resultElements[vi].vCalc) { // parfois le calcul des cloisons échoue
                this._dataSet.push([
                    this.intlService.localizeText("INFO_LIB_AMONT"),
                    pr.cloisonsResults[0].resultElements[vi] ? pr.cloisonsResults[0].resultElements[vi].vCalc.toFixed(nDigits) : "",
                    "", "", "", "", "", "", ""
                ]);
            // lines 2 - n-1
            for (let i = 0; i < pr.cloisonsResults.length; i++) {
                if (
                    pr.cloisonsResults[i].resultElements[vi].vCalc
                ) {
                    const r2n = pr.cloisonsResults[i].resultElements[vi].extraResults;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
let Z1: number; if (i < pr.cloisonsResults.length - 1) { Z1 = pr.cloisonsResults[i + 1].resultElements[vi].vCalc; } else { Z1 = pr.cloisonAvalResults.resultElements[vi].vCalc; } this._dataSet.push([ i + 1, // n° cloison Z1.toFixed(nDigits), // Z r2n.ZRAM.toFixed(nDigits), r2n.DH.toFixed(nDigits), r2n.Q.toFixed(nDigits), r2n.PV.toFixed(nDigits), r2n.YMOY.toFixed(nDigits), r2n.ZRMB.toFixed(nDigits), r2n.QA // QA ]); } } // downstream line if (pr.cloisonAvalResults.resultElements[vi].vCalc) { const rln = pr.cloisonAvalResults.resultElements[vi].extraResults; this._dataSet.push([ this.intlService.localizeText("INFO_LIB_AVAL"), pr.Z2.toFixed(nDigits), rln.ZRAM.toFixed(nDigits), rln.DH.toFixed(nDigits), rln.Q.toFixed(nDigits), "", "", "", "" ]); } } } public get hasResults(): boolean { return this._pabResults && this._pabResults.hasResults; } public get headers() { return this._headers; } /** * Returns a combination of and results and extraResults for mat-table */ public get dataSet() { return this._dataSet; } public exportAsSpreadsheet() { // automagic <table> extraction const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(this.table.nativeElement); const wb: XLSX.WorkBook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "default"); // save and download XLSX.writeFile(wb, "PABResults.xlsx"); } }