Skip to content

Commit

Permalink
feat(core/reports): export all exportable widgets in columns and layout
Browse files Browse the repository at this point in the history
  • Loading branch information
sara-gnucoop committed Sep 28, 2023
1 parent 71949e2 commit 4d7dc7b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 46 deletions.
4 changes: 1 addition & 3 deletions projects/core/reports/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {AfterViewInit, ChangeDetectorRef, Directive, Input} from '@angular/core'

import {AjfReportInstance} from './interface/reports-instances/report-instance';
import {AjfReport} from './interface/reports/report';
import {exportableWidgetTypes} from './widget-export';

@Directive()
export abstract class AjfReportRenderer implements AfterViewInit {
Expand Down Expand Up @@ -58,8 +57,7 @@ export abstract class AjfReportRenderer implements AfterViewInit {
this.enableExportAll &&
this._instance != null &&
this._instance.content &&
this._instance.content.content &&
this._instance.content.content.some(inst => exportableWidgetTypes.includes(inst.widgetType))
this._instance.content.content
? true
: false;
this._cdr.markForCheck();
Expand Down
96 changes: 53 additions & 43 deletions projects/core/reports/src/widget-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ import {format} from 'date-fns';
import {utils, WorkBook, WorkSheet, writeFile} from 'xlsx';
import {AjfReportInstance} from './interface/reports-instances/report-instance';
import {AjfChartWidgetInstance} from './interface/widgets-instances/chart-widget-instance';
import {AjfColumnWidgetInstance} from './interface/widgets-instances/column-widget-instance';
import {AjfLayoutWidgetInstance} from './interface/widgets-instances/layout-widget-instance';
import {AjfTableWidgetInstance} from './interface/widgets-instances/table-widget-instance';
import {AjfTextWidgetInstance} from './interface/widgets-instances/text-widget-instance';
import {AjfWidgetInstance} from './interface/widgets-instances/widget-instance';

import {AjfWidgetType} from './interface/widgets/widget-type';

export const exportableWidgetTypes = [
AjfWidgetType.Chart,
AjfWidgetType.Table,
AjfWidgetType.DynamicTable,
AjfWidgetType.PaginatedTable,
];

/**
* Export all widgets data in Xlsx format, one per sheet
* @param report the ajf report instance
Expand All @@ -47,10 +43,10 @@ export const exportableWidgetTypes = [
export function exportReportXlsx(
report: AjfReportInstance,
iconsMap: {[html: string]: string} | undefined,
) {
): boolean {
iconsMap = iconsMap ? iconsMap : {};
const widgetInstances = report && report.content ? report.content.content : undefined;
exportAllWidgets(widgetInstances, iconsMap);
return exportAllWidgets(widgetInstances, iconsMap);
}

/**
Expand Down Expand Up @@ -152,53 +148,67 @@ function buildXlsxData(
return xlsxData;
}

function addExportableWidgetsToSheets(
widget: AjfWidgetInstance,
iconsMap: {[html: string]: string},
sheets: {[sheet: string]: WorkSheet},
): void {
const idx = Object.keys(sheets).length;
const sheetName = `${idx}_${AjfWidgetType[widget.widgetType]}`;
switch (widget.widget.widgetType) {
case AjfWidgetType.Layout:
const lw = widget as AjfLayoutWidgetInstance;
lw.content.map(w => addExportableWidgetsToSheets(w, iconsMap, sheets));
break;
case AjfWidgetType.Column:
const cw = widget as AjfColumnWidgetInstance;
cw.content.map(w => addExportableWidgetsToSheets(w, iconsMap, sheets));
break;
case AjfWidgetType.Chart:
const chartInstance = widget as AjfChartWidgetInstance;
sheets[sheetName] = utils.aoa_to_sheet(
buildXlsxData(chartInstance.widgetType, chartInstance.data, iconsMap),
);
break;
case AjfWidgetType.Text:
const tw = widget as AjfTextWidgetInstance;
sheets[sheetName] = utils.aoa_to_sheet([[tw.htmlText]]);
break;
case AjfWidgetType.Table:
case AjfWidgetType.DynamicTable:
case AjfWidgetType.PaginatedTable:
const tableInstance = widget as AjfTableWidgetInstance;
sheets[sheetName] = utils.aoa_to_sheet(
buildXlsxData(tableInstance.widgetType, tableInstance.data, iconsMap),
);
break;
}
}

/**
* Export all widgets data in Xlsx format, one per sheet
* Export all exportable widgets in Xlsx format, one per sheet
*/
function exportAllWidgets(
widgets: AjfWidgetInstance[] | undefined,
iconsMap: {[html: string]: string},
): void {
): boolean {
const bookType = 'xlsx';
if (
widgets &&
widgets.length &&
widgets.some(inst => exportableWidgetTypes.includes(inst.widgetType))
) {
const sheets: {[sheet: string]: WorkSheet} = {};
const sheetNames: string[] = [];

let idx = 0;
let fileName = `AllWidgets_${format(new Date(), `yyyy-MM-dd`)}`;
const sheets: {[sheet: string]: WorkSheet} = {};
let fileName = `AllWidgets_${format(new Date(), `yyyy-MM-dd`)}`;
if (widgets && widgets.length) {
widgets.forEach(instance => {
if (exportableWidgetTypes.includes(instance.widgetType)) {
sheetNames[idx] = `${idx}_${AjfWidgetType[instance.widgetType]}`;
switch (instance.widgetType) {
case AjfWidgetType.Chart:
const chartInstance = instance as AjfChartWidgetInstance;
sheets[sheetNames[idx]] = utils.aoa_to_sheet(
buildXlsxData(chartInstance.widgetType, chartInstance.data, iconsMap),
);
break;
case AjfWidgetType.DynamicTable:
case AjfWidgetType.Table:
case AjfWidgetType.PaginatedTable:
const tableInstance = instance as AjfTableWidgetInstance;
sheets[sheetNames[idx]] = utils.aoa_to_sheet(
buildXlsxData(tableInstance.widgetType, tableInstance.data, iconsMap),
);
break;
}
idx++;
}
addExportableWidgetsToSheets(instance, iconsMap, sheets);
});

const workBook: WorkBook = {Sheets: sheets, SheetNames: sheetNames};
}
if (Object.keys(sheets).length) {
const workBook: WorkBook = {Sheets: sheets, SheetNames: Object.keys(sheets)};
writeFile(workBook, `${fileName}.${bookType}`, {
bookType,
type: 'array',
});
return true;
}
return false;
}

@Component({
Expand Down

0 comments on commit 4d7dc7b

Please sign in to comment.