Skip to content
Snippets Groups Projects
Commit a2d9350e authored by Lukas Mauß's avatar Lukas Mauß
Browse files

Fix export

parent a5f7ec65
No related merge requests found
......@@ -9,6 +9,6 @@
<div fxLayout="row" fxLayoutAlign="center center" fxLayoutGap="20px">
<button mat-raised-button color="warn" (click)="onNoClick()">{{'comment-page.abort' | translate}}</button>
<button mat-raised-button color="primary"
(click)="onExport(exportType)">{{'comment-page.export' | translate}}</button>
(click)="dialogRef.close(exportType)">{{'comment-page.export' | translate}}</button>
</div>
</div>
......@@ -9,7 +9,7 @@ import { Comment } from '../../../../models/comment';
styleUrls: ['./comment-export.component.scss']
})
export class CommentExportComponent implements OnInit {
comments: Comment[];
exportType = 'comma';
constructor(public dialogRef: MatDialogRef<CommentCreatorPageComponent>) { }
......@@ -20,37 +20,5 @@ export class CommentExportComponent implements OnInit {
onNoClick(): void {
this.dialogRef.close();
}
exportCsv(delimiter: string, date: string): void {
let csv: string;
let keyFields = '';
let valueFields = '';
keyFields = Object.keys(this.comments[0]).slice(3).join(delimiter) + '\r\n';
this.comments.forEach(element => {
element.body = '"' + element.body.replace(/[\r\n]/g, ' ').replace(/ +/g, ' ').replace(/"/g, '""') + '"';
valueFields += Object.values(element).slice(3).join(delimiter) + '\r\n';
});
csv = keyFields + valueFields;
const myBlob = new Blob([csv], { type: 'text/csv' });
const link = document.createElement('a');
const fileName = 'comments_' + date + '.csv';
link.setAttribute('download', fileName);
link.href = window.URL.createObjectURL(myBlob);
link.click();
}
onExport(exportType: string): void {
const date = new Date();
const dateString = date.getFullYear() + '_' + ('0' + (date.getMonth() + 1)).slice(-2) + '_' + ('0' + date.getDate()).slice(-2);
const timeString = ('0' + date.getHours()).slice(-2) + ('0' + date.getMinutes()).slice(-2) + ('0' + date.getSeconds()).slice(-2);
const timestamp = dateString + '_' + timeString;
if (exportType === 'comma') {
this.exportCsv(',', timestamp);
this.onNoClick();
}
if (exportType === 'semicolon') {
this.exportCsv(';', timestamp);
this.onNoClick();
}
}
}
......@@ -170,11 +170,46 @@ export class CommentListComponent implements OnInit {
this.wsCommentService.add(comment);
}
exportCsv(delimiter: string, date: string): void {
let exportComments = JSON.parse(JSON.stringify(this.comments));
let csv: string;
let keyFields = '';
let valueFields = '';
keyFields = Object.keys(exportComments[0]).slice(3).join(delimiter) + '\r\n';
exportComments.forEach(element => {
element.body = '"' + element.body.replace(/[\r\n]/g, ' ').replace(/ +/g, ' ').replace(/"/g, '""') + '"';
valueFields += Object.values(element).slice(3).join(delimiter) + '\r\n';
});
csv = keyFields + valueFields;
const myBlob = new Blob([csv], { type: 'text/csv' });
const link = document.createElement('a');
const fileName = 'comments_' + date + '.csv';
link.setAttribute('download', fileName);
link.href = window.URL.createObjectURL(myBlob);
link.click();
}
onExport(exportType: string): void {
const date = new Date();
const dateString = date.getFullYear() + '_' + ('0' + (date.getMonth() + 1)).slice(-2) + '_' + ('0' + date.getDate()).slice(-2);
const timeString = ('0' + date.getHours()).slice(-2) + ('0' + date.getMinutes()).slice(-2) + ('0' + date.getSeconds()).slice(-2);
const timestamp = dateString + '_' + timeString;
if (exportType === 'comma') {
this.exportCsv(',', timestamp);
}
if (exportType === 'semicolon') {
this.exportCsv(';', timestamp);
}
}
openExportDialog(): void {
const dialogRef = this.dialog.open(CommentExportComponent, {
width: '400px'
});
dialogRef.componentInstance.comments = this.comments;
dialogRef.afterClosed().subscribe(result => {
this.onExport(result);
});
}
filterFavorite(): void {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment