Skip to content

Commit

Permalink
fix(exports): should be able to change export file name, fixes #655
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jan 5, 2021
1 parent 48b4ebc commit 7b2a9ef
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,7 @@ describe('contextMenuExtension', () => {
const menuItemCommand = copyGridOptionsMock.contextMenu.commandItems.find((item: MenuCommandItem) => item.command === 'export-excel') as MenuCommandItem;
menuItemCommand.action(new CustomEvent('change'), { command: 'export-excel', cell: 0, row: 0 } as any);

expect(excelExportSpy).toHaveBeenCalledWith({
filename: 'export',
format: FileType.xlsx,
});
expect(excelExportSpy).toHaveBeenCalled();
});

it('should call "exportToFile" with CSV set when the command triggered is "export-csv"', () => {
Expand All @@ -870,9 +867,7 @@ describe('contextMenuExtension', () => {

expect(exportSpy).toHaveBeenCalledWith({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true
});
});

Expand All @@ -887,9 +882,7 @@ describe('contextMenuExtension', () => {

expect(exportSpy).toHaveBeenCalledWith({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,7 @@ describe('gridMenuExtension', () => {
instance.onCommand.notify({ grid: gridStub, command: 'export-excel' }, new Slick.EventData(), gridStub);

expect(onCommandSpy).toHaveBeenCalled();
expect(excelExportSpy).toHaveBeenCalledWith({
filename: 'export',
format: FileType.xlsx,
});
expect(excelExportSpy).toHaveBeenCalled();
});

it('should call "exportToFile" with CSV set when the command triggered is "export-csv"', () => {
Expand All @@ -678,9 +675,7 @@ describe('gridMenuExtension', () => {
expect(onCommandSpy).toHaveBeenCalled();
expect(exportSpy).toHaveBeenCalledWith({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true
});
});

Expand All @@ -694,9 +689,7 @@ describe('gridMenuExtension', () => {
expect(onCommandSpy).toHaveBeenCalled();
expect(exportSpy).toHaveBeenCalledWith({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ export class ContextMenuExtension implements Extension {
positionOrder: 51,
action: () => this.exportService.exportToFile({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true,
}),
}
);
Expand All @@ -250,10 +248,7 @@ export class ContextMenuExtension implements Extension {
disabled: false,
command: commandName,
positionOrder: 52,
action: () => this.excelExportService.exportToExcel({
filename: 'export',
format: FileType.xlsx,
}),
action: () => this.excelExportService.exportToExcel(),
}
);
}
Expand All @@ -272,9 +267,7 @@ export class ContextMenuExtension implements Extension {
positionOrder: 53,
action: () => this.exportService.exportToFile({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true,
}),
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,23 +398,16 @@ export class GridMenuExtension implements Extension {
case 'export-csv':
this.exportService.exportToFile({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true,
});
break;
case 'export-excel':
this.excelExportService.exportToExcel({
filename: 'export',
format: FileType.xlsx,
});
this.excelExportService.exportToExcel();
break;
case 'export-text-delimited':
this.exportService.exportToFile({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true,
});
break;
case 'toggle-filter':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ import {
} from '../models/index';
import { Constants } from '../constants';
import { exportWithFormatterWhenDefined } from './export-utilities';
import { addWhiteSpaces, getTranslationPrefix, mapMomentDateFormatWithFieldType, sanitizeHtmlToText, titleCase } from './utilities';
import { addWhiteSpaces, deepCopy, getTranslationPrefix, mapMomentDateFormatWithFieldType, sanitizeHtmlToText, titleCase } from './utilities';

// using external non-typed js libraries
declare let $: any;

const DEFAULT_EXPORT_OPTIONS: ExcelExportOption = {
filename: 'export',
format: FileType.xlsx
};

@Injectable()
export class ExcelExportService {
private _fileFormat = FileType.xlsx;
Expand Down Expand Up @@ -80,14 +85,14 @@ export class ExcelExportService {
*
* Example: exportToExcel({ format: FileType.csv, delimiter: DelimiterType.comma })
*/
exportToExcel(options: ExcelExportOption): Promise<boolean> {
exportToExcel(options?: ExcelExportOption): Promise<boolean> {
if (!this._grid || !this._dataView) {
throw new Error('[Angular-Slickgrid] it seems that the SlickGrid & DataView objects are not initialized did you forget to enable the grid option flag "enableExcelExport"?');
}

return new Promise((resolve, reject) => {
this.onGridBeforeExportToExcel.next(true);
this._excelExportOptions = $.extend(true, {}, this._gridOptions.excelExportOptions, options);
this._excelExportOptions = deepCopy({ ...DEFAULT_EXPORT_OPTIONS, ...this._gridOptions.excelExportOptions, ...options });
this._fileFormat = this._excelExportOptions.format || FileType.xlsx;

// prepare the Excel Workbook & Sheet
Expand Down
14 changes: 11 additions & 3 deletions src/app/modules/angular-slickgrid/services/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Subject } from 'rxjs';

import {
Column,
DelimiterType,
ExportOption,
FileType,
GridOption,
Expand All @@ -13,11 +14,18 @@ import {
} from './../models/index';
import { Constants } from './../constants';
import { exportWithFormatterWhenDefined } from './export-utilities';
import { addWhiteSpaces, getTranslationPrefix, htmlEntityDecode, sanitizeHtmlToText, titleCase } from './../services/utilities';
import { addWhiteSpaces, deepCopy, getTranslationPrefix, htmlEntityDecode, sanitizeHtmlToText, titleCase } from './../services/utilities';

// using external non-typed js libraries
declare let $: any;

const DEFAULT_EXPORT_OPTIONS: ExportOption = {
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true,
};

@Injectable()
export class ExportService {
private _delimiter = ',';
Expand Down Expand Up @@ -72,14 +80,14 @@ export class ExportService {
*
* Example: exportToFile({ format: FileType.csv, delimiter: DelimiterType.comma })
*/
exportToFile(options: ExportOption): Promise<boolean> {
exportToFile(options?: ExportOption): Promise<boolean> {
if (!this._grid || !this._dataView) {
throw new Error('[Angular-Slickgrid] it seems that the SlickGrid & DataView objects are not initialized did you forget to enable the grid option flag "enableExcelExport"?');
}

return new Promise((resolve, reject) => {
this.onGridBeforeExportToFile.next(true);
this._exportOptions = $.extend(true, {}, this._gridOptions.exportOptions, options);
this._exportOptions = deepCopy({ ...DEFAULT_EXPORT_OPTIONS, ...this._gridOptions.exportOptions, ...options });
this._delimiter = this._exportOptions.delimiterOverride || this._exportOptions.delimiter || '';
this._fileFormat = this._exportOptions.format || FileType.csv;

Expand Down

0 comments on commit 7b2a9ef

Please sign in to comment.