Merge pull request #4432 from MaximilianKrambach/csvExport

csv example export refactor
This commit is contained in:
Maximilian Krambach 2019-03-06 11:29:55 +01:00 committed by GitHub
commit b978029fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 55 deletions

View File

@ -75,7 +75,7 @@ export class CsvExportService {
* @param filename name of the resulting file * @param filename name of the resulting file
* @param options optional: * @param options optional:
* lineSeparator (defaults to \r\n windows style line separator), * lineSeparator (defaults to \r\n windows style line separator),
* columnseparator defaults to semicolon (other usual separators are ',' '\T' (tab), ' 'whitespace) * columnseparator defaults to configured option (',' , other values are ';', '\t' (tab), ' 'whitespace)
*/ */
public export<T extends BaseViewModel>( public export<T extends BaseViewModel>(
models: T[], models: T[],
@ -190,4 +190,33 @@ export class CsvExportService {
const temp = input.charAt(0).toUpperCase() + input.slice(1); const temp = input.charAt(0).toUpperCase() + input.slice(1);
return this.translate.instant(temp); return this.translate.instant(temp);
} }
public dummyCSVExport(header: string[], rows: (string | number | boolean | null)[][], filename: string): void {
const separator = this.config.instant<string>('general_csv_separator');
const tsList = this.checkCsvTextSafety(separator, ['"', "'", '`', '/', '\\', ';', '.']);
const headerRow = [header.map(item => this.translate.instant(item)).join(separator)];
const content = rows.map(row =>
row
.map(item => {
if (item === null) {
return '';
}
if (typeof item === 'string') {
return `${tsList[0]}${item}${tsList[0]}`;
} else if (typeof item === 'boolean') {
return item ? '1' : '0';
} else {
return `${item}`;
}
})
.join(separator)
);
const csvContentAsString = headerRow.concat(content).join('\r\n');
const encoding = this.config.instant<'utf-8' | 'iso-8859-15'>('general_csv_encoding');
if (encoding === 'iso-8859-15') {
this.exporter.saveFile(this.exporter.convertTo8859_15(csvContentAsString), filename, 'text/csv');
} else {
this.exporter.saveFile(csvContentAsString, filename, 'text/csv');
}
}
} }

View File

