Skip to content

Commit

Permalink
fix (core/reports): fix the csv/xsls export of tables with colspan or…
Browse files Browse the repository at this point in the history
… duplicated header labels
  • Loading branch information
sara-gnucoop committed May 28, 2021
1 parent 7d62964 commit e0c01fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
20 changes: 7 additions & 13 deletions src/core/reports/widget-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,21 @@ describe('widget-export', () => {
it('widget chart to xlsx data', () => {
widgetExport.data = dataChart;
widgetExport.widgetType = AjfWidgetType.Chart;
const testSpy = spyOn(XLSX.utils, 'json_to_sheet').and.callThrough();
const testSpy = spyOn(XLSX.utils, 'aoa_to_sheet').and.callThrough();
widgetExport.exportCsv();
const toEqual = [{
name: 'My First Dataset',
January: 65000000,
February: 59,
March: 80,
April: 81,
May: 56,
June: 55,
July: 40
}];
const toEqual: unknown[][] = [
['name', 'January', 'February', 'March', 'April', 'May', 'June', 'July'],
['My First Dataset', 65000000, 59, 80, 81, 56, 55, 40]
];
expect(testSpy).toHaveBeenCalledWith(toEqual);
});

it('widget table to xlsx data', () => {
widgetExport.data = dataTableXlsx;
widgetExport.widgetType = AjfWidgetType.Table;
const testSpy = spyOn(XLSX.utils, 'json_to_sheet').and.callThrough();
const testSpy = spyOn(XLSX.utils, 'aoa_to_sheet').and.callThrough();
widgetExport.exportCsv();
const toEqual = [{a: 'd', b: 'e', c: 'f'}, {a: 'g', b: 'h', c: 'i'}];
const toEqual: unknown[][] = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];
expect(testSpy).toHaveBeenCalledWith(toEqual);
});
});
31 changes: 20 additions & 11 deletions src/core/reports/widget-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,51 @@ export class AjfWidgetExport {
export(bookType: 'csv'|'xlsx'): void {
const sheetName = this._buildTitle(this.widgetType);
const sheets: {[sheet: string]: XLSX.WorkSheet} = {};
sheets[sheetName] = XLSX.utils.json_to_sheet(this._buildXlsxData());
sheets[sheetName] = XLSX.utils.aoa_to_sheet(this._buildXlsxData());
const workBook: XLSX.WorkBook = {Sheets: sheets, SheetNames: [sheetName]};
XLSX.writeFile(workBook, `${sheetName}.${bookType}`, {
bookType,
type: 'array',
});
}

private _buildXlsxData(): {[key: string]: string|number}[] {
let xlsxData: {[key: string]: string|number}[] = [];
private _buildXlsxData(): unknown[][] {
let xlsxData: unknown[][] = [];
let labels: string[] = [];
switch (this.widgetType) {
default:
case AjfWidgetType.Chart:
this.data = this.data as ChartData;
const datasets = this.data.datasets || [];
labels = this.data.labels as string[];
labels = ['name'].concat(this.data.labels as string[]);
xlsxData.push(labels);
for (let i = 0; i < datasets.length; i++) {
const row: {[id: string]: any} = {};
const row: unknown[] = [];
const data = datasets[i].data || [];
row['name'] = datasets[i].label;
row.push(datasets[i].label);
for (let j = 0; j < data.length; j++) {
row[labels[j]] = data[j];
row.push(data[j]);
}
xlsxData.push(row);
}
break;
case AjfWidgetType.Table:
this.data = this.data as AjfTableCell[][];
this.data.forEach((row: AjfTableCell[], idxRow: number) => {
const res: {[id: string]: any} = {};
const res: unknown[] = [];
if (idxRow === 0) {
labels = row.map(r => r.value.changingThisBreaksApplicationSecurity);
row.forEach((elem: AjfTableCell) => {
labels.push(elem.value.changingThisBreaksApplicationSecurity);
if (elem.colspan && elem.colspan > 1) {
for (let i = 1; i < elem.colspan; i++) {
labels.push(' ');
}
}
});
xlsxData.push(labels);
} else {
row.forEach((elem: AjfTableCell, idxElem: number) => {
res[labels[idxElem]] = elem.value.changingThisBreaksApplicationSecurity;
row.forEach((elem: AjfTableCell) => {
res.push(elem.value.changingThisBreaksApplicationSecurity);
});
xlsxData.push(res);
}
Expand Down

0 comments on commit e0c01fa

Please sign in to comment.