From 996f9ca9403d488cde2a0bc5bfcd981016396d46 Mon Sep 17 00:00:00 2001 From: astaev Date: Fri, 21 Dec 2018 17:01:14 +0200 Subject: [PATCH 1/4] fix(#3332): Exception when exporting more than 8 level --- .../src/lib/services/excel/excel-exporter.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts b/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts index b963347131b..c5e7b6678c2 100644 --- a/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts +++ b/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts @@ -75,6 +75,13 @@ export class IgxExcelExporterService extends IgxBaseExporter { } protected exportDataImplementation(data: any[], options: IgxExcelExporterOptions): void { + let maxLevel = 0; + data.forEach((r) => { + maxLevel = Math.max(maxLevel, r.originalRowData.level); + }); + if (maxLevel > 7) { + throw Error('Can create an outline of up to eight levels!'); + } const worksheetData = new WorksheetData(data, options, this._indexOfLastPinnedColumn, this._sort, this._isTreeGrid); this._xlsx = new JSZip(); From 6062580aa2d156624016939fdc28fff88e554731 Mon Sep 17 00:00:00 2001 From: gedinakova Date: Thu, 27 Dec 2018 13:38:50 +0200 Subject: [PATCH 2/4] fix(Exporter): #3332 Adding a test. --- .../excel/excel-exporter-grid.spec.ts | 22 +++++++++++++++ .../src/lib/services/excel/excel-exporter.ts | 28 ++++++++----------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts b/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts index b5249922387..ff6e7a45a74 100644 --- a/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts +++ b/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts @@ -19,6 +19,7 @@ import { configureTestSuite } from '../../test-utils/configure-suite'; import { IgxTreeGridPrimaryForeignKeyComponent } from '../../test-utils/tree-grid-components.spec'; import { IgxTreeGridModule, IgxTreeGridComponent } from '../../grids/tree-grid'; import { IgxNumberFilteringOperand } from '../../data-operations/filtering-condition'; +import { wait } from '../../test-utils/ui-interactions.spec'; describe('Excel Exporter', () => { configureTestSuite(); @@ -449,6 +450,27 @@ describe('Excel Exporter', () => { fix.detectChanges(); await exportAndVerify(treeGrid, options, actualData.treeGridDataExpDepth(0)); }); + + it('should throw an exception when nesting level is greater than 8.', async () => { + const nestedData = SampleTestData.employeePrimaryForeignKeyTreeData(); + for (let i = 1; i < 9; i++) { + nestedData[i - 1].ID = i; + nestedData[i - 1].ParentID = i - 1; + } + nestedData.push({ ID: 9, ParentID: 8, Name: 'Test', JobTitle: '', Age: 49 }); + treeGrid.data = nestedData; + fix.detectChanges(); + await wait(16); + + let error = ''; + try { + exporter.export(treeGrid, options); + await wait(); + } catch (ex) { + error = ex.message; + } + expect(error).toMatch('Can create an outline of up to eight levels!'); + }); }); function getExportedData(grid, exportOptions: IgxExcelExporterOptions) { diff --git a/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts b/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts index c5e7b6678c2..d6745a60789 100644 --- a/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts +++ b/projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts @@ -1,17 +1,10 @@ import * as JSZip from 'jszip/dist/jszip'; -import { CommonModule } from '@angular/common'; -import { Directive, EventEmitter, Injectable, NgModule, Output } from '@angular/core'; - +import { EventEmitter, Injectable, Output } from '@angular/core'; import { ExcelElementsFactory } from './excel-elements-factory'; import { ExcelFolderTypes } from './excel-enums'; import { IgxExcelExporterOptions } from './excel-exporter-options'; - -import { - IExcelFile, - IExcelFolder -} from './excel-interfaces'; - +import { IExcelFolder } from './excel-interfaces'; import { IgxBaseExporter } from '../exporter-common/base-export-service'; import { ExportUtilities } from '../exporter-common/export-utilities'; import { WorksheetData } from './worksheet-data'; @@ -45,8 +38,6 @@ export interface IExcelExportEndedEventArgs { export class IgxExcelExporterService extends IgxBaseExporter { private static ZIP_OPTIONS = { compression: 'DEFLATE', type: 'base64' }; - private static DATA_URL_PREFIX = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,'; - private _xlsx: JSZip; /** @@ -75,13 +66,16 @@ export class IgxExcelExporterService extends IgxBaseExporter { } protected exportDataImplementation(data: any[], options: IgxExcelExporterOptions): void { - let maxLevel = 0; - data.forEach((r) => { - maxLevel = Math.max(maxLevel, r.originalRowData.level); - }); - if (maxLevel > 7) { - throw Error('Can create an outline of up to eight levels!'); + if (this._isTreeGrid) { + let maxLevel = 0; + data.forEach((r) => { + maxLevel = Math.max(maxLevel, r.originalRowData.level); + }); + if (maxLevel > 7) { + throw Error('Can create an outline of up to eight levels!'); + } } + const worksheetData = new WorksheetData(data, options, this._indexOfLastPinnedColumn, this._sort, this._isTreeGrid); this._xlsx = new JSZip(); From f4f11f60b58dcb0bb7341da339243aa9a7fc8037 Mon Sep 17 00:00:00 2001 From: astaev Date: Thu, 3 Jan 2019 14:28:13 +0200 Subject: [PATCH 3/4] fix(Exporter): #3332 Improve the fix and test --- .../excel/excel-exporter-grid.spec.ts | 26 +++++++++++++++++++ .../exporter-common/base-export-service.ts | 3 +-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts b/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts index ff6e7a45a74..f645446d478 100644 --- a/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts +++ b/projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts @@ -470,6 +470,32 @@ describe('Excel Exporter', () => { error = ex.message; } expect(error).toMatch('Can create an outline of up to eight levels!'); + + treeGrid.deleteRowById(9); + fix.detectChanges(); + await wait(16); + + error = ''; + try { + exporter.export(treeGrid, options); + await wait(); + } catch (ex) { + error = ex.message; + } + expect(error).toEqual(''); + + treeGrid.addRow({ ID: 9, ParentID: 8, Name: 'Test', JobTitle: '', Age: 49 }); + fix.detectChanges(); + await wait(16); + + error = ''; + try { + exporter.export(treeGrid, options); + await wait(); + } catch (ex) { + error = ex.message; + } + expect(error).toMatch('Can create an outline of up to eight levels!'); }); }); diff --git a/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts b/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts index 42b22eb17f9..07b18db4f34 100644 --- a/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts +++ b/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts @@ -192,6 +192,7 @@ export abstract class IgxBaseExporter { } private prepareData(grid: any, options: IgxExporterOptionsBase): any[] { + this.flatRecords = []; let rootRecords = grid.rootRecords; this._isTreeGrid = rootRecords !== undefined; @@ -210,7 +211,6 @@ export abstract class IgxBaseExporter { }; if (this._isTreeGrid) { - this.flatRecords = []; rootRecords = DataUtil.treeGridFilter(rootRecords, filteringState); this.prepareHierarchicalData(rootRecords); data = this.flatRecords; @@ -225,7 +225,6 @@ export abstract class IgxBaseExporter { this._sort = cloneValue(grid.sortingExpressions[0]); if (this._isTreeGrid) { - this.flatRecords = []; rootRecords = DataUtil.treeGridSort(rootRecords, grid.sortingExpressions); this.prepareHierarchicalData(rootRecords); data = this.flatRecords; From af24b2b22bb59a1787a012099ff8149e9b3107c8 Mon Sep 17 00:00:00 2001 From: astaev Date: Thu, 3 Jan 2019 17:21:02 +0200 Subject: [PATCH 4/4] fix(Exporter): #3332 Fix was braking the existing export --- .../src/lib/services/exporter-common/base-export-service.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts b/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts index 07b18db4f34..1f3d768d405 100644 --- a/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts +++ b/projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts @@ -211,6 +211,7 @@ export abstract class IgxBaseExporter { }; if (this._isTreeGrid) { + this.flatRecords = []; rootRecords = DataUtil.treeGridFilter(rootRecords, filteringState); this.prepareHierarchicalData(rootRecords); data = this.flatRecords; @@ -225,6 +226,7 @@ export abstract class IgxBaseExporter { this._sort = cloneValue(grid.sortingExpressions[0]); if (this._isTreeGrid) { + this.flatRecords = []; rootRecords = DataUtil.treeGridSort(rootRecords, grid.sortingExpressions); this.prepareHierarchicalData(rootRecords); data = this.flatRecords;