@ -6,8 +6,8 @@ import { TranslateService } from '@ngx-translate/core';
import { AgendaImportService } from '../../agenda-import.service'; import { AgendaImportService } from '../../agenda-import.service';
import { BaseImportListComponent } from 'app/site/base/base-import-list'; import { BaseImportListComponent } from 'app/site/base/base-import-list';
import { CsvExportService } from 'app/core/ui-services/csv-export.service';
import { DurationService } from 'app/core/ui-services/duration.service'; import { DurationService } from 'app/core/ui-services/duration.service';
import { FileExportService } from 'app/core/ui-services/file-export.service';
import { itemVisibilityChoices } from 'app/shared/models/agenda/item'; import { itemVisibilityChoices } from 'app/shared/models/agenda/item';
import { ViewCreateTopic } from '../../models/view-create-topic'; import { ViewCreateTopic } from '../../models/view-create-topic';
@ -41,7 +41,7 @@ export class AgendaImportListComponent extends BaseImportListComponent<ViewCreat
translate: TranslateService, translate: TranslateService,
importer: AgendaImportService, importer: AgendaImportService,
formBuilder: FormBuilder, formBuilder: FormBuilder,
private exporter: FileExportService, private exporter: CsvExportService,
private durationService: DurationService private durationService: DurationService
) { ) {
super(importer, titleService, translate, matSnackBar); super(importer, titleService, translate, matSnackBar);
@ -52,16 +52,17 @@ export class AgendaImportListComponent extends BaseImportListComponent<ViewCreat
* Triggers an example csv download * Triggers an example csv download
*/ */
public downloadCsvExample(): void { public downloadCsvExample(): void {
const headerRow = ['Title', 'Text', 'Duration', 'Comment', 'Internal item'] const headerRow = ['Title', 'Text', 'Duration', 'Comment', 'Internal item'];
.map(item => this.translate.instant(item))
.join(',');
const rows = [ const rows = [
headerRow, ['Demo 1', 'Demo text 1', '1:00', 'test comment', null],
'Demo 1,Demo text 1,1:00,test comment,', ['Break', null, '0:10', null, 'internal', null],
'Break,,0:10,,internal', ['Demo 2', 'Demo text 2', '1:30', null, 'hidden']
'Demo 2,Demo text 2,1:30,,hidden'
]; ];
this.exporter.saveFile(rows.join('\n'), this.translate.instant('Topic example') + '.csv', 'text/csv'); this.exporter.dummyCSVExport(
headerRow,
rows,
`${this.translate.instant('Agenda')}-${this.translate.instant('example')}.csv`
);
} }
/** /**

View File

@ -3,7 +3,6 @@ import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { CsvExportService, CsvColumnDefinitionProperty } from 'app/core/ui-services/csv-export.service'; import { CsvExportService, CsvColumnDefinitionProperty } from 'app/core/ui-services/csv-export.service';
import { FileExportService } from 'app/core/ui-services/file-export.service';
import { InfoToExport } from './motion-pdf.service'; import { InfoToExport } from './motion-pdf.service';
import { ViewMotion } from '../models/view-motion'; import { ViewMotion } from '../models/view-motion';
@ -20,11 +19,7 @@ export class MotionCsvExportService {
* @param csvExport CsvExportService * @param csvExport CsvExportService
* @param translate TranslateService * @param translate TranslateService
*/ */
public constructor( public constructor(private csvExport: CsvExportService, private translate: TranslateService) {}
private csvExport: CsvExportService,
private translate: TranslateService,
private fileExport: FileExportService
) {}
/** /**
* Export all motions as CSV * Export all motions as CSV
@ -67,15 +62,12 @@ export class MotionCsvExportService {
} }
public exportDummyMotion(): void { public exportDummyMotion(): void {
const headerRow = ['Identifier', 'Title', 'Text', 'Reason', 'Submitters', 'Category', 'Origin', 'Motion block'] const headerRow = ['Identifier', 'Title', 'Text', 'Reason', 'Submitters', 'Category', 'Origin', 'Motion block'];
.map(item => this.translate.instant(item))
.join(',');
const rows = [ const rows = [
headerRow, ['A1', 'Title 1', 'Text 1', 'Reason 1', 'Submitter A', 'Category A', 'Last Year Conference A', 'Block A'],
'A1,Title 1,Text 1,Reason 1,Submitter A,Category A,"Last Year Conference A", Block A', ['B1', 'Title 2', 'Text 2', 'Reason 2', 'Submitter B', 'Category B', null, 'Block A'],
'B1,Title 2,Text 2,Reason 2,Submitter B, Category B,, Block A', [null, 'Title 3', 'Text 3', null, null, null, null, null]
',Title 3, Text 3,,,,,'
]; ];
this.fileExport.saveFile(rows.join('\n'), this.translate.instant('motions-example') + '.csv', 'text/csv'); this.csvExport.dummyCSVExport(headerRow, rows, `${this.translate.instant('motions-example')}.csv`);
} }
} }

View File

@ -4,7 +4,6 @@ import { TranslateService } from '@ngx-translate/core';
import { CsvExportService, CsvColumnDefinitionProperty } from 'app/core/ui-services/csv-export.service'; import { CsvExportService, CsvColumnDefinitionProperty } from 'app/core/ui-services/csv-export.service';
import { ViewStatuteParagraph } from '../models/view-statute-paragraph'; import { ViewStatuteParagraph } from '../models/view-statute-paragraph';
import { FileExportService } from 'app/core/ui-services/file-export.service';
/** /**
* Exports CSVs for statute paragraphs. * Exports CSVs for statute paragraphs.
@ -18,13 +17,8 @@ export class StatuteCsvExportService {
* *
* @param csvExport CsvExportService * @param csvExport CsvExportService
* @param translate TranslateService * @param translate TranslateService
* @param fileExport FileExportService
*/ */
public constructor( public constructor(private csvExport: CsvExportService, private translate: TranslateService) {}
private csvExport: CsvExportService,
private translate: TranslateService,
private fileExport: FileExportService
) {}
/** /**
* Export all statute paragraphs as CSV * Export all statute paragraphs as CSV
@ -43,17 +37,13 @@ export class StatuteCsvExportService {
* Exports a short example file * Exports a short example file
*/ */
public exportDummyCSV(): void { public exportDummyCSV(): void {
const headerRow = ['Title', 'Text'].map(item => this.translate.instant(item)).join(','); const header = ['Title', 'Text'];
const rows = [ const rows = [
headerRow, ['§1', 'This is the first section'],
'§1,"This is the first section"', ['§1, A 3', 'This is another important aspect'],
'"§1, A 3", "This is another important aspect"', ['§2', 'Yet another']
'§2,Yet another'
]; ];
this.fileExport.saveFile( const filename = `${this.translate.instant('Statute')}-${this.translate.instant('example')}.csv`;
rows.join('\n'), this.csvExport.dummyCSVExport(header, rows, filename);
`${this.translate.instant('Statute')}-${this.translate.instant('example')}.csv`,
'text/csv'
);
} }
} }

View File

@ -4,7 +4,7 @@ import { Title } from '@angular/platform-browser';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { BaseImportListComponent } from 'app/site/base/base-import-list'; import { BaseImportListComponent } from 'app/site/base/base-import-list';
import { FileExportService } from 'app/core/ui-services/file-export.service'; import { CsvExportService } from 'app/core/ui-services/csv-export.service';
import { FormBuilder, FormGroup } from '@angular/forms'; import { FormBuilder, FormGroup } from '@angular/forms';
import { NewEntry } from 'app/core/ui-services/base-import.service'; import { NewEntry } from 'app/core/ui-services/base-import.service';
import { UserImportService } from '../../services/user-import.service'; import { UserImportService } from '../../services/user-import.service';
@ -27,7 +27,7 @@ export class UserImportListComponent extends BaseImportListComponent<ViewUser> {
* @param matSnackBar snackbar for displaying errors * @param matSnackBar snackbar for displaying errors
* @param formBuilder: FormBuilder for the textArea * @param formBuilder: FormBuilder for the textArea
* @param translate the translate service * @param translate the translate service
* @param exporter: csv export service for dummy dat * @param exporter: csv export service for dummy data
* @param importer: The motion csv import service * @param importer: The motion csv import service
*/ */
public constructor( public constructor(
@ -35,7 +35,7 @@ export class UserImportListComponent extends BaseImportListComponent<ViewUser> {
matSnackBar: MatSnackBar, matSnackBar: MatSnackBar,
formBuilder: FormBuilder, formBuilder: FormBuilder,
public translate: TranslateService, public translate: TranslateService,
private exporter: FileExportService, private exporter: CsvExportService,
importer: UserImportService importer: UserImportService
) { ) {
super(importer, titleService, translate, matSnackBar); super(importer, titleService, translate, matSnackBar);
@ -59,17 +59,40 @@ export class UserImportListComponent extends BaseImportListComponent<ViewUser> {
'Is a committee', 'Is a committee',
'Initial password', 'Initial password',
'Email' 'Email'
]
.map(item => this.translate.instant(item))
.join(',');
const rows = [
headerRow,
'Dr.,Max,Mustermann,"Berlin",1234567890,"Delegates, Staff",xyz,1,1,,initialPassword,',
',John,Doe,Washington,75/99/8-2,Committees,"This is a comment, without doubt",1,1,,,john.doe@email.com',
',Fred,Bloggs,London,,,,,,,,',
',,Executive Board,,,,,,,1,,'
]; ];
this.exporter.saveFile(rows.join('\n'), this.translate.instant('participants-example') + '.csv', 'text/csv'); const rows = [
[
'Dr.',
'Max',
'Mustermann',
'Berlin',
1234567890,
'Delegates, Staff',
'xyz',
1,
1,
,
'initialPassword',
null
],
[
null,
'John',
'Doe',
'Washington',
'75/99/8-2',
'Committees',
'This is a comment, without doubt',
1,
1,
null,
null,
'john.doe@email.com'
],
[null, 'Fred', 'Bloggs', 'London', null, null, null, null, null, null, null, null],
[null, null, 'Executive Board', null, null, null, null, null, null, 1, null, null]
];
this.exporter.dummyCSVExport(headerRow, rows, `${this.translate.instant('participants-example')}.csv`);
} }
/** /**