Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#3332): Exception when exporting more than 8 nested levels #3501

Merged
merged 10 commits into from
Jan 4, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 12 additions & 11 deletions projects/igniteui-angular/src/lib/services/excel/excel-exporter.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down