UNPKG

@syncfusion/ej2-excel-export

Version:
1,335 lines (1,323 loc) 117 kB
import { ZipArchiveItem, ZipArchive } from '@syncfusion/ej2-compression'; import { isNullOrUndefined, Internationalization } from '@syncfusion/ej2-base'; import { Encoding } from '@syncfusion/ej2-file-utils'; /** * CellStyle class * @private */ /* eslint-disable */ var CellStyle = /** @__PURE__ @class */ (function () { function CellStyle() { this.numFmtId = 0; this.backColor = 'none'; this.fontName = 'Calibri'; this.fontSize = 10.5; this.fontColor = '#000000'; this.italic = false; this.bold = false; this.underline = false; this.strikeThrough = false; this.wrapText = false; this.hAlign = 'general'; this.vAlign = 'bottom'; this.indent = 0; this.rotation = 0; this.numberFormat = 'GENERAL'; this.type = 'datetime'; this.borders = new Borders(); this.isGlobalStyle = false; } return CellStyle; }()); /** * Font Class * @private */ var Font = /** @__PURE__ @class */ (function () { function Font() { this.sz = 10.5; this.name = 'Calibri'; this.u = false; this.b = false; this.i = false; this.color = 'FF000000'; this.strike = false; } return Font; }()); /** * CellXfs class * @private */ var CellXfs = /** @__PURE__ @class */ (function () { function CellXfs() { } return CellXfs; }()); /** * Alignment class * @private */ var Alignment = /** @__PURE__ @class */ (function () { function Alignment() { } return Alignment; }()); /** * CellStyleXfs class * @private */ var CellStyleXfs = /** @__PURE__ @class */ (function () { function CellStyleXfs() { } return CellStyleXfs; }()); /** * CellStyles class * @private */ var CellStyles = /** @__PURE__ @class */ (function () { function CellStyles() { this.name = 'Normal'; this.xfId = 0; } return CellStyles; }()); /** * NumFmt class * @private */ var NumFmt = /** @__PURE__ @class */ (function () { function NumFmt(id, code) { this.numFmtId = id; this.formatCode = code; } return NumFmt; }()); /** * Border class * @private */ var Border = /** @__PURE__ @class */ (function () { function Border(mLine, mColor) { this.lineStyle = mLine; this.color = mColor; } return Border; }()); /** * Borders class * @private */ var Borders = /** @__PURE__ @class */ (function () { function Borders() { this.left = new Border('none', '#FFFFFF'); this.right = new Border('none', '#FFFFFF'); this.top = new Border('none', '#FFFFFF'); this.bottom = new Border('none', '#FFFFFF'); this.all = new Border('none', '#FFFFFF'); } return Borders; }()); var __extends = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /** * Worksheet class * @private */ /* eslint-disable */ var Cell = /** @__PURE__ @class */ (function () { function Cell() { } return Cell; }()); /** * Cells class * @private */ var Cells = /** @__PURE__ @class */ (function (_super) { __extends(Cells, _super); function Cells() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.add = function (cell) { var inserted = false; var count = 0; for (var _i = 0, _a = _this; _i < _a.length; _i++) { var c = _a[_i]; if (c.index === cell.index) { _this[count] = cell; inserted = true; } count++; } if (!inserted) { _this.push(cell); } }; return _this; } return Cells; }(Array)); /** * Column class * @private */ /* eslint-disable */ var Column = /** @__PURE__ @class */ (function () { function Column() { } return Column; }()); var __extends$1 = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /** * Row class * @private */ /* eslint-disable */ var Row = /** @__PURE__ @class */ (function () { function Row() { } return Row; }()); /** * Rows class * @private */ var Rows = /** @__PURE__ @class */ (function (_super) { __extends$1(Rows, _super); function Rows() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.add = function (row) { _this.push(row); }; return _this; } return Rows; }(Array)); var __extends$2 = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /** * Worksheets class * @private */ /* eslint-disable */ var Worksheets = /** @__PURE__ @class */ (function (_super) { __extends$2(Worksheets, _super); function Worksheets() { return _super !== null && _super.apply(this, arguments) || this; } return Worksheets; }(Array)); var __extends$3 = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); /** * Worksheet class * @private */ /* eslint-disable */ var Worksheet = /** @__PURE__ @class */ (function () { function Worksheet() { this.isSummaryRowBelow = true; this.showGridLines = true; this.enableRtl = false; } return Worksheet; }()); /** * Hyperlink class * @private */ var HyperLink = /** @__PURE__ @class */ (function () { function HyperLink() { } return HyperLink; }()); /** * Grouping class * @private */ var Grouping = /** @__PURE__ @class */ (function () { function Grouping() { } return Grouping; }()); /** * FreezePane class * @private */ var FreezePane = /** @__PURE__ @class */ (function () { function FreezePane() { } return FreezePane; }()); /** * MergeCell * @private */ var MergeCell = /** @__PURE__ @class */ (function () { function MergeCell() { } return MergeCell; }()); /** * MergeCells class * @private */ var MergeCells = /** @__PURE__ @class */ (function (_super) { __extends$3(MergeCells, _super); function MergeCells() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.add = function (mergeCell) { var inserted = false; var count = 0; for (var _i = 0, _a = _this; _i < _a.length; _i++) { var mCell = _a[_i]; if (MergeCells.isIntersecting(mCell, mergeCell)) { var intersectingCell = new MergeCell(); intersectingCell.x = Math.min(mCell.x, mergeCell.x); intersectingCell.y = Math.min(mCell.Y, mergeCell.y); intersectingCell.width = Math.max(mCell.Width + mCell.X, mergeCell.width + mergeCell.x); intersectingCell.height = Math.max(mCell.Height + mCell.Y, mergeCell.height + mergeCell.y); intersectingCell.ref = (_this[count].ref.split(':')[0]) + ':' + (mergeCell.ref.split(':')[1]); _this[count] = intersectingCell; mergeCell = intersectingCell; inserted = true; } count++; } if (!inserted) { _this.push(mergeCell); } return mergeCell; }; return _this; } MergeCells.isIntersecting = function (base, compare) { return (base.x <= compare.x + compare.width) && (compare.x <= base.x + base.width) && (base.y <= compare.y + compare.height) && (compare.y <= base.y + base.height); }; return MergeCells; }(Array)); /** * Image class * @private */ /* eslint-disable */ var Image = /** @__PURE__ @class */ (function () { function Image() { } return Image; }()); // import { IValueFormatter } from '../base/interface'; /** * ValueFormatter class to globalize the value. * @private */ /* eslint-disable */ var ValueFormatter = /** @__PURE__ @class */ (function () { function ValueFormatter(cultureName) { this.intl = new Internationalization(); // if (!isNullOrUndefined(cultureName)) { // this.intl.culture = cultureName; // } } ValueFormatter.prototype.getFormatFunction = function (format, isServerRendered) { if (format.type) { if (isServerRendered) { format.isServerRendered = true; } return this.intl.getDateFormat(format); } else { return this.intl.getNumberFormat(format); } }; // public getParserFunction(format: NumberFormatOptions | DateFormatOptions): Function { // if ((<DateFormatOptions>format).type) { // return this.intl.getDateParser(<DateFormatOptions>format); // } else { // return this.intl.getNumberParser(<DateFormatOptions>format); // } // } // public fromView(value: string, format: Function, type?: string): string | number | Date { // if (type === 'date' || type === 'datetime' || type === 'number') { // return format(value); // } else { // return value; // } // } ValueFormatter.prototype.toView = function (value, format) { var result = value; if (!isNullOrUndefined(format) && !isNullOrUndefined(value)) { result = format(value); } return result; }; // public setCulture(cultureName: string): void { // if (!isNullOrUndefined(cultureName)) { // setCulture(cultureName); // } // } /* tslint:disable:no-any */ ValueFormatter.prototype.displayText = function (value, format, isServerRendered) { return this.toView(value, this.getFormatFunction(format, isServerRendered)); }; return ValueFormatter; }()); /** * CsvHelper class * @private */ /* eslint-disable */ var CsvHelper = /** @__PURE__ @class */ (function () { /* tslint:disable:no-any */ function CsvHelper(json, separator) { this.csvStr = ''; if (separator === null || separator === undefined) { this.separator = ','; } else { this.separator = separator; } this.formatter = new ValueFormatter(); this.isMicrosoftBrowser = !(!navigator.msSaveBlob); if (json.isServerRendered !== null && json.isServerRendered !== undefined) { this.isServerRendered = json.isServerRendered; } if (json.styles !== null && json.styles !== undefined) { this.globalStyles = new Map(); for (var i = 0; i < json.styles.length; i++) { if (json.styles[i].name !== undefined && json.styles[i].numberFormat !== undefined) { this.globalStyles.set(json.styles[i].name, json.styles[i].numberFormat); } } } // Parses Worksheets data to DOM. if (json.worksheets !== null && json.worksheets !== undefined) { this.parseWorksheet(json.worksheets[0]); } //this.csvStr = 'a1,a2,a3\nb1,b2,b3'; } CsvHelper.prototype.parseWorksheet = function (json) { //Rows if (json.rows !== null && json.rows !== undefined) { this.parseRows(json.rows); } }; /* tslint:disable:no-any */ CsvHelper.prototype.parseRows = function (rows) { var count = 1; for (var _i = 0, rows_1 = rows; _i < rows_1.length; _i++) { var row = rows_1[_i]; //Row index if (row.index !== null && row.index !== undefined) { while (count < row.index) { this.csvStr += '\r\n'; count++; } this.parseRow(row); } else { throw Error('Row index is missing.'); } } this.csvStr += '\r\n'; }; /* tslint:disable:no-any */ CsvHelper.prototype.parseRow = function (row) { if (row.cells !== null && row.cells !== undefined) { var count = 1; for (var _i = 0, _a = row.cells; _i < _a.length; _i++) { var cell = _a[_i]; //cell index if (cell.index !== null && cell.index !== undefined) { while (count < cell.index) { this.csvStr += this.separator; count++; } this.parseCell(cell); } else { throw Error('Cell index is missing.'); } } } }; /* tslint:disable:no-any */ CsvHelper.prototype.parseCell = function (cell) { var csv = this.csvStr; if (cell.value !== undefined) { if (cell.value instanceof Date) { if (cell.style !== undefined && cell.style.numberFormat !== undefined) { /* tslint:disable-next-line:max-line-length */ try { csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', skeleton: cell.style.numberFormat }, this.isServerRendered)); } catch (error) { /* tslint:disable-next-line:max-line-length */ csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', format: cell.style.numberFormat }, this.isServerRendered)); } } else if (cell.style !== undefined && cell.style.name !== undefined && this.globalStyles.has(cell.style.name)) { /* tslint:disable-next-line:max-line-length */ try { csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', skeleton: this.globalStyles.get(cell.style.name) }, this.isServerRendered)); } catch (error) { /* tslint:disable-next-line:max-line-length */ csv += this.parseCellValue(this.formatter.displayText(cell.value, { type: 'dateTime', format: this.globalStyles.get(cell.style.name) }, this.isServerRendered)); } } else { csv += cell.value; } } else if (typeof (cell.value) === 'boolean') { csv += cell.value ? 'TRUE' : 'FALSE'; } else if (typeof (cell.value) === 'number') { if (cell.style !== undefined && cell.style.numberFormat !== undefined) { /* tslint:disable-next-line:max-line-length */ csv += this.parseCellValue(this.formatter.displayText(cell.value, { format: cell.style.numberFormat, ignoreCurrency: true }, this.isServerRendered)); } else if (cell.style !== undefined && cell.style.name !== undefined && this.globalStyles.has(cell.style.name)) { /* tslint:disable-next-line:max-line-length */ csv += this.parseCellValue(this.formatter.displayText(cell.value, { format: this.globalStyles.get(cell.style.name), ignoreCurrency: true }, this.isServerRendered)); } else { csv += cell.value; } } else { csv += this.parseCellValue(cell.value); } } this.csvStr = csv; }; CsvHelper.prototype.parseCellValue = function (value) { var val = ''; var length = value.length; for (var start = 0; start < length; start++) { if (value[start] === '\"') { val += value[start].replace('\"', '\"\"'); } else { val += value[start]; } } value = val; if (value.indexOf(this.separator) !== -1 || value.indexOf('\n') !== -1 || value.indexOf('\"') !== -1) { return value = '\"' + value + '\"'; } else { return value; } }; /** * Saves the file with specified name and sends the file to client browser * @param {string} fileName- file name to save. * @param {Blob} buffer- the content to write in file */ CsvHelper.prototype.save = function (fileName) { this.buffer = new Blob(['\ufeff' + this.csvStr], { type: 'text/csv;charset=UTF-8' }); if (this.isMicrosoftBrowser) { navigator.msSaveBlob(this.buffer, fileName); } else { var dataUrl_1 = window.URL.createObjectURL(this.buffer); var dwlLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); dwlLink.download = fileName; dwlLink.href = dataUrl_1; var event_1 = document.createEvent('MouseEvent'); event_1.initEvent('click', true, true); dwlLink.dispatchEvent(event_1); setTimeout(function () { window.URL.revokeObjectURL(dataUrl_1); }); } }; /** * Returns a Blob object containing CSV data with optional encoding. * @param {string} [encodingType] - The supported encoding types are "ansi", "unicode" and "utf8". */ /* tslint:disable:no-any */ CsvHelper.prototype.saveAsBlob = function (encodingType) { if (encodingType != undefined) { var encoding = new Encoding(); var encodeString = 'UTF-8'; if (encodingType.toUpperCase() == "ANSI") { encoding.type = 'Ansi'; encodeString = 'ANSI'; } else if (encodingType.toUpperCase() == "UNICODE") { encoding.type = 'Unicode'; encodeString = 'UNICODE'; } else { encoding.type = 'Utf8'; encodeString = 'UTF-8'; } var buffer = encoding.getBytes(this.csvStr, 0, this.csvStr.length); return new Blob([buffer], { type: 'text/csv;charset=' + encodeString }); } else return new Blob(['\ufeff' + this.csvStr], { type: 'text/csv;charset=UTF-8' }); }; return CsvHelper; }()); /** * BlobHelper class * @private */ /* eslint-disable */ var BlobHelper = /** @__PURE__ @class */ (function () { function BlobHelper() { /* tslint:disable:no-any */ this.parts = []; } /* tslint:disable:no-any */ BlobHelper.prototype.append = function (part) { this.parts.push(part); this.blob = undefined; // Invalidate the blob }; BlobHelper.prototype.getBlob = function () { return new Blob(this.parts, { type: 'text/plain' }); }; return BlobHelper; }()); /** * AutoFilters class * @private */ /* eslint-disable */ var AutoFilters = /** @__PURE__ @class */ (function () { function AutoFilters() { } return AutoFilters; }()); /** * Workbook class */ /* eslint-disable */ var Workbook = /** @__PURE__ @class */ (function () { /* tslint:disable:no-any */ function Workbook(json, saveType, culture, currencyString, separator) { this.sharedStringCount = 0; this.unitsProportions = [ 96 / 75.0, 96 / 300.0, 96, 96 / 25.4, 96 / 2.54, 1, 96 / 72.0, 96 / 72.0 / 12700, ]; /* tslint:disable:no-any */ this.hyperlinkStyle = { fontColor: '#0000FF', underline: true }; if (culture !== undefined) { this.culture = culture; } else { this.culture = 'en-US'; } if (currencyString !== undefined) { this.currency = currencyString; } else { this.currency = 'USD'; } this.intl = new Internationalization(this.culture); this.mSaveType = saveType; if (saveType === 'xlsx') { this.mArchive = new ZipArchive(); this.sharedString = []; this.mFonts = []; this.mBorders = []; this.mStyles = []; this.printTitles = new Map(); this.cellStyles = new Map(); this.mNumFmt = new Map(); this.mFills = new Map(); this.mStyles.push(new CellStyle()); this.mFonts.push(new Font()); /* tslint:disable */ this.cellStyles.set('Normal', new CellStyles()); /* tslint:enable */ this.mCellXfs = []; this.mCellStyleXfs = []; this.drawingCount = 0; this.imageCount = 0; if (json.styles !== null && json.styles !== undefined) { /* tslint:disable-next-line:no-any */ this.globalStyles = new Map(); for (var i = 0; i < json.styles.length; i++) { if (json.styles[i].name !== undefined) { if (!this.cellStyles.has(json.styles[i].name)) { var cellStyle = new CellStyle(); cellStyle.isGlobalStyle = true; this.parserCellStyle(json.styles[i], cellStyle, 'none'); var cellStylesIn = new CellStyles(); cellStylesIn.name = cellStyle.name; cellStylesIn.xfId = (cellStyle.index - 1); this.cellStyles.set(cellStylesIn.name, cellStylesIn); /* tslint:disable-next-line:no-any */ var tFormat = {}; if (json.styles[i].numberFormat !== undefined) { tFormat.format = json.styles[i].numberFormat; } if (json.styles[i].type !== undefined) { tFormat.type = json.styles[i].type; } else { tFormat.type = 'datetime'; } if (tFormat.format !== undefined) { this.globalStyles.set(json.styles[i].name, tFormat); } } else { throw Error('Style name ' + json.styles[i].name + ' is already existed'); } } } } // Parses Worksheets data to DOM. if (json.worksheets !== null && json.worksheets !== undefined) { this.parserWorksheets(json.worksheets); } else { throw Error('Worksheet is expected.'); } // Parses the BuiltInProperties data to DOM. if (json.builtInProperties !== null && json.builtInProperties !== undefined) { this.builtInProperties = new BuiltInProperties(); this.parserBuiltInProperties(json.builtInProperties, this.builtInProperties); } } else { this.csvHelper = new CsvHelper(json, separator); } } /* tslint:disable:no-any */ Workbook.prototype.parserBuiltInProperties = function (jsonBuiltInProperties, builtInProperties) { //Author if (jsonBuiltInProperties.author !== null && jsonBuiltInProperties.author !== undefined) { builtInProperties.author = jsonBuiltInProperties.author; } //Comments if (jsonBuiltInProperties.comments !== null && jsonBuiltInProperties.comments !== undefined) { builtInProperties.comments = jsonBuiltInProperties.comments; } //Category if (jsonBuiltInProperties.category !== null && jsonBuiltInProperties.category !== undefined) { builtInProperties.category = jsonBuiltInProperties.category; } //Company if (jsonBuiltInProperties.company !== null && jsonBuiltInProperties.company !== undefined) { builtInProperties.company = jsonBuiltInProperties.company; } //Manager if (jsonBuiltInProperties.manager !== null && jsonBuiltInProperties.manager !== undefined) { builtInProperties.manager = jsonBuiltInProperties.manager; } //Subject if (jsonBuiltInProperties.subject !== null && jsonBuiltInProperties.subject !== undefined) { builtInProperties.subject = jsonBuiltInProperties.subject; } //Title if (jsonBuiltInProperties.title !== null && jsonBuiltInProperties.title !== undefined) { builtInProperties.title = jsonBuiltInProperties.title; } //Creation date if (jsonBuiltInProperties.createdDate !== null && jsonBuiltInProperties.createdDate !== undefined) { builtInProperties.createdDate = jsonBuiltInProperties.createdDate; } //Modified date if (jsonBuiltInProperties.modifiedDate !== null && jsonBuiltInProperties.modifiedDate !== undefined) { builtInProperties.modifiedDate = jsonBuiltInProperties.modifiedDate; } //Tags if (jsonBuiltInProperties.tags !== null && jsonBuiltInProperties.tags !== undefined) { builtInProperties.tags = jsonBuiltInProperties.tags; } //Status if (jsonBuiltInProperties.status !== null && jsonBuiltInProperties.status !== undefined) { builtInProperties.status = jsonBuiltInProperties.status; } }; /* tslint:disable:no-any */ Workbook.prototype.parserWorksheets = function (json) { this.worksheets = new Worksheets(); var length = json.length; for (var i = 0; i < length; i++) { var jsonSheet = json[i]; var sheet = new Worksheet(); this.mergeCells = new MergeCells(); this.mergedCellsStyle = new Map(); this.mHyperLinks = []; //Name if (jsonSheet.name !== null && jsonSheet.name !== undefined) { sheet.name = jsonSheet.name; } else { sheet.name = 'Sheet' + (i + 1).toString(); } if (jsonSheet.enableRtl !== null && jsonSheet.enableRtl !== undefined) { sheet.enableRtl = jsonSheet.enableRtl; } sheet.index = (i + 1); //Columns if (jsonSheet.columns !== null && jsonSheet.columns !== undefined) { this.parserColumns(jsonSheet.columns, sheet); } //Rows if (jsonSheet.rows !== null && jsonSheet.rows !== undefined) { this.parserRows(jsonSheet.rows, sheet); } //showGridLines if (jsonSheet.showGridLines !== null && jsonSheet.showGridLines !== undefined) { sheet.showGridLines = jsonSheet.showGridLines; } //FreezePanes if (jsonSheet.freeze !== null && jsonSheet.freeze !== undefined) { this.parserFreezePanes(jsonSheet.freeze, sheet); } //Print Title if (jsonSheet.printTitle !== null && jsonSheet.printTitle !== undefined) { this.parserPrintTitle(jsonSheet.printTitle, sheet); } if (jsonSheet.pageSetup !== undefined) { if (jsonSheet.pageSetup.isSummaryRowBelow !== undefined) { sheet.isSummaryRowBelow = jsonSheet.pageSetup.isSummaryRowBelow; } } if (jsonSheet.images !== undefined) { this.parserImages(jsonSheet.images, sheet); } if (jsonSheet.autoFilters !== null && jsonSheet.autoFilters !== undefined) { this.parseFilters(jsonSheet.autoFilters, sheet); } sheet.index = (i + 1); sheet.mergeCells = this.mergeCells; sheet.hyperLinks = this.mHyperLinks; this.worksheets.push(sheet); } }; /* tslint:disable:no-any */ Workbook.prototype.mergeOptions = function (fromJson, toJson) { /* tslint:disable:no-any */ var result = {}; this.applyProperties(fromJson, result); this.applyProperties(toJson, result); return result; }; /* tslint:disable:no-any */ Workbook.prototype.applyProperties = function (sourceJson, destJson) { var keys = Object.keys(sourceJson); for (var index = 0; index < keys.length; index++) { if (keys[index] !== 'name') { destJson[keys[index]] = sourceJson[keys[index]]; } } }; Workbook.prototype.getCellName = function (row, column) { return this.getColumnName(column) + row.toString(); }; Workbook.prototype.getColumnName = function (col) { col--; var strColumnName = ''; do { var iCurrentDigit = col % 26; col = col / 26 - 1; strColumnName = String.fromCharCode(65 + iCurrentDigit) + strColumnName; } while (col >= 0); return strColumnName; }; /* tslint:disable:no-any */ Workbook.prototype.parserPrintTitle = function (json, sheet) { var printTitleName = ''; var titleRowName; if (json.fromRow !== null && json.fromRow !== undefined) { var fromRow = json.fromRow; var toRow = void 0; if (json.toRow !== null && json.toRow !== undefined) { toRow = json.toRow; } else { toRow = json.fromRow; } titleRowName = '$' + fromRow + ':$' + toRow; } var titleColName; if (json.fromColumn !== null && json.fromColumn !== undefined) { var fromColumn = json.fromColumn; var toColumn = void 0; if (json.toColumn !== null && json.toColumn !== undefined) { toColumn = json.toColumn; } else { toColumn = json.fromColumn; } titleColName = '$' + this.getColumnName(fromColumn) + ':$' + this.getColumnName(toColumn); } if (titleRowName !== undefined) { printTitleName += (sheet.name + '!' + titleRowName); } if (titleColName !== undefined && titleRowName !== undefined) { printTitleName += ',' + (sheet.name + '!' + titleColName); } else if (titleColName !== undefined) { printTitleName += (sheet.name + '!' + titleColName); } if (printTitleName !== '') { this.printTitles.set(sheet.index - 1, printTitleName); } }; /* tslint:disable:no-any */ Workbook.prototype.parserFreezePanes = function (json, sheet) { sheet.freezePanes = new FreezePane(); if (json.row !== null && json.row !== undefined) { sheet.freezePanes.row = json.row; } else { sheet.freezePanes.row = 0; } if (json.column !== null && json.column !== undefined) { sheet.freezePanes.column = json.column; } else { sheet.freezePanes.column = 0; } sheet.freezePanes.leftCell = this.getCellName(sheet.freezePanes.row + 1, sheet.freezePanes.column + 1); }; /* tslint:disable:no-any */ Workbook.prototype.parserColumns = function (json, sheet) { var columnsLength = json.length; sheet.columns = []; for (var column = 0; column < columnsLength; column++) { var col = new Column(); if (json[column].index !== null && json[column].index !== undefined) { col.index = json[column].index; } else { throw Error('Column index is missing.'); } if (json[column].width !== null && json[column].width !== undefined) { col.width = json[column].width; } sheet.columns.push(col); } }; /* tslint:disable:no-any */ Workbook.prototype.parserRows = function (json, sheet) { var rowsLength = json.length; sheet.rows = new Rows(); var rowId = 0; for (var r = 0; r < rowsLength; r++) { var row = this.parserRow(json[r], rowId); rowId = row.index; sheet.rows.add(row); } this.insertMergedCellsStyle(sheet); }; Workbook.prototype.insertMergedCellsStyle = function (sheet) { var _this = this; if (this.mergeCells.length > 0) { this.mergedCellsStyle.forEach(function (value, key) { var row = sheet.rows.filter(function (item) { return item.index === value.y; })[0]; if (!isNullOrUndefined(row)) { var cell = row.cells.filter(function (item) { return item.index === value.x; })[0]; if (!isNullOrUndefined(cell)) { cell.styleIndex = value.styleIndex; } else { var cells = row.cells.filter(function (item) { return item.index <= value.x; }); var insertIndex = 0; if (cells.length > 0) { insertIndex = row.cells.indexOf(cells[cells.length - 1]) + 1; } row.cells.splice(insertIndex, 0, _this.createCell(value, key)); } } else { var rows = sheet.rows.filter(function (item) { return item.index <= value.y; }); var rowToInsert = new Row(); rowToInsert.index = value.y; rowToInsert.cells = new Cells(); rowToInsert.cells.add(_this.createCell(value, key)); var insertIndex = 0; if (rows.length > 0) { insertIndex = sheet.rows.indexOf(rows[rows.length - 1]) + 1; } sheet.rows.splice(insertIndex, 0, rowToInsert); } }); } }; Workbook.prototype.createCell = function (value, key) { var cellToInsert = new Cell(); cellToInsert.refName = key; cellToInsert.index = value.x; cellToInsert.cellStyle = new CellStyle(); cellToInsert.styleIndex = value.styleIndex; return cellToInsert; }; /* tslint:disable:no-any */ Workbook.prototype.parserRow = function (json, rowIndex) { var row = new Row(); //Row Height if (json.height !== null && json.height !== undefined) { row.height = json.height; } //Row index if (json.index !== null && json.index !== undefined) { row.index = json.index; } else { throw Error('Row index is missing.'); } if (json.grouping !== null && json.grouping !== undefined) { this.parseGrouping(json.grouping, row); } this.parseCells(json.cells, row); return row; }; /* tslint:disable:no-any */ Workbook.prototype.parseGrouping = function (json, row) { row.grouping = new Grouping(); if (json.outlineLevel !== undefined) { row.grouping.outlineLevel = json.outlineLevel; } if (json.isCollapsed !== undefined) { row.grouping.isCollapsed = json.isCollapsed; } if (json.isHidden !== undefined) { row.grouping.isHidden = json.isHidden; } }; /* tslint:disable:no-any */ Workbook.prototype.parseCells = function (json, row) { row.cells = new Cells(); var cellsLength = json !== undefined ? json.length : 0; var spanMin = 1; var spanMax = 1; var curCellIndex = 0; for (var cellId = 0; cellId < cellsLength; cellId++) { /* tslint:disable:no-any */ var jsonCell = json[cellId]; var cell = new Cell(); //cell index if (jsonCell.index !== null && jsonCell.index !== undefined) { cell.index = jsonCell.index; } else { throw Error('Cell index is missing.'); } if (cell.index < spanMin) { spanMin = cell.index; } else if (cell.index > spanMax) { spanMax = cell.index; } //Update the Cell name cell.refName = this.getCellName(row.index, cell.index); //Row span if (jsonCell.rowSpan !== null && jsonCell.rowSpan !== undefined) { cell.rowSpan = jsonCell.rowSpan - 1; } else { cell.rowSpan = 0; } //Column span if (jsonCell.colSpan !== null && jsonCell.colSpan !== undefined) { cell.colSpan = jsonCell.colSpan - 1; } else { cell.colSpan = 0; } //Hyperlink if (jsonCell.hyperlink !== null && jsonCell.hyperlink !== undefined) { var hyperLink = new HyperLink(); if (jsonCell.hyperlink.target !== undefined) { hyperLink.target = jsonCell.hyperlink.target; if (jsonCell.hyperlink.displayText !== undefined) { cell.value = jsonCell.hyperlink.displayText; } else { cell.value = jsonCell.hyperlink.target; } cell.type = this.getCellValueType(cell.value); hyperLink.ref = cell.refName; hyperLink.rId = (this.mHyperLinks.length + 1); this.mHyperLinks.push(hyperLink); cell.cellStyle = new CellStyle(); /* tslint:disable-next-line:max-line-length */ this.parserCellStyle((jsonCell.style !== undefined ? this.mergeOptions(jsonCell.style, this.hyperlinkStyle) : this.hyperlinkStyle), cell.cellStyle, 'string'); cell.styleIndex = cell.cellStyle.index; } } // formulas if (jsonCell.formula !== null && jsonCell.formula !== undefined) { cell.formula = jsonCell.formula; cell.type = 'formula'; } //Cell value if (jsonCell.value !== null && jsonCell.value !== undefined) { if (cell.formula !== undefined) { cell.value = 0; } else { cell.value = jsonCell.value; cell.type = this.getCellValueType(cell.value); } } if (jsonCell.style !== null && jsonCell.style !== undefined && cell.styleIndex === undefined) { cell.cellStyle = new CellStyle(); if (cell.value instanceof Date) { this.parserCellStyle(jsonCell.style, cell.cellStyle, cell.type, 14); } else { this.parserCellStyle(jsonCell.style, cell.cellStyle, cell.type); } cell.styleIndex = cell.cellStyle.index; } else if (cell.value instanceof Date) { cell.cellStyle = new CellStyle(); this.parserCellStyle({}, cell.cellStyle, cell.type, 14); cell.styleIndex = cell.cellStyle.index; } this.parseCellType(cell); this.mergeCells = this.processMergeCells(cell, row.index, this.mergeCells); row.cells.add(cell); curCellIndex = (cell.index + 1); } row.spans = (spanMin) + ':' + (spanMax); }; Workbook.prototype.GetColors = function () { var colors; colors = new Map(); /* tslint:disable */ colors.set('WHITE', 'FFFFFFFF'); /* tslint:disable */ colors.set('SILVER', 'FFC0C0C0'); /* tslint:disable */ colors.set('GRAY', 'FF808080'); /* tslint:disable */ colors.set('BLACK', 'FF000000'); /* tslint:disable */ colors.set('RED', 'FFFF0000'); /* tslint:disable */ colors.set('MAROON', 'FF800000'); /* tslint:disable */ colors.set('YELLOW', 'FFFFFF00'); /* tslint:disable */ colors.set('OLIVE', 'FF808000'); /* tslint:disable */ colors.set('LIME', 'FF00FF00'); /* tslint:disable */ colors.set('GREEN', 'FF008000'); /* tslint:disable */ colors.set('AQUA', 'FF00FFFF'); /* tslint:disable */ colors.set('TEAL', 'FF008080'); /* tslint:disable */ colors.set('BLUE', 'FF0000FF'); /* tslint:disable */ colors.set('NAVY', 'FF000080'); /* tslint:disable */ colors.set('FUCHSIA', 'FFFF00FF'); /* tslint:disable */ colors.set('PURPLE', 'FF800080'); return colors; }; Workbook.prototype.processColor = function (colorVal) { if (colorVal.indexOf('#') === 0) { return colorVal.replace('#', 'FF'); } colorVal = colorVal.toUpperCase(); this.rgbColors = this.GetColors(); if (this.rgbColors.has(colorVal)) { colorVal = this.rgbColors.get(colorVal); } else { colorVal = 'FF000000'; } return colorVal; }; Workbook.prototype.processCellValue = function (value, cell) { var cellValue = value; if (value.indexOf("<font") !== -1 || value.indexOf("<a") !== -1 || value.indexOf("<b>") !== -1 || value.indexOf("<i>") !== -1 || value.indexOf("<u>") !== -1) { var processedVal = ''; var startindex = value.indexOf('<', 0); var endIndex = value.indexOf('>', startindex + 1); if (startindex >= 0 && endIndex >= 0) { if (startindex !== 0) { processedVal += '<r><t xml:space="preserve">' + this.processString(value.substring(0, startindex)) + '</t></r>'; } while (startindex >= 0 && endIndex >= 0) { endIndex = value.indexOf('>', startindex + 1); if (endIndex >= 0) { var subString = value.substring(startindex + 1, endIndex); startindex = value.indexOf('<', endIndex + 1); if (startindex < 0) { startindex = cellValue.length; } var text = cellValue.substring(endIndex + 1, startindex); if (text.length !== 0) { var subSplit = subString.split(' '); if (subSplit.length > 0) { processedVal += '<r><rPr>'; } if (subSplit.length > 1) { for (var _i = 0, subSplit_1 = subSplit; _i < subSplit_1.length; _i++) { var element = subSplit_1[_i]; var start = element.trim().substring(0, 5); switch (start) { case 'size=': processedVal += '<sz val="' + element.substring(6, element.length - 1) + '"/>'; break; case 'face=': processedVal += '<rFont val="' + element.substring(6, element.length - 1) + '"/>'; break; case 'color': processedVal += '<color rgb="' + this.processColor(element.substring(7, element.length - 1)) + '"/>'; break; case 'href=': var hyperLink = new HyperLink(); hyperLink.target = element.substring(6, element.length - 1).trim(); hyperLink.ref = cell.refName; hyperLink.rId = (this.mHyperLinks.length + 1); this.mHyperLinks.push(hyperLink); processedVal += '<color rgb="FF0000FF"/><u/><b/>'; break; } } } else if (subSplit.length === 1) { var style = subSplit[0].trim(); switch (style) { case 'b': processedVal += '<b/>'; break; case 'i': processedVal += '<i/>'; break; case 'u': processedVal += '<u/>'; break; } } processedVal += '</rPr><t xml:space="preserve">' + this.processString(text) + '</t></r>'; } } } if (processedVal === '') { return cellValue; } return processedVal; } else { return cellValue; } } else { return cellValue; } }; Workbook.prototype.applyGlobalStyle = function (json, cellStyle) { if (this.cellStyles.has(json.name)) { cellStyle.index = this.mStyles.filter(function (a) { return (a.name === json.name); })[0].index; cellStyle.name = json.name; } }; /* tslint:disable:no-any */ Workbook.prototype.parserCellStyle = function (json, cellStyle, cellType, defStyleIndex) { //name if (json.name !== null && json.name !== undefined) { if (cellStyle.isGlobalStyle) { cellStyle.name = json.name; } else { this.applyGlobalStyle(json, cellStyle); return; } } //background color if (json.backColor !== null && json.backColor !== undefined) { cellStyle.backColor = json.backColor; } //borders //leftBorder cellStyle.borders = new Borders(); //AllBorder if (json.borders !== null && json.borders !== undefined) { this.parserBorder(json.borders, cellStyle.borders.all); } //leftborder if (json.leftBorder !== null && json.leftBorder !== undefined) { this.parserBorder(json.leftBorder, cellStyle.borders.left); } //rightBorder if (json.rightBorder !== null && json.rightBorder !== undefined) { this.parserBorder(json.rightBorder, cellStyle.borders.right); } //topBorder if (json.topBorder !== null && json.topBorder !== undefined) { this.parserBorder(json.topBorder,