UNPKG

@syncfusion/ej2-pivotview

Version:

The pivot grid, or pivot table, is used to visualize large sets of relational data in a cross-tabular format, similar to an Excel pivot table.

480 lines (479 loc) 31.5 kB
import { Workbook } from '@syncfusion/ej2-excel-export'; import * as events from '../../common/base/constant'; import { isNullOrUndefined } from '@syncfusion/ej2-base'; import { PivotExportUtil } from '../../base/export-util'; import { ExcelExportHelper } from './excel-export-helper'; /** * @hidden * `ExcelExport` module is used to handle the Excel export action. */ var ExcelExport = /** @class */ (function () { /** * Constructor for the PivotGrid Excel Export module. * * @param {PivotView} parent - Instance of pivot table. * @hidden */ function ExcelExport(parent) { this.actualrCnt = 0; /** @hidden */ this.images = []; this.workSheet = []; this.isHeaderIncluded = false; this.book = {}; this.parent = parent; this.excelExportHelper = new ExcelExportHelper(this.parent); this.rows = []; } /** * For internal use only - Get the module name. * * @returns {string} - string. * @private */ ExcelExport.prototype.getModuleName = function () { return 'excelExport'; }; ExcelExport.prototype.addHeaderAndFooter = function (excelExportProperties, stringValue, type, rowCount) { if (!this.rows) { this.rows = []; } var cells = []; if (!isNullOrUndefined(excelExportProperties.rows)) { this.actualrCnt = (type === 'footer') ? this.actualrCnt + rowCount - (excelExportProperties.rows[0].cells.length) : this.actualrCnt; var row = excelExportProperties.rows; for (var i = 0; i < row.length; i++) { var spanCount = 0; cells = []; var currentRow = row[i]; for (var j = 0; j < currentRow.cells.length; j++) { var cell = { index: spanCount + 1, value: currentRow.cells[j].value, colSpan: currentRow.cells[j].colSpan, rowSpan: currentRow.cells[j].rowSpan, style: !isNullOrUndefined(this.theme) && !isNullOrUndefined(this.theme.caption) ? this.excelExportHelper.getCaptionThemeStyle(this.theme, currentRow.cells[j].style) : currentRow.cells[j].style }; if (currentRow.cells[j].hyperlink) { cell.hyperlink = { target: currentRow.cells[j].hyperlink.target }; cell.value = currentRow.cells[j].value ? currentRow.cells[j].value : currentRow.cells[j].hyperlink.displayText; } cells.push(cell); spanCount = spanCount + cells[j].colSpan; } this.actualrCnt++; this.rows.push({ index: this.actualrCnt, cells: cells }); } this.actualrCnt = (type === 'header') ? rowCount : this.actualrCnt; } else { if (stringValue !== '') { if (type === 'footer') { this.actualrCnt++; } cells.push({ index: 1, value: stringValue }); this.rows.push({ index: this.actualrCnt + 1, cells: cells }); this.actualrCnt = (type === 'header') ? this.actualrCnt + 2 : this.actualrCnt; } } }; /** * * Method to perform excel export. * * @hidden */ ExcelExport.prototype.exportToExcel = function (type, exportProperties, isBlob, workBook, isMultipleExport, currentPivotInstance) { var _this = this; return new Promise(function (resolve, reject) { try { _this.actualrCnt = 0; var expType = 'AppendToSheet'; var blankRows = 5; if (!isNullOrUndefined(exportProperties) && !isNullOrUndefined(exportProperties.multipleExport)) { expType = (!isNullOrUndefined(exportProperties.multipleExport.type) ? exportProperties.multipleExport.type : 'AppendToSheet'); if (!isNullOrUndefined(exportProperties.multipleExport.blankRows)) { blankRows = exportProperties.multipleExport.blankRows; } } if (isNullOrUndefined(workBook)) { _this.workSheet = []; _this.rows = []; _this.columns = []; _this.images = []; } else if (expType === 'NewSheet') { _this.rows = []; _this.columns = []; _this.images = []; } else { _this.workSheet = []; _this.rows = workBook['worksheets'][0].rows; _this.columns = workBook['worksheets'][0].columns; _this.images = workBook['worksheets'][0].images; _this.actualrCnt = (_this.rows[_this.rows.length - 1].index + (blankRows - 1)); _this.actualrCnt++; } _this.parent = currentPivotInstance ? currentPivotInstance : _this.parent; var isHeaderDefined = !isNullOrUndefined(exportProperties) && !isNullOrUndefined(exportProperties.header); var isFooterDefined = !isNullOrUndefined(exportProperties) && !isNullOrUndefined(exportProperties.footer); var isFileNameDefined = !isNullOrUndefined(exportProperties) && !isNullOrUndefined(exportProperties.fileName); _this.engine = _this.parent.dataType === 'olap' ? _this.parent.olapEngineModule : _this.parent.engineModule; _this.theme = !isNullOrUndefined(exportProperties) ? exportProperties.theme : undefined; /** Event trigerring */ var clonedValues = void 0; var currentPivotValues = PivotExportUtil.getClonedPivotValues(_this.engine.pivotValues); var customFileName = isFileNameDefined ? exportProperties.fileName : type === 'CSV' ? 'default.csv' : 'default.xlsx'; if (_this.parent.exportAllPages && (_this.parent.enableVirtualization || _this.parent.enablePaging)) { var pageSettings = _this.engine.pageSettings; var mdxQuery = void 0; _this.engine.isPagingOrVirtualizationEnabled = false; if (_this.parent.dataType === 'olap') { _this.updateOlapPageSettings(true); mdxQuery = _this.parent.olapEngineModule.mdxQuery.slice(0); } else { _this.engine.pageSettings = null; } var dataSourceSettings = !isNullOrUndefined(currentPivotInstance) ? currentPivotInstance.dataSourceSettings : _this.parent.dataSourceSettings; _this.engine.generateGridData(dataSourceSettings, true, true); _this.parent.applyFormatting(_this.engine.pivotValues, dataSourceSettings); clonedValues = PivotExportUtil.getClonedPivotValues(_this.engine.pivotValues); _this.engine.pivotValues = currentPivotValues; _this.engine.pageSettings = pageSettings; _this.engine.isPagingOrVirtualizationEnabled = true; if (_this.parent.dataType === 'olap') { _this.updateOlapPageSettings(false); _this.parent.olapEngineModule.mdxQuery = mdxQuery; } } else { clonedValues = currentPivotValues; } var args = { fileName: customFileName, header: '', footer: '', dataCollections: [clonedValues], excelExportProperties: exportProperties }; var fileName_1; var header_1; var footer_1; var dataCollections_1; _this.parent.trigger(events.beforeExport, args, function (observedArgs) { fileName_1 = observedArgs.fileName; header_1 = observedArgs.header; footer_1 = observedArgs.footer; dataCollections_1 = observedArgs.dataCollections; }); if ((!_this.isHeaderIncluded && expType === 'AppendToSheet') || (expType === 'NewSheet')) { if (!isHeaderDefined && isNullOrUndefined(args.excelExportProperties) && header_1 !== '') { _this.addHeaderAndFooter({}, header_1, 'header', undefined); } else if (!isNullOrUndefined(args.excelExportProperties) && !isNullOrUndefined(args.excelExportProperties.header)) { _this.addHeaderAndFooter(args.excelExportProperties.header, '', 'header', args.excelExportProperties.header.headerRows); } _this.isHeaderIncluded = true; } var includeHiddenColumn = args.excelExportProperties && args.excelExportProperties.includeHiddenColumn ? args.excelExportProperties.includeHiddenColumn : true; var col = currentPivotInstance ? currentPivotInstance.grid.getColumns() : _this.parent.grid.getColumns(); var hiddenColumnsIndex = []; if (!includeHiddenColumn) { for (var column = 0; column < col.length; column++) { if (!col[column].visible) { hiddenColumnsIndex.push(column); } } } var columnCount = 0; var rowHeight = 0; for (var dataColl = 0; dataColl < dataCollections_1.length; dataColl++) { var pivotValues = dataCollections_1[dataColl]; var colLen = 0; var rowLen = pivotValues.length; var formatList = currentPivotInstance ? currentPivotInstance.renderModule.formatList : _this.parent.renderModule.formatList; var maxLevel = 0; for (var colCount = 0; colCount < pivotValues[0].length; colCount++) { if (pivotValues[0][colCount] !== null && pivotValues[0][colCount] !== undefined) { columnCount++; } } for (var rCnt = 0; rCnt < rowLen; rCnt++) { if (pivotValues[rCnt]) { _this.actualrCnt++; if (!includeHiddenColumn) { for (var _i = 0, hiddenColumnsIndex_1 = hiddenColumnsIndex; _i < hiddenColumnsIndex_1.length; _i++) { var colIndex = hiddenColumnsIndex_1[_i]; pivotValues[rCnt].splice(colIndex, 1); } } colLen = pivotValues[rCnt].length; var cells = []; for (var cCnt = 0; cCnt < colLen; cCnt++) { if (pivotValues[rCnt][cCnt]) { var pivotCell = pivotValues[rCnt][cCnt]; var field = (_this.parent.dataSourceSettings.valueAxis === 'row' && _this.parent.dataType === 'olap' && pivotCell.rowOrdinal && _this.engine.tupRowInfo[pivotCell.rowOrdinal]) ? _this.engine.tupRowInfo[pivotCell.rowOrdinal].measureName : pivotCell.actualText; var styles = (pivotCell.axis === 'row') ? { hAlign: 'Left', bold: true, wrapText: true } : { numberFormat: formatList[field], bold: false, wrapText: true }; var headerStyle = { bold: true, vAlign: 'Center', wrapText: true, indent: cCnt === 0 ? pivotCell.level * 10 : 0 }; if (!(pivotCell.level === -1 && !pivotCell.rowSpan)) { var aggMatrix = _this.engine.aggregatedValueMatrix; var cellValue = pivotCell.axis === 'value' ? ((aggMatrix[rCnt] && aggMatrix[rCnt][cCnt]) ? aggMatrix[rCnt][cCnt] : (pivotCell.formattedText === '#DIV/0!' ? pivotCell.formattedText : pivotCell.value)) : pivotCell.formattedText; var isgetValuesHeader = ((_this.parent.dataSourceSettings.rows.length === 0 && _this.parent.dataSourceSettings.valueAxis === 'row') || (_this.parent.dataSourceSettings.columns.length === 0 && _this.parent.dataSourceSettings.valueAxis === 'column')); if (pivotCell.type === 'grand sum' && !(_this.parent.dataSourceSettings.values.length === 1 && _this.parent.dataSourceSettings.valueAxis === 'row' && pivotCell.axis === 'column')) { cellValue = isgetValuesHeader ? _this.parent.getValuesHeader(pivotCell, 'grandTotal') : _this.parent.localeObj.getConstant('grandTotal'); } else if (pivotCell.type === 'sum') { cellValue = cellValue.toString().replace('Total', _this.parent.localeObj.getConstant('total')); } else { cellValue = (!isNullOrUndefined(pivotCell.valueSort) && (_this.parent.localeObj.getConstant('grandTotal') + _this.parent.dataSourceSettings.valueSortSettings.headerDelimiter + pivotCell.formattedText === pivotCell.valueSort.levelName) && isgetValuesHeader) ? _this.parent.getValuesHeader(pivotCell, 'value') : cellValue; } if (!(pivotCell.level === -1 && !pivotCell.rowSpan) && pivotCell.rowSpan !== 0) { cells.push({ index: cCnt + 1, value: cellValue, colSpan: pivotCell.colSpan, rowSpan: (pivotCell.rowSpan === -1 ? 1 : pivotCell.rowSpan) }); var lastCell = cells[cells.length - 1]; if (pivotCell.axis === 'value') { if (isNaN(pivotCell.value) || pivotCell.formattedText === '' || pivotCell.formattedText === undefined || isNullOrUndefined(pivotCell.value)) { lastCell.value = type === 'Excel' ? null : ''; } styles.numberFormat = typeof cellValue === 'string' ? undefined : styles.numberFormat; lastCell.style = styles; } else { lastCell.style = headerStyle; if (pivotCell.axis === 'row' && (_this.parent.isTabular ? cCnt < _this.parent.engineModule.rowMaxLevel + 1 : cCnt === 0)) { lastCell.style = styles; if (_this.parent.dataType === 'olap') { var indent = _this.parent.renderModule.indentCollection[rCnt]; lastCell.style.indent = indent * 2; maxLevel = maxLevel > indent ? maxLevel : indent; } else { var levelName = pivotCell.valueSort ? pivotCell.valueSort.levelName.toString() : ''; var delimiter = _this.parent.dataSourceSettings.valueSortSettings.headerDelimiter; var memberPos = pivotCell.actualText ? pivotCell.actualText.toString().split(delimiter).length : 0; var levelPosition = levelName.split(delimiter).length - (memberPos ? memberPos - 1 : memberPos); var level = levelPosition ? (levelPosition - 1) : 0; lastCell.style.indent = level * 2; maxLevel = level > maxLevel ? level : maxLevel; } } } if (pivotCell.style || lastCell.style.backColor || lastCell.style.fontColor || lastCell.style.fontName || lastCell.style.fontSize) { lastCell.style.backColor = lastCell.style.backColor ? lastCell.style.backColor : pivotCell.style.backgroundColor; lastCell.style.fontColor = lastCell.style.fontColor ? lastCell.style.fontColor : pivotCell.style.color; lastCell.style.fontName = lastCell.style.fontName ? lastCell.style.fontName : pivotCell.style.fontFamily; if (!isNullOrUndefined(lastCell.style.fontSize) || !isNullOrUndefined(pivotCell.style.fontSize)) { lastCell.style.fontSize = !isNullOrUndefined(lastCell.style.fontSize) ? Number(lastCell.style.fontSize) : Number(pivotCell.style.fontSize.split('px')[0]); } } lastCell.style.borders = { color: '#000000', lineStyle: 'thin' }; var excelHeaderQueryCellInfoArgs = void 0; var excelQueryCellInfoArgs = void 0; if (pivotCell.axis === 'column') { excelHeaderQueryCellInfoArgs = { style: !isNullOrUndefined(_this.theme) && !isNullOrUndefined(_this.theme.header) ? _this.excelExportHelper.getHeaderThemeStyle(_this.theme, headerStyle) : headerStyle, cell: pivotCell }; _this.parent.trigger(events.excelHeaderQueryCellInfo, excelHeaderQueryCellInfoArgs); } else { excelQueryCellInfoArgs = { style: !isNullOrUndefined(_this.theme) && !isNullOrUndefined(_this.theme.record) ? _this.excelExportHelper.getRecordThemeStyle(_this.theme, styles) : styles, cell: pivotCell, column: undefined, data: pivotValues, value: cellValue, colSpan: 1 }; _this.parent.trigger(events.excelQueryCellInfo, excelQueryCellInfoArgs); } lastCell.value = (pivotCell.axis === 'column') ? excelHeaderQueryCellInfoArgs.cell.formattedText : excelQueryCellInfoArgs.value; lastCell.style = (pivotCell.axis === 'column') ? excelHeaderQueryCellInfoArgs.style : excelQueryCellInfoArgs.style; if ((excelHeaderQueryCellInfoArgs && excelHeaderQueryCellInfoArgs.image) || (excelQueryCellInfoArgs && excelQueryCellInfoArgs.image)) { rowHeight = _this.excelExportHelper.setImage((pivotCell.axis === 'column') ? excelHeaderQueryCellInfoArgs : excelQueryCellInfoArgs, cCnt, _this.actualrCnt, rowHeight); } if (!isNullOrUndefined(excelHeaderQueryCellInfoArgs) && !isNullOrUndefined(excelHeaderQueryCellInfoArgs.hyperLink)) { lastCell.hyperlink = { target: excelHeaderQueryCellInfoArgs.hyperLink.target }; lastCell.value = excelHeaderQueryCellInfoArgs.hyperLink.displayText || lastCell.value; } else if (!isNullOrUndefined(excelQueryCellInfoArgs) && !isNullOrUndefined(excelQueryCellInfoArgs.hyperLink)) { lastCell.hyperlink = { target: excelQueryCellInfoArgs.hyperLink.target }; lastCell.value = excelQueryCellInfoArgs.hyperLink.displayText || lastCell.value; } if (pivotCell.axis === 'column') { lastCell.colSpan = excelHeaderQueryCellInfoArgs.cell.colSpan; lastCell.rowSpan = excelHeaderQueryCellInfoArgs.cell.rowSpan; } else { lastCell.colSpan = excelQueryCellInfoArgs.colSpan > 1 ? excelQueryCellInfoArgs.colSpan : excelQueryCellInfoArgs.cell.colSpan; lastCell.rowSpan = excelQueryCellInfoArgs.cell.rowSpan; } } } cCnt = cCnt + (pivotCell.colSpan ? (pivotCell.colSpan - 1) : 0); } else { var pivotCell = { formattedText: '', colSpan: 1, rowSpan: 1 }; if (rCnt === 0 && cCnt === 0) { if (!includeHiddenColumn) { pivotCell.colSpan = pivotValues[0].length - (columnCount - hiddenColumnsIndex.length); pivotCell.rowSpan = Object.keys(pivotValues).length - _this.engine.rowCount; } else { pivotCell.colSpan = pivotValues[0].length - _this.engine.columnCount; pivotCell.rowSpan = Object.keys(pivotValues).length - _this.engine.rowCount; } } var excelHeaderQueryCellInfoArgs = void 0; if (pivotCell) { excelHeaderQueryCellInfoArgs = { style: { borders: { color: '#000000', lineStyle: 'thin' } }, cell: pivotCell }; _this.parent.trigger(events.excelHeaderQueryCellInfo, excelHeaderQueryCellInfoArgs); } var cell = excelHeaderQueryCellInfoArgs.cell; cells.push({ index: cCnt + 1, colSpan: cell['colSpan'], rowSpan: cell['rowSpan'], value: pivotCell.formattedText, style: excelHeaderQueryCellInfoArgs.style }); if (excelHeaderQueryCellInfoArgs.image) { rowHeight = _this.excelExportHelper.setImage(excelHeaderQueryCellInfoArgs, cCnt, _this.actualrCnt, rowHeight); } if (!isNullOrUndefined(excelHeaderQueryCellInfoArgs) && !isNullOrUndefined(excelHeaderQueryCellInfoArgs.hyperLink)) { cell.hyperlink = { target: excelHeaderQueryCellInfoArgs.hyperLink.target }; cell.value = excelHeaderQueryCellInfoArgs.hyperLink.displayText || cell.value; } } } var row = { index: _this.actualrCnt, cells: cells }; if (rowHeight > 0) { row.height = rowHeight; rowHeight = 0; } _this.rows.push(row); } } if ((!isMultipleExport && expType === 'AppendToSheet') || expType === 'NewSheet') { if (isFooterDefined) { _this.addHeaderAndFooter(exportProperties.footer, '', 'footer', exportProperties.footer.footerRows); } else if (!isFooterDefined && footer_1 !== '' && isNullOrUndefined(args.excelExportProperties)) { _this.addHeaderAndFooter({}, footer_1, 'footer', undefined); } else if (!isNullOrUndefined(args.excelExportProperties) && !isNullOrUndefined(args.excelExportProperties.footer)) { _this.addHeaderAndFooter(args.excelExportProperties.footer, '', 'footer', args.excelExportProperties.footer.footerRows); } } if (_this.columns.length < col.length) { _this.columns = []; for (var cCnt = 0; cCnt < colLen; cCnt++) { var width = col[cCnt].width; if (typeof (width) === 'string' && width.indexOf('px') !== -1) { width = parseInt(width, 10); } _this.columns.push({ index: cCnt + 1, width: width }); } } if (maxLevel > 0) { _this.columns[0].width = 100 + (maxLevel * 20); } var sheet = { columns: _this.columns, rows: _this.rows, images: _this.images, enableRtl: _this.parent.enableRtl }; _this.workSheet.push(sheet); _this.book['worksheets'] = _this.workSheet; } var blobData = void 0; if (!isMultipleExport) { var book = new Workbook(_this.book, type === 'Excel' ? 'xlsx' : 'csv', undefined, _this.parent.currencyCode); var fileExtension = fileName_1.split('.').pop(); if (!isBlob) { book.save(fileExtension === 'xlsx' || fileExtension === 'csv' ? fileName_1 : (fileName_1 + (type === 'Excel' ? '.xlsx' : '.csv'))); } else { blobData = book.saveAsBlob(fileExtension === 'xlsx' || type === 'Excel' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'text/csv'); } var exportCompleteEventArgs = { type: type, promise: isBlob ? blobData : null }; _this.parent.trigger(events.exportComplete, exportCompleteEventArgs); _this.isHeaderIncluded = false; } resolve(_this.book); } catch (error) { reject(error); } }); }; ExcelExport.prototype.updateOlapPageSettings = function (isUpdate) { this.parent.olapEngineModule.isExporting = isUpdate ? true : false; if (!this.parent.exportSpecifiedPages) { this.parent.olapEngineModule.pageSettings = isUpdate ? null : this.parent.olapEngineModule.pageSettings; this.parent.olapEngineModule.isPaging = isUpdate ? false : true; } else { this.parent.olapEngineModule.exportSpeciedPages = this.parent.exportSpecifiedPages = isUpdate ? this.parent.exportSpecifiedPages : undefined; } }; /** * To destroy the excel export module * * @returns {void} * @hidden */ ExcelExport.prototype.destroy = function () { this.rows = []; this.actualrCnt = 0; if (this.engine) { this.engine = null; } if (this.rows) { this.rows = null; } }; return ExcelExport; }()); export { ExcelExport };