diff --git a/src/features/exporter/js/exporter.js b/src/features/exporter/js/exporter.js index 3b0f15ee4e..7807fb6cff 100755 --- a/src/features/exporter/js/exporter.js +++ b/src/features/exporter/js/exporter.js @@ -242,6 +242,24 @@ *
Defaults to 'download.pdf' */ gridOptions.exporterPdfFilename = gridOptions.exporterPdfFilename ? gridOptions.exporterPdfFilename : 'download.pdf'; + /** + * @ngdoc object + * @name exporterExcelFilename + * @propertyOf ui.grid.exporter.api:GridOptions + * @description The default filename to use when saving the downloaded excel, only used in IE (other browsers open excels in a new window) + *
Defaults to 'download.xlsx' + */ + gridOptions.exporterExcelFilename = gridOptions.exporterExcelFilename ? gridOptions.exporterExcelFilename : 'download.xlsx'; + + /** + * @ngdoc object + * @name exporterExcelSheetName + * @propertyOf ui.grid.exporter.api:GridOptions + * @description The default sheetname to use when saving the downloaded to excel + *
Defaults to 'Sheet1' + */ + gridOptions.exporterExcelSheetName = gridOptions.exporterExcelSheetName ? gridOptions.exporterExcelSheetName : 'Sheet1'; + /** * @ngdoc object * @name exporterOlderExcelCompatibility @@ -560,6 +578,66 @@ */ gridOptions.exporterFieldFormatCallback = gridOptions.exporterFieldFormatCallback ? gridOptions.exporterFieldFormatCallback : function( grid, row, col, value ) { return null; }; + /** + * @ngdoc function + * @name exporterExcelCustomFormatters + * @propertyOf ui.grid.exporter.api:GridOptions + * @description A function to call to setup formatters and store on docDefinition. + * + * The method is called at the start and can setup all the formatters to export to excel + * + * @param {Grid} grid provides the grid in case you have need of it + * @param {Workbook} row the row from which the data comes + * @param {docDefinition} The docDefinition that will have styles as a object to store formatters + * @returns {docDefinition} Updated docDefinition with formatter styles + * + * @example + *
+           *   gridOptions.exporterExcelCustomFormatters = function(grid, workbook, docDefinition) {
+           *     const formatters = {};
+           *     const stylesheet = workbook.getStyleSheet();
+           *     const headerFormatDefn = {
+           *       'font': { 'size': 11, 'fontName': 'Calibri', 'bold': true },
+           *       'alignment': { 'wrapText': false }
+           *     };
+           *
+           *     formatters['header'] = headerFormatter;
+           *     Object.assign(docDefinition.styles , formatters);
+           *     grid.docDefinition = docDefinition;
+           *     return docDefinition;
+           *   }
+           * 
+ */ + gridOptions.exporterExcelCustomFormatters = gridOptions.exporterExcelCustomFormatters ? gridOptions.exporterExcelCustomFormatters : function( grid, workbook, docDefinition ) { return null; }; + + /** + * @ngdoc function + * @name exporterExcelHeader + * @propertyOf ui.grid.exporter.api:GridOptions + * @description A function to write formatted header data to sheet. + * + * The method is called to provide custom header building for Excel. This data comes before the grid header + * + * @param {grid} grid provides the grid in case you have need of it + * @param {Workbook} row the row from which the data comes + * @param {Sheet} the sheet to insert data + * @param {docDefinition} The docDefinition that will have styles as a object to store formatters + * @returns {docDefinition} Updated docDefinition with formatter styles + * + * @example + *
+           *   gridOptions.exporterExcelCustomFormatters = function (grid, workbook, sheet, docDefinition) {
+           *      const headerFormatter = docDefinition.styles['header'];
+           *      let cols = [];
+           *      // push data in A1 cell with metadata formatter
+           *      cols.push({ value: 'Summary Report', metadata: {style: headerFormatter.id} });
+           *      sheet.data.push(cols);
+           *   }
+           * 
+ */ + gridOptions.exporterExcelHeader = gridOptions.exporterExcelHeader ? gridOptions.exporterExcelHeader : function( grid, workbook, sheet, docDefinition ) { return null; }; + + /** * @ngdoc object * @name exporterColumnScaleFactor diff --git a/src/features/exporter/test/exporter.spec.js b/src/features/exporter/test/exporter.spec.js index 0cd4c1329f..bc9099f3cc 100644 --- a/src/features/exporter/test/exporter.spec.js +++ b/src/features/exporter/test/exporter.spec.js @@ -130,6 +130,8 @@ describe('ui.grid.exporter', function() { exporterCsvColumnSeparator: ',', exporterCsvFilename: 'download.csv', exporterPdfFilename: 'download.pdf', + exporterExcelFilename: 'download.xlsx', + exporterExcelSheetName: 'Sheet1', exporterOlderExcelCompatibility: false, exporterIsExcelCompatible: false, exporterPdfDefaultStyle: {fontSize: 11}, @@ -150,10 +152,12 @@ describe('ui.grid.exporter', function() { exporterMenuExcel: true, exporterFieldCallback: jasmine.any(Function), exporterFieldFormatCallback: jasmine.any(Function), + exporterExcelCustomFormatters: jasmine.any(Function), + exporterExcelHeader: jasmine.any(Function), + exporterColumnScaleFactor: 3.5, exporterFieldApplyFilters: false, exporterAllDataFn: null, exporterSuppressColumns: [], - exporterColumnScaleFactor: 3.5, exporterMenuItemOrder: 200 }); }); @@ -174,6 +178,8 @@ describe('ui.grid.exporter', function() { exporterCsvColumnSeparator: ',', exporterCsvFilename: 'download.csv', exporterPdfFilename: 'download.pdf', + exporterExcelFilename: 'download.xlsx', + exporterExcelSheetName: 'Sheet1', exporterOlderExcelCompatibility: false, exporterIsExcelCompatible: false, exporterPdfDefaultStyle : { fontSize : 11 }, @@ -194,10 +200,12 @@ describe('ui.grid.exporter', function() { exporterMenuExcel: true, exporterFieldCallback: jasmine.any(Function), exporterFieldFormatCallback: jasmine.any(Function), + exporterExcelCustomFormatters: jasmine.any(Function), + exporterExcelHeader: jasmine.any(Function), + exporterColumnScaleFactor: 3.5, exporterFieldApplyFilters: false, exporterAllDataFn: null, exporterSuppressColumns: [], - exporterColumnScaleFactor: 3.5, exporterMenuItemOrder: 200 }); }); @@ -231,10 +239,10 @@ describe('ui.grid.exporter', function() { exporterMenuExcel: false, exporterFieldCallback: callback, exporterFieldFormatCallback: callback, + exporterExcelCustomFormatters: callback, exporterFieldApplyFilters: false, exporterAllDataPromise: callback, exporterSuppressColumns: [ 'buttons' ], - exporterExcelCustomFormatters: callback, exporterExcelFilename: 'myFile.xlsx', exporterExcelSheetName: 'Sheet1', exporterExcelHeader: 'My Header',