Skip to content

Commit

Permalink
Added Export Excel function for Stats table.
Browse files Browse the repository at this point in the history
  • Loading branch information
augustinecyr committed Apr 20, 2023
1 parent 10bd25d commit c1e5532
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 16 deletions.
171 changes: 156 additions & 15 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"googleapis": "^113.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"xlsx": "^0.18.5",
"zone.js": "~0.12.0"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/app/components/stats.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ <h2>Stats</h2>
</tr>
</tbody>
</table>
<br>
<button mat-raised-button color="basic" (click)="toExcel()">
Export Excel
</button>
</div>
</div>
</div>
45 changes: 44 additions & 1 deletion frontend/src/app/components/stats.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { Observable, map } from 'rxjs';
import { GoogleData, PlayerSQL, Stats, UserData } from '../models';
import { StatsService } from '../stats.service';
import { UserService } from '../user.service';
import { Router } from '@angular/router';
import { writeFile } from 'xlsx';

@Component({
selector: 'app-stats',
Expand Down Expand Up @@ -60,4 +61,46 @@ export class StatsComponent implements OnInit {
}
);
}

toExcel() {
if (!this.stats) {
console.log('No stats available');
return;
}
// create the excel format
const columns = [
{ header: 'Goals', key: 'goals' },
{ header: 'Assists', key: 'assists' },
{ header: 'Yellow Cards', key: 'yellowCards' },
{ header: 'Red Cards', key: 'redCards' },
{ header: 'Clean Sheets', key: 'cleanSheets' },
{ header: 'Conceded Goals', key: 'concededGoals' },
];

this.stats
.pipe(
map((stats) =>
stats.map((s) => ({
goals: s.goals,
assists: s.assists,
yellowCards: s.yellowCards,
redCards: s.redCards,
cleanSheets: s.cleanSheets,
concededGoals: s.concededGoals,
}))
)
)
.subscribe((statsArr) => {
console.log('Stats:', statsArr);
const excelsheet = { columns, rows: statsArr };
const excelbook = {
Sheets: { data: excelsheet },
SheetNames: ['STATS'],
};

const fileName = 'stats.xlsx';
writeFile(excelbook, fileName);
console.log('Successfully exported excel file');
});
}
}

0 comments on commit c1e5532

Please sign in to comment.