Skip to content

Commit

Permalink
Excel export with npm instructions, fix error on menu and more exampl…
Browse files Browse the repository at this point in the history
…es (#6506)

* Initial checkin of export to excel. No examples or unit tests

* Eliminate $$hashKey and uidPrefix = 'uiGrid' from export

* Sheet name cannot be empty string or it causes export errors. Default to 'Sheet1'

* Additional languages

* Add documentation

* fix syntax causing lint errors. Check if not a tree when exporting to prevent error

* Updated example with colors

* Update unit tests and fix the lodash version for import to remove ambiguity.

* Update comments

* Fix menu under strict mode from throwing error

* Updated directions for npm and add more formatting examples

* Fix test cases for excel export that was a merge error
  • Loading branch information
monster910 authored and mportuga committed Dec 19, 2017
1 parent 88f5525 commit bc37cb4
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
5 changes: 5 additions & 0 deletions misc/tutorial/206_exporting_data.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ available through:
<pre> bower install lodash
bower install jszip#2.6.1
bower install excelbuilder </pre>
or

<pre> npm install lodash
npm install [email protected]
npm install excel-builder </pre>

The options and API for exporter can be found at {@link api/ui.grid.exporter ui.grid.exporter}, and

Expand Down
40 changes: 33 additions & 7 deletions misc/tutorial/410_excel_exporting_data_and_header.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ available through:
bower install jszip#2.6.1
bower install excelbuilder </pre>

or

<pre> npm install lodash
npm install [email protected]
npm install excel-builder </pre>


The options and API for exporter can be found at {@link api/ui.grid.exporter ui.grid.exporter}, and

- {@link api/ui.grid.exporter.api:ColumnDef columnDef}
Expand Down Expand Up @@ -48,7 +55,9 @@ In this example we use the native grid menu buttons, and we show the pdf, csv an
columnDefs: [
{ field: 'name' },
{ field: 'gender', visible: false},
{ field: 'company' }
{ field: 'company' },
{ field: 'member' },
{ field: 'total' }
],
enableGridMenu: true,
enableSelectAll: true,
Expand All @@ -74,19 +83,29 @@ In this example we use the native grid menu buttons, and we show the pdf, csv an
exporterExcelCustomFormatters: function ( grid, workbook, docDefinition ) {

var stylesheet = workbook.getStyleSheet();
var stdStyle = stylesheet.createFontStyle({
size: 9, fontName: 'Calibri'
});
var boldStyle = stylesheet.createFontStyle({
size: 9, fontName: 'Calibri', bold: true
});
var aFormatDefn = {
"font": { "size": 9, "fontName": "Calibri", "bold": true },
"font": boldStyle.id,
"alignment": { "wrapText": true }
};
var formatter = stylesheet.createFormat(aFormatDefn);
// save the formatter
$scope.formatters['bold'] = formatter;

aFormatDefn = {
"font": { "size": 9, "fontName": "Calibri" },
"font": stdStyle.id,
"fill": { "type": "pattern", "patternType": "solid", "fgColor": "FFFFC7CE" },
"alignment": { "wrapText": true }
};
var singleDefn = {
font: stdStyle.id,
format: '#,##0.0'
};
formatter = stylesheet.createFormat(aFormatDefn);
// save the formatter
$scope.formatters['red'] = formatter;
Expand All @@ -99,22 +118,25 @@ In this example we use the native grid menu buttons, and we show the pdf, csv an
// this can be defined outside this method
var stylesheet = workbook.getStyleSheet();
var aFormatDefn = {
"font": { "size": 9, "fontName": "Calibri", "bold": true },
"alignment": { "wrapText": true }
};
"font": { "size": 11, "fontName": "Calibri", "bold": true },
"alignment": { "wrapText": true }
};
var formatterId = stylesheet.createFormat(aFormatDefn);

// excel cells start with A1 which is upper left corner
sheet.mergeCells('B1', 'C1');
var cols = [];
// push empty data
cols.push({ value: '' });
// push data in B1 cell with metadata formatter
cols.push({ value: 'My header that is long enough to wrap', metadata: {style: formatterId.id} });
sheet.data.push(cols);
},
exporterFieldFormatCallback: function(grid, row, gridCol, cellValue) {
// set metadata on export data to set format id. See exportExcelHeader config above for example of creating
// a formatter and obtaining the id
var formatterId = null;
if (cellValue && cellValue.startsWith('W')) {
if (cellValue && typeof cellValue === 'string' && cellValue.startsWith('W')) {
formatterId = $scope.formatters['red'].id;
}

Expand All @@ -131,6 +153,10 @@ In this example we use the native grid menu buttons, and we show the pdf, csv an

$http.get('/data/100.json')
.success(function(data) {
for (var i=0; i < data.length; i++) {
data[i].member = false;
data[i].total = i + 0.1;
}
$scope.gridOptions.data = data;
});

Expand Down
45 changes: 44 additions & 1 deletion src/features/exporter/test/exporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('ui.grid.exporter', function() {
grid.api.registerMethodsFromObject.calls.reset();
});
describe('public events', function() {
it('should define exporter public events as an empty object', function() {
it('should define exporter public events as an empty object', function () {
expect(publicEvents.exporter).toEqual({});
});
});
Expand Down Expand Up @@ -115,6 +115,49 @@ describe('ui.grid.exporter', function() {
});
});

describe('defaultGridOptions', function() {
var options;

beforeEach(function () {
options = {};
});

it('set all options to default', function () {
uiGridExporterService.defaultGridOptions(options);
expect(options).toEqual({
exporterSuppressMenu: false,
exporterMenuLabel: 'Export',
exporterCsvColumnSeparator: ',',
exporterCsvFilename: 'download.csv',
exporterPdfFilename: 'download.pdf',
exporterOlderExcelCompatibility: false,
exporterIsExcelCompatible: false,
exporterPdfDefaultStyle: {fontSize: 11},
exporterPdfTableStyle: {margin: [0, 5, 0, 15]},
exporterPdfTableHeaderStyle: {bold: true, fontSize: 12, color: 'black'},
exporterPdfHeader: null,
exporterPdfFooter: null,
exporterPdfOrientation: 'landscape',
exporterPdfPageSize: 'A4',
exporterPdfMaxGridWidth: 720,
exporterPdfCustomFormatter: jasmine.any(Function),
exporterHeaderFilterUseName: false,
exporterMenuAllData: true,
exporterMenuVisibleData: true,
exporterMenuSelectedData: true,
exporterMenuCsv: true,
exporterMenuPdf: true,
exporterMenuExcel: true,
exporterFieldCallback: jasmine.any(Function),
exporterFieldFormatCallback: jasmine.any(Function),
exporterFieldApplyFilters: false,
exporterAllDataFn: null,
exporterSuppressColumns: [],
exporterMenuItemOrder: 200
});
});
});

describe('defaultGridOptions', function() {
var options;

Expand Down

0 comments on commit bc37cb4

Please sign in to comment.