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..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 @@ -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,53 @@ 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!'); + + 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!'); + }); }); 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 b963347131b..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,6 +66,16 @@ export class IgxExcelExporterService extends IgxBaseExporter { } protected exportDataImplementation(data: any[], options: IgxExcelExporterOptions): void { + 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(); 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..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 @@ -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;