Merge pull request #4276 from MaximilianKrambach/csvExportImport

fixing csv export/import, add option for iso 8895-15
This commit is contained in:
Sean 2019-02-08 14:37:23 +01:00 committed by GitHub
commit 524d6d82a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 4 deletions

View File

@ -40,6 +40,7 @@ _('Allow access for anonymous guest users');
_('Show this text on the login page'); _('Show this text on the login page');
_('Export'); _('Export');
_('Separator used for all csv exports and examples'); _('Separator used for all csv exports and examples');
_('Default encoding for all csv exports');
_('Page number alignment in PDF'); _('Page number alignment in PDF');
_('Left'); _('Left');
_('Center'); _('Center');

View File

@ -288,7 +288,7 @@ export abstract class BaseImportService<V extends BaseViewModel> {
public onSelectFile(event: any): void { public onSelectFile(event: any): void {
// TODO type // TODO type
if (event.target.files && event.target.files.length === 1) { if (event.target.files && event.target.files.length === 1) {
if (event.target.files[0].type === 'text/csv') { if (event.target.files[0].type.startsWith('text/csv')) {
this._rawFile = event.target.files[0]; this._rawFile = event.target.files[0];
this.readFile(event.target.files[0]); this.readFile(event.target.files[0]);
} else { } else {

View File

@ -83,10 +83,12 @@ export class CsvExportService {
filename: string, filename: string,
{ {
lineSeparator = '\r\n', lineSeparator = '\r\n',
columnSeparator = this.config.instant('general_csv_separator') columnSeparator = this.config.instant('general_csv_separator'),
encoding = this.config.instant('general_csv_encoding')
}: { }: {
lineSeparator?: string; lineSeparator?: string;
columnSeparator?: string; columnSeparator?: string;
encoding?: 'utf-8' | 'iso-8859-15';
} = {} } = {}
): void { ): void {
let csvContent = []; // Holds all lines as arrays with each column-value let csvContent = []; // Holds all lines as arrays with each column-value
@ -153,8 +155,12 @@ export class CsvExportService {
return line.map(entry => tsList[0] + entry + tsList[0]).join(columnSeparator); return line.map(entry => tsList[0] + entry + tsList[0]).join(columnSeparator);
}) })
.join(lineSeparator); .join(lineSeparator);
const filetype = `text/csv;charset=${encoding}`;
this.exporter.saveFile(csvContentAsString, filename, 'text/csv'); if (encoding === 'iso-8859-15') {
this.exporter.saveFile(this.exporter.convertTo8859_15(csvContentAsString), filename, filetype);
} else {
this.exporter.saveFile(csvContentAsString, filename, filetype);
}
} }
/** /**

View File

@ -35,4 +35,18 @@ export class FileExportService {
const pattern = new RegExp(/^[^\\\/\?%\*:\|\"\<\>]*[^\.]+$/i); const pattern = new RegExp(/^[^\\\/\?%\*:\|\"\<\>]*[^\.]+$/i);
return pattern.test(filename); return pattern.test(filename);
} }
/**
* get an iso-8859-15 - compatible blob part
*
* @param data
* @returns a Blob part
*/
public convertTo8859_15(data: string): BlobPart {
const array = new Uint8Array(new ArrayBuffer(data.length));
for (let i = 0; i < data.length; i++) {
array[i] = data.charCodeAt(i);
}
return array;
}
} }

View File

@ -138,6 +138,20 @@ def get_config_variables():
subgroup="Export", subgroup="Export",
) )
yield ConfigVariable(
name="general_csv_encoding",
default_value="utf-8",
input_type="choice",
label="Default encoding for all csv exports",
choices=(
{"value": "utf-8", "display_name": "UTF-8"},
{"value": "iso-8859-15", "display_name": "ISO-8859-15"},
),
weight=143,
group="General",
subgroup="Export",
)
yield ConfigVariable( yield ConfigVariable(
name="general_export_pdf_pagenumber_alignment", name="general_export_pdf_pagenumber_alignment",
default_value="center", default_value="center",