all files / pivotview/actions/ excel-export.js

93.18% Statements 164/176
74.13% Branches 149/201
100% Functions 9/9
93.18% Lines 164/176
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289   19× 19×   1809×                                           15× 15× 15× 15× 15× 15× 15× 15× 15× 15×               15×     15× 15× 15× 15× 15× 15× 15× 15× 15×   15×     15×   15× 15× 15× 15× 15× 15× 15× 15× 2586× 2583× 2583× 2583× 2583× 37592× 37562× 37562× 7808×   37562×         37562× 37562× 37562× 37562× 37562× 37562×   37562× 20×   37542×       37542×     37562× 37562×       37562× 37562× 34814×   8444×   34814× 34814×     2748× 2748× 2553× 2553× 2478× 2478× 2478×     75× 75×     75×     75× 75× 75×       37562×   904×   904×   904×   904× 904×     37562× 37562× 37562× 37562× 195×       195×     37367×             37367×   37562× 37562×     37562×     30× 30× 30× 30×       30×   30×           2583×     15×   14×     14×     15× 15× 145×   15×     15×   15× 14× 14× 14× 14×           14×       14×             19×          
define(["require", "exports", "@syncfusion/ej2-excel-export", "../../common/base/constant", "@syncfusion/ej2-base", "../../base/export-util"], function (require, exports, ej2_excel_export_1, events, ej2_base_1, export_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ExcelExport = (function () {
        function ExcelExport(parent) {
            this.actualrCnt = 0;
            this.parent = parent;
        }
        ExcelExport.prototype.getModuleName = function () {
            return 'excelExport';
        };
        ExcelExport.prototype.addHeaderAndFooter = function (excelExportProperties, stringValue, type, rowCount) {
            var cells = [];
            Eif (!ej2_base_1.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++) {
                        cells.push({
                            index: spanCount + 1, value: currentRow.cells[j].value,
                            colSpan: currentRow.cells[j].colSpan, rowSpan: currentRow.cells[j].rowSpan,
                            style: currentRow.cells[j].style
                        });
                        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;
                }
            }
        };
        ExcelExport.prototype.exportToExcel = function (type, exportProperties, isBlob) {
            this.rows = [];
            this.actualrCnt = 0;
            var isHeaderSet = !ej2_base_1.isNullOrUndefined(exportProperties) && !ej2_base_1.isNullOrUndefined(exportProperties.header);
            var isFooterSet = !ej2_base_1.isNullOrUndefined(exportProperties) && !ej2_base_1.isNullOrUndefined(exportProperties.footer);
            var isFileNameSet = !ej2_base_1.isNullOrUndefined(exportProperties) && !ej2_base_1.isNullOrUndefined(exportProperties.fileName);
            this.engine = this.parent.dataType === 'olap' ? this.parent.olapEngineModule : this.parent.engineModule;
            var clonedValues;
            var currentPivotValues = export_util_1.PivotExportUtil.getClonedPivotValues(this.engine.pivotValues);
            var customFileName = isFileNameSet ? 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;
                }
                this.engine.generateGridData(this.parent.dataSourceSettings, true, true);
                this.parent.applyFormatting(this.engine.pivotValues);
                clonedValues = export_util_1.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;
            var header;
            var footer;
            var dataCollections;
            this.parent.trigger(events.beforeExport, args, function (observedArgs) {
                fileName = observedArgs.fileName;
                header = observedArgs.header;
                footer = observedArgs.footer;
                dataCollections = observedArgs.dataCollections;
            });
            Iif (!isHeaderSet && ej2_base_1.isNullOrUndefined(args.excelExportProperties) && header !== '') {
                this.addHeaderAndFooter({}, header, 'header', undefined);
            }
            else if (!ej2_base_1.isNullOrUndefined(args.excelExportProperties) && !ej2_base_1.isNullOrUndefined(args.excelExportProperties.header)) {
                this.addHeaderAndFooter(args.excelExportProperties.header, '', 'header', args.excelExportProperties.header.headerRows);
            }
            var workSheets = [];
            for (var dataColl = 0; dataColl < dataCollections.length; dataColl++) {
                var pivotValues = dataCollections[dataColl];
                var colLen = 0;
                var rowLen = pivotValues.length;
                var formatList = this.parent.renderModule.getFormatList();
                var maxLevel = 0;
                for (var rCnt = 0; rCnt < rowLen; rCnt++) {
                    if (pivotValues[rCnt]) {
                        this.actualrCnt++;
                        colLen = pivotValues[rCnt].length;
                        var cells = [];
                        for (var cCnt = 0; cCnt < colLen; cCnt++) {
                            if (pivotValues[rCnt][cCnt]) {
                                var pivotCell = pivotValues[rCnt][cCnt];
                                if (pivotCell && pivotCell.axis === 'value' && pivotCell.formattedText === '') {
                                    pivotCell.value = Number(pivotCell.formattedText);
                                }
                                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 };
                                Eif (!(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 Iif (pivotCell.type === 'sum') {
                                        cellValue = cellValue.toString().replace('Total', this.parent.localeObj.getConstant('total'));
                                    }
                                    else {
                                        cellValue = (!ej2_base_1.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;
                                    }
                                    Eif (!(pivotCell.level === -1 && !pivotCell.rowSpan)) {
                                        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 || ej2_base_1.isNullOrUndefined(pivotCell.value)) {
                                                lastCell.value = type === 'Excel' ? null : '';
                                            }
                                            styles.numberFormat = typeof cellValue === 'string' ? undefined : styles.numberFormat;
                                            lastCell.style = !ej2_base_1.isNullOrUndefined(lastCell.value) ? styles : { bold: false, wrapText: true };
                                        }
                                        else {
                                            lastCell.style = headerStyle;
                                            if (pivotCell.axis === 'row' && 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 memberPos = pivotCell.actualText ?
                                                        pivotCell.actualText.toString().
                                                            split(this.parent.dataSourceSettings.valueSortSettings.headerDelimiter).length : 0;
                                                    var levelPosition = levelName.
                                                        split(this.parent.dataSourceSettings.valueSortSettings.headerDelimiter).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;
                                            Eif (!ej2_base_1.isNullOrUndefined(lastCell.style.fontSize) || !ej2_base_1.isNullOrUndefined(pivotCell.style.fontSize)) {
                                                lastCell.style.fontSize = !ej2_base_1.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: headerStyle,
                                                cell: pivotCell
                                            };
                                            this.parent.trigger(events.excelHeaderQueryCellInfo, excelHeaderQueryCellInfoArgs);
                                        }
                                        else {
                                            excelQueryCellInfoArgs = {
                                                style: styles,
                                                cell: pivotCell,
                                                column: undefined,
                                                data: pivotValues,
                                                value: cellValue
                                            };
                                            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;
                                    }
                                }
                                cCnt = cCnt + (pivotCell.colSpan ? (pivotCell.colSpan - 1) : 0);
                            }
                            else {
                                var pivotCell = { formattedText: '' };
                                var excelHeaderQueryCellInfoArgs = void 0;
                                Eif (pivotCell) {
                                    excelHeaderQueryCellInfoArgs = {
                                        style: undefined,
                                        cell: pivotCell
                                    };
                                    this.parent.trigger(events.excelHeaderQueryCellInfo, excelHeaderQueryCellInfoArgs);
                                }
                                cells.push({
                                    index: cCnt + 1, colSpan: 1, rowSpan: 1, value: pivotCell.formattedText,
                                    style: excelHeaderQueryCellInfoArgs.style
                                });
                            }
                        }
                        this.rows.push({ index: this.actualrCnt, cells: cells });
                    }
                }
                if (isFooterSet) {
                    this.addHeaderAndFooter(exportProperties.footer, '', 'footer', exportProperties.footer.footerRows);
                }
                else Iif (!isFooterSet && footer !== '' && ej2_base_1.isNullOrUndefined(args.excelExportProperties)) {
                    this.addHeaderAndFooter({}, footer, 'footer', undefined);
                }
                else Iif (!ej2_base_1.isNullOrUndefined(args.excelExportProperties) && !ej2_base_1.isNullOrUndefined(args.excelExportProperties.footer)) {
                    this.addHeaderAndFooter(args.excelExportProperties.footer, '', 'footer', args.excelExportProperties.footer.footerRows);
                }
                var columns = [];
                for (var cCnt = 0; cCnt < colLen; cCnt++) {
                    columns.push({ index: cCnt + 1, width: 100 });
                }
                Iif (maxLevel > 0) {
                    columns[0].width = 100 + (maxLevel * 20);
                }
                workSheets.push({ columns: columns, rows: this.rows });
            }
            var book = new ej2_excel_export_1.Workbook({ worksheets: workSheets }, type === 'Excel' ? 'xlsx' : 'csv', undefined, this.parent.currencyCode);
            var fileExtension = fileName.split('.').pop();
            var blobData;
            Eif (!isBlob) {
                book.save(fileExtension === 'xlsx' || fileExtension === 'csv' ? fileName : (fileName + (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);
        };
        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;
            }
        };
        ExcelExport.prototype.destroy = function () {
            if (this.engine) {
                this.engine = null;
            }
        };
        return ExcelExport;
    }());
    exports.ExcelExport = ExcelExport;
});