Skip to content

Commit

Permalink
fix(exports): should be able to change export file name (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding authored Dec 18, 2020
1 parent 3aca8f9 commit 9d26213
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ export class Example2 {
sanitizeDataExport: true
},
enableExcelExport: true,
excelExportOptions: {
sanitizeDataExport: true
},
excelExportOptions: { filename: 'my-export', sanitizeDataExport: true },
textExportOptions: { filename: 'my-export', sanitizeDataExport: true },
registerExternalResources: [this.excelExportService, new TextExportService()],
showCustomFooter: true, // display some metrics in the bottom custom footer
customFooterOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ describe('contextMenuExtension', () => {
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onCommandSpy).toHaveBeenCalledWith(expect.anything(), { item: { command: 'help' }, column: {}, grid: gridStub, command: 'help' });
expect(onCommandSpy).toHaveBeenCalledWith(expect.anything(), { item: { command: 'help' }, column: {} as Column, grid: gridStub, command: 'help' });
expect(onOptionSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
Expand All @@ -299,7 +299,7 @@ describe('contextMenuExtension', () => {
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
expect.anything()
);
expect(onOptionSpy).toHaveBeenCalledWith(expect.anything(), { item: { option: 'help' }, column: {}, grid: gridStub, option: 'help' });
expect(onOptionSpy).toHaveBeenCalledWith(expect.anything(), { item: { option: 'help' }, column: {} as Column, grid: gridStub, option: 'help' });
expect(onCommandSpy).not.toHaveBeenCalled();
expect(onb4CloseSpy).not.toHaveBeenCalled();
expect(onb4ShowSpy).not.toHaveBeenCalled();
Expand Down Expand Up @@ -840,10 +840,7 @@ describe('contextMenuExtension', () => {
const menuItemCommand = ((copyGridOptionsMock.contextMenu as ContextMenu).commandItems as MenuCommandItem[]).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 @@ -858,9 +855,7 @@ describe('contextMenuExtension', () => {

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

Expand All @@ -876,9 +871,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 @@ -672,10 +672,7 @@ describe('gridMenuExtension', () => {
instance.onCommand!.notify({ item: { command: 'export-excel' }, column: {} as Column, 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 @@ -689,9 +686,7 @@ describe('gridMenuExtension', () => {
expect(onCommandSpy).toHaveBeenCalled();
expect(exportSpy).toHaveBeenCalledWith({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true
});
});

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

Expand Down
9 changes: 1 addition & 8 deletions packages/common/src/extensions/contextMenuExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ export class ContextMenuExtension implements Extension {
if (excelService?.exportToFile) {
excelService.exportToFile({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true,
});
} else {
throw new Error(`[Slickgrid-Universal] You must register the TextExportService to properly use Export to File in the Context Menu. Example:: this.gridOptions = { enableTextExport: true, registerExternalResources: [new TextExportService()] };`);
Expand All @@ -266,10 +264,7 @@ export class ContextMenuExtension implements Extension {
const registedServices = this.sharedService?.externalRegisteredResources || [];
const excelService: ExcelExportService = registedServices.find((service: any) => service.className === 'ExcelExportService');
if (excelService?.exportToExcel) {
excelService.exportToExcel({
filename: 'export',
format: FileType.xlsx,
});
excelService.exportToExcel();
} else {
throw new Error(`[Slickgrid-Universal] You must register the ExcelExportService to properly use Export to Excel in the Context Menu. Example:: this.gridOptions = { enableExcelExport: true, registerExternalResources: [new ExcelExportService()] };`);
}
Expand All @@ -296,9 +291,7 @@ export class ContextMenuExtension implements Extension {
if (excelService?.exportToFile) {
excelService.exportToFile({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true,
});
} else {
throw new Error(`[Slickgrid-Universal] You must register the TextExportService to properly use Export to File in the Context Menu. Example:: this.gridOptions = { enableTextExport: true, registerExternalResources: [new TextExportService()] };`);
Expand Down
9 changes: 1 addition & 8 deletions packages/common/src/extensions/gridMenuExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@ export class GridMenuExtension implements Extension {
if (exportCsvService?.exportToFile) {
exportCsvService.exportToFile({
delimiter: DelimiterType.comma,
filename: 'export',
format: FileType.csv,
useUtf8WithBom: true,
});
} else {
throw new Error(`[Slickgrid-Universal] You must register the TextExportService to properly use Export to File in the Grid Menu. Example:: this.gridOptions = { enableTextExport: true, registerExternalResources: [new TextExportService()] };`);
Expand All @@ -429,10 +427,7 @@ export class GridMenuExtension implements Extension {
case 'export-excel':
const excelService: ExcelExportService = registeredResources.find((service: any) => service.className === 'ExcelExportService');
if (excelService?.exportToExcel) {
excelService.exportToExcel({
filename: 'export',
format: FileType.xlsx,
});
excelService.exportToExcel();
} else {
throw new Error(`[Slickgrid-Universal] You must register the ExcelExportService to properly use Export to Excel in the Grid Menu. Example:: this.gridOptions = { enableExcelExport: true, registerExternalResources: [new ExcelExportService()] };`);
}
Expand All @@ -442,9 +437,7 @@ export class GridMenuExtension implements Extension {
if (exportTxtService?.exportToFile) {
exportTxtService.exportToFile({
delimiter: DelimiterType.tab,
filename: 'export',
format: FileType.txt,
useUtf8WithBom: true,
});
} else {
throw new Error(`[Slickgrid-Universal] You must register the TextExportService to properly use Export to File in the Grid Menu. Example:: this.gridOptions = { enableTextExport: true, registerExternalResources: [new TextExportService()] };`);
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/services/excelExport.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export abstract class ExcelExportService implements ExternalResource {
* Method to return the current locale used by the App
* @return {string} current locale
*/
exportToExcel(_options: ExcelExportOption): Promise<boolean> {
exportToExcel(_options?: ExcelExportOption): Promise<boolean> {
throw new Error('ExcelExportService the "exportToExcel" method must be implemented');
}
}
2 changes: 1 addition & 1 deletion packages/common/src/services/textExport.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export abstract class TextExportService implements ExternalResource {
* Method to return the current locale used by the App
* @return {string} current locale
*/
exportToFile(_options: TextExportOption): Promise<boolean> {
exportToFile(_options?: TextExportOption): Promise<boolean> {
throw new Error('ExportService the "exportToFile" method must be implemented');
}
}
9 changes: 7 additions & 2 deletions packages/excel-export/src/excelExport.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import {
ExcelWorksheet,
} from './interfaces/index';

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

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

return new Promise(resolve => {
this._pubSubService?.publish(`onBeforeExportToExcel`, true);
this._excelExportOptions = deepCopy({ ...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
12 changes: 10 additions & 2 deletions packages/text-export/src/textExport.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Column,
Constants,
ContainerService,
DelimiterType,
ExternalResource,
FileType,
GridOption,
Expand All @@ -26,6 +27,13 @@ import {
TranslaterService,
} from '@slickgrid-universal/common';

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

export class TextExportService implements ExternalResource, BaseTextExportService {
private _delimiter = ',';
private _exportQuoteWrapper = '';
Expand Down Expand Up @@ -86,14 +94,14 @@ export class TextExportService implements ExternalResource, BaseTextExportServic
*
* Example: exportToFile({ format: FileType.csv, delimiter: DelimiterType.comma })
*/
exportToFile(options: TextExportOption): Promise<boolean> {
exportToFile(options?: TextExportOption): Promise<boolean> {
if (!this._grid || !this._dataView || !this._pubSubService) {
throw new Error('[Slickgrid-Universal] it seems that the SlickGrid & DataView objects and/or PubSubService are not initialized did you forget to enable the grid option flag "enableTextExport"?');
}

return new Promise(resolve => {
this._pubSubService?.publish(`onBeforeExportToTextFile`, true);
this._exportOptions = deepCopy({ ...this._gridOptions.exportOptions, ...this._gridOptions.textExportOptions, ...options });
this._exportOptions = deepCopy({ ...DEFAULT_EXPORT_OPTIONS, ...this._gridOptions.exportOptions, ...this._gridOptions.textExportOptions, ...options });
this._delimiter = this._exportOptions.delimiterOverride || this._exportOptions.delimiter || '';
this._fileFormat = this._exportOptions.format || FileType.csv;

Expand Down

0 comments on commit 9d26213

Please sign in to comment.