UNPKG

@syncfusion/ej2-spreadsheet

Version:

Feature-rich JavaScript Spreadsheet (Excel) control with built-in support for selection, editing, formatting, importing and exporting to Excel

960 lines 119 kB
import { getRangeIndexes, updateCell, applyCellFormat, isReadOnly, isImported, getSwapRange, getGcd } from '../common/index'; import { getCell, getSheet, setCell, getSheetIndex, getColorCode, getCustomColors, getRow, isHiddenRow } from '../base/index'; import { Internationalization, getNumberDependable, getNumericObject, isNullOrUndefined, IntlBase } from '@syncfusion/ej2-base'; import { cldrData, defaultCurrencyCode } from '@syncfusion/ej2-base'; import { isNumber, toFraction, intToDate, toDate, dateToInt, rowFillHandler } from '../common/index'; import { applyNumberFormatting, getFormattedCellObject, refreshCellElement, checkDateFormat, getFormattedBarText, applyCF } from '../common/index'; import { getTextSpace, isCustomDateTime, setVisibleMergeIndex, refreshChart } from './../index'; import { checkIsNumberAndGetNumber, parseThousandSeparator } from '../common/internalization'; import { checkNumberFormat, parseDecimalNumber } from './../common/index'; import { localizedFormatAction, wrapEvent } from '../common/index'; /** * Specifies number format. */ var WorkbookNumberFormat = /** @class */ (function () { function WorkbookNumberFormat(parent) { this.parent = parent; this.localeObj = getNumericObject(this.parent.locale); var dependables = IntlBase.getDependables(cldrData, this.parent.locale, null).dateObject; if (dependables.dayPeriods && dependables.dayPeriods && dependables.dayPeriods.format && dependables.dayPeriods.format.wide) { this.localeObj.am = dependables.dayPeriods.format.wide.am || 'AM'; this.localeObj.pm = dependables.dayPeriods.format.wide.pm || 'PM'; } else { this.localeObj.am = 'AM'; this.localeObj.pm = 'PM'; } this.updateLocalizedFormats(dependables); this.addEventListener(); } WorkbookNumberFormat.prototype.numberFormatting = function (args) { var sheetIdx = this.parent.activeSheetIndex; var activeSheet = true; if (args.range && args.range.indexOf('!') > -1) { sheetIdx = getSheetIndex(this.parent, args.range.substring(0, args.range.lastIndexOf('!'))); activeSheet = sheetIdx === this.parent.activeSheetIndex; } var sheet = getSheet(this.parent, sheetIdx); var formatRange = args.range ? ((args.range.lastIndexOf('!') > -1) ? args.range.substring(args.range.lastIndexOf('!') + 1) : args.range) : sheet.selectedRange; var selectedRange = getSwapRange(getRangeIndexes(formatRange)); args.curSym = getNumberDependable(this.parent.locale, defaultCurrencyCode); var fArgs; var cell; var prevFormat; var row; var isVisibleRow; for (var rowIdx = selectedRange[0]; rowIdx <= selectedRange[2]; rowIdx++) { row = getRow(sheet, rowIdx); isVisibleRow = activeSheet && !isHiddenRow(sheet, rowIdx); for (var colIdx = selectedRange[1]; colIdx <= selectedRange[3]; colIdx++) { cell = getCell(rowIdx, colIdx, sheet, false, true); prevFormat = cell.format; if (!isReadOnly(cell, sheet.columns[colIdx], row) && !updateCell(this.parent, sheet, { cell: { format: args.format }, rowIdx: rowIdx, colIdx: colIdx })) { cell = getCell(rowIdx, colIdx, sheet); if (!(cell.rowSpan < 0 || cell.colSpan < 0)) { fArgs = { value: (cell.value || cell.value === 0) ? cell.value : (cell.hyperlink ? (typeof cell.hyperlink === 'string' ? cell.hyperlink : cell.hyperlink.address) : ''), format: cell.format, rowIndex: rowIdx, colIndex: colIdx, sheetIndex: sheetIdx, cell: cell, refresh: activeSheet, curSymbol: args.curSym }; this.getFormattedCell(fArgs); if (isVisibleRow) { this.setCell(fArgs); if (fArgs.td) { this.parent.notify(refreshCellElement, fArgs); if (cell.wrap && (!row || !row.customHeight) && prevFormat !== args.format) { this.parent.notify(wrapEvent, { range: [rowIdx, colIdx, rowIdx, colIdx], wrap: true, sheet: sheet, initial: true, td: fArgs.td, isOtherAction: true }); } } if (prevFormat && prevFormat !== args.format && prevFormat.includes('[') && getCustomColors().indexOf(getColorCode(args.format)) === -1) { this.removeFormatColor(fArgs, { format: prevFormat, style: cell.style }); } } } this.parent.setUsedRange(rowIdx, colIdx); } } } if (sheet.conditionalFormats && sheet.conditionalFormats.length) { this.parent.notify(applyCF, { indexes: selectedRange, isAction: true, isEdit: true }); } if (this.parent.chartColl && this.parent.chartColl.length) { this.parent.notify(refreshChart, { range: selectedRange }); } }; WorkbookNumberFormat.prototype.isDigitPlaceHolder = function (char) { return char === '#' || char === '0' || char === '?' || char === '.'; }; WorkbookNumberFormat.prototype.parseToLocalizedFormat = function (args) { if (args.decimalGroupSepsChanged && (args.format.includes('.') || args.format.includes(','))) { var formatChar = void 0; var endPos = void 0; var prevChar_1; var formatChars = args.format.split(''); for (var idx = 0; idx < formatChars.length; idx++) { formatChar = formatChars[idx]; if (formatChar === '"') { endPos = args.format.indexOf('"', idx + 1); if (endPos > -1) { idx = endPos; } } else if (formatChar === '_' || formatChar === '*' || formatChar === '\\') { idx++; } else if (formatChar === '[') { endPos = args.format.indexOf(']', idx + 1); if (endPos > -1) { idx = endPos; } } else if (formatChar === ',') { if (this.isDigitPlaceHolder(formatChars[idx - 1])) { formatChars[idx] = this.localeObj.group; } } else if (formatChar === '.') { if (formatChars[idx - 1]) { prevChar_1 = formatChars[idx - 1].toLowerCase(); if (!['d', 'm', 'y', 'h'].some(function (char) { return prevChar_1 === char; })) { formatChars[idx] = this.localeObj.decimal; } } else { formatChars[idx] = this.localeObj.decimal; } } } args.format = formatChars.join(''); } if (args.curChanged && args.format.includes("\"" + args.curSym + "\"")) { args.format = args.format.split("\"" + args.curSym + "\"").join(args.curSym); } }; WorkbookNumberFormat.prototype.updateLocalizedFormats = function (dependables, isFormatMapping) { var _this = this; var _a; numberFormatsCode = { currency: ['$#,##0.00', '$#,##0', '$#,##0_);($#,##0)', '$#,##0_);[Red]($#,##0)', '$#,##0.00_);($#,##0.00)', '$#,##0.00_);[Red]($#,##0.00)'], accounting: ['_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)', '_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)', '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)', '_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)'], time: dependables.timeFormats && dependables.timeFormats.medium === 'HH:mm:ss' ? 'HH:mm:ss' : 'h:mm:ss AM/PM' }; var curSym = getNumberDependable(this.parent.locale, defaultCurrencyCode); var args = { curChanged: curSym !== '$', curSym: curSym, decimalGroupSepsChanged: this.localeObj.decimal !== '.' && this.localeObj.group !== ',' }; if (args.curChanged) { var intl = new Internationalization(this.parent.locale); var formatStr = intl.getNumberPattern({ currency: '$', useGrouping: true, format: 'c0' }, true); if (formatStr && formatStr.endsWith('$')) { var curSpacing_1 = formatStr[formatStr.indexOf('$') - 1].trim().length ? '' : ' '; numberFormatsCode.currency.forEach(function (format, index) { if (format.includes('$#,##0')) { var decimalFormat = ''; var decimalPart = format.split('$#,##0.')[1]; if (decimalPart) { var decimalCount = 0; while (decimalPart[decimalCount] === '0') { decimalFormat += '0'; decimalCount++; } } if (decimalFormat) { decimalFormat = "." + decimalFormat; } numberFormatsCode.currency[index] = format.split("$#,##0" + decimalFormat).join("#,##0" + decimalFormat + curSpacing_1 + "\"" + curSym + "\""); } }); numberFormatsCode.accounting.forEach(function (format, index) { if (format.slice(0, format.indexOf('#')).includes('$')) { var formatArr_1 = format.split(';'); var replaceIdx_1; formatArr_1.forEach(function (formatStr, index) { if (formatStr.includes('$')) { formatStr = formatStr.replace('$', ''); if (formatStr.includes('0)')) { replaceIdx_1 = formatStr.indexOf('0)') + 2; } else { replaceIdx_1 = formatStr.lastIndexOf(formatStr.includes('0') ? '0' : (formatStr.includes('?') ? '?' : (formatStr.includes('"-"') ? '"' : '#'))) + 1; } if (replaceIdx_1 > 0) { formatArr_1[index] = formatStr.slice(0, replaceIdx_1) + curSpacing_1 + ("\"" + curSym + "\"") + formatStr.slice(replaceIdx_1); } } }); numberFormatsCode.accounting[index] = formatArr_1.join(';'); } }); } else { var updateLocalizedCurrency = function (format, index, formats) { return formats[index] = format.split('$').join("\"" + curSym + "\""); }; numberFormatsCode.currency.forEach(updateLocalizedCurrency); numberFormatsCode.accounting.forEach(updateLocalizedCurrency); } } var customFormats = ['General', '0', '0.00', '#,##0', '#,##0.00', '#,##0_);(#,##0)', '#,##0_);[Red](#,##0)', '#,##0.00_);(#,##0.00)', '#,##0.00_);[Red](#,##0.00)', numberFormatsCode.currency[2], numberFormatsCode.currency[3], numberFormatsCode.currency[4], numberFormatsCode.currency[5], '0%', '0.00%', '0.00E+00', '##0.0E+0', '# ?/?', '# ??/??', 'm/d/yyyy', 'd-mmm-yy', 'd-mmm', 'mmm-yy', 'h:mm AM/PM', 'h:mm:ss AM/PM', 'h:mm', 'h:mm:ss', 'm/d/yyyy h:mm', 'mm:ss', 'mm:ss.0', '@', '[h]:mm:ss'].concat(numberFormatsCode.accounting); if (isFormatMapping) { (_a = this.customFormats).splice.apply(_a, [0, customFormats.length].concat(customFormats)); } else { this.customFormats = customFormats; this.localizedFormats = []; } var defaultFormatsId = [0, 1, 2, 3, 4, 37, 38, 39, 40, 5, 6, 7, 8, 9, 10, 11, 48, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 45, 47, 49, 46, 42, 41, 44, 43]; var formatIdx; defaultFormatsId.forEach(function (id, index) { if (defaultFormats && defaultFormats.has(id)) { _this.customFormats[index] = defaultFormats.get(id); formatIdx = [5, 6, 7, 8].indexOf(id); if (formatIdx > -1) { numberFormatsCode.currency[formatIdx + 2] = _this.customFormats[index]; } else { formatIdx = [42, 41, 44, 43].indexOf(id); if (formatIdx > -1) { numberFormatsCode.accounting[formatIdx] = _this.customFormats[index]; } } } args.format = _this.customFormats[index]; _this.parseToLocalizedFormat(args); _this.localizedFormats[index] = args.format; }); var _loop_1 = function (idx) { var cusFormatIdx = this_1.localizedFormats.findIndex(function (format, index) { return format === _this.localizedFormats[idx] && index < defaultFormatsId.length; }); if (cusFormatIdx > -1) { this_1.localizedFormats.splice(idx, 1); this_1.customFormats.splice(idx, 1); idx--; } out_idx_1 = idx; }; var this_1 = this, out_idx_1; for (var idx = defaultFormatsId.length; idx < this.localizedFormats.length; idx++) { _loop_1(idx); idx = out_idx_1; } }; WorkbookNumberFormat.prototype.localizedFormatAction = function (args) { if (args.action === 'getLocalizedFormats') { args.defaultFormats = this.customFormats; args.localizedFormats = this.localizedFormats; } else if (args.action === 'mapNumberFormatId') { this.updateLocalizedFormats(IntlBase.getDependables(cldrData, this.parent.locale, null).dateObject, true); } else { args.curSym = getNumberDependable(this.parent.locale, defaultCurrencyCode); if (args.action === 'parseToDefaultFormat') { this.parseToDefaultFormat(args); } else { // addToCustomFormats action args.decimalGroupSepsChanged = this.localeObj.decimal !== '.' && this.localeObj.group !== ','; args.curChanged = args.curSym !== '$'; if (!args.defaultFormat) { args.defaultFormat = args.format; this.parseToLocalizedFormat(args); } if (this.localizedFormats.indexOf(args.format) === -1) { this.localizedFormats.push(args.format); this.customFormats.push(args.defaultFormat); } } } }; WorkbookNumberFormat.prototype.parseToDefaultFormat = function (args) { var _this = this; var decimalSepChanged = this.localeObj.decimal !== '.' && args.format.includes(this.localeObj.decimal); var groupSepChanged = this.localeObj.group !== ',' && args.format.includes(this.localeObj.group); var curSymChanged = args.curSym !== '$' && args.format.includes(args.curSym); if (decimalSepChanged || groupSepChanged || curSymChanged) { var endPos_1; var prevChar_2; var formatChar_1; var formatSection_1 = args.format.split(';'); formatSection_1.forEach(function (format, index) { var formatChars = format.split(''); for (var idx = 0; idx < formatChars.length; idx++) { formatChar_1 = formatChars[idx]; if (formatChar_1 === '"') { idx = format.indexOf('"', idx + 1); } else if (formatChar_1 === '_' || formatChar_1 === '*' || formatChar_1 === '\\') { idx++; } else if (formatChar_1 === '[') { endPos_1 = format.indexOf(']', idx + 1); if (endPos_1 > -1) { idx = endPos_1; } } else if (decimalSepChanged && formatChar_1 === _this.localeObj.decimal) { prevChar_2 = formatChars[idx - 1]; if (prevChar_2) { prevChar_2 = prevChar_2.toLowerCase(); if (!['d', 'm', 'y', 'h'].some(function (char) { return prevChar_2 === char; })) { formatChars[idx] = '.'; } } else { formatChars[idx] = '.'; } } else if (groupSepChanged && formatChar_1 === _this.localeObj.group) { if (_this.isDigitPlaceHolder(formatChars[idx - 1])) { formatChars[idx] = ','; } } else if (curSymChanged) { if (formatChar_1 === args.curSym) { formatChars[idx] = "\"" + args.curSym + "\""; } else if (args.curSym.startsWith(formatChar_1) && format.substring(idx, idx + args.curSym.length) === args.curSym) { formatChars.splice(idx, args.curSym.length, "\"" + args.curSym + "\""); } } } formatSection_1[index] = formatChars.join(''); }); args.format = formatSection_1.join(';'); } }; /** * @hidden * * @param {Object} args - Specifies the args. * @returns {string} - to get formatted cell. */ WorkbookNumberFormat.prototype.getFormattedCell = function (args) { var fResult = args.value === undefined || args.value === null ? '' : args.value; args.sheetIndex = args.sheetIndex === undefined ? this.parent.activeSheetIndex : args.sheetIndex; var sheet = getSheet(this.parent, args.sheetIndex); var cell = args.cell || getCell(args.rowIndex, args.colIndex, sheet, false, true); var rightAlign = false; var intl = new Internationalization(); if (!args.curSymbol) { args.curSymbol = getNumberDependable(this.parent.locale, defaultCurrencyCode); } if ((!args.format || args.format === 'General') && !args.skipFormatCheck && (!cell.formula || !cell.formula.toLowerCase().startsWith('=text('))) { args.type = args.format = 'General'; if (!cell.formula || (cell.formula && cell.formula.indexOf('&-') === -1)) { // for 5&-3=>5-3. var dateEventArgs = { value: fResult, updatedVal: fResult, cell: cell, isEdit: args.isEdit, intl: intl }; this.checkDateFormat(dateEventArgs); if (dateEventArgs.isDate || dateEventArgs.isTime) { rightAlign = true; cell.value = args.value = dateEventArgs.updatedVal; if (cell.format && cell.format !== 'General') { args.format = cell.format; args.type = getTypeFromFormat(args.format); } else { cell.format = args.format = getFormatFromType(dateEventArgs.isDate ? 'ShortDate' : 'Time'); } } } } else { args.type = getTypeFromFormat(args.format); if (args.skipFormatCheck && !args.format && args.type === 'General') { args.format = 'General'; } } if (cell.format && this.isCustomType(cell)) { args.type = 'Custom'; var isTextFormat = cell.format.indexOf('@') > -1; if (fResult !== '' && !isTextFormat && this.isPercentageValue(fResult.toString(), args, cell)) { fResult = args.value.toString(); } var isCustomText = void 0; var option = {}; if (defaultFormats && isImported(this.parent)) { cell.format = args.format = this.getMatchingCustomFormat(cell.format); } var orgFormat = cell.format; cell.format = cell.format.split('\\').join(''); var formats = cell.format.split(';'); if (isCustomDateTime(formats[0], true, option, true)) { if (fResult !== '') { args.result = this.processCustomDateTime(args, cell, option.type !== 'time', formats); isCustomText = !args.formatApplied; } args.result = args.result || cell.value; } else if (formats.length > 1) { if (cell.format.indexOf('<') > -1 || cell.format.indexOf('>') > -1) { args.result = this.processCustomConditions(cell, args); } else { var numObj = checkIsNumberAndGetNumber(cell, this.parent.locale, this.localeObj.group, this.localeObj.decimal, args.curSymbol); if (numObj.isNumber) { cell.value = numObj.value; this.processCustomAccounting(cell, args, formats, formats[0]); isCustomText = false; } else { args.result = this.processCustomText(cell, args, formats); isCustomText = true; } } cell.format = orgFormat; } else if (isTextFormat) { isCustomText = true; args.result = this.processCustomText(cell, args); } else { var numObj = checkIsNumberAndGetNumber({ value: fResult }, this.parent.locale, this.localeObj.group, this.localeObj.decimal); if (numObj.isNumber) { cell.value = args.value = numObj.value; if (cell.format.includes('E+0')) { if (args.format !== cell.format) { args.format = cell.format; } this.checkAndSetColor(args); var numberFormat = args.format.split('E')[0]; var formatArr = numberFormat.split('.'); if (this.localeObj.decimal !== '.' && formatArr.length === 1) { formatArr = numberFormat.split(this.localeObj.decimal); } args.result = formatArr[0].length > 1 ? this.scientificHashFormat(args, formatArr) : this.scientificFormat(args); } else { args.result = this.processCustomNumberFormat(cell, args); isCustomText = !isNumber(cell.value); } } else { if (cell.format && cell.format.includes('[')) { this.removeFormatColor(args, { format: cell.format, style: cell.style }); } isCustomText = args.dataUpdate = true; } } if (args.dataUpdate) { args.formattedText = args.result || (isNullOrUndefined(args.value) ? '' : args.value.toString()); } else { args.value = args.result; args.formattedText = isNullOrUndefined(args.value) ? '' : args.value.toString(); } if (isCustomText) { args.isRightAlign = false; } else { args.isRightAlign = !isNullOrUndefined(args.value); } } else { var result = this.processFormats(args, fResult, rightAlign, cell, intl, sheet); args.formattedText = result.fResult || (args.value === undefined || args.value === null ? '' : args.value.toString()); args.isRightAlign = result.rightAlign; } if (args.rowIndex !== undefined) { if (cell.format && args.formattedText && args.formattedText !== cell.value && cell.format !== 'General') { cell.formattedText = args.formattedText; } else if (cell.formattedText) { delete cell.formattedText; } } return args.formattedText; }; WorkbookNumberFormat.prototype.isCustomType = function (cell) { var format = getTypeFromFormat(cell.format); return (format === 'General' && cell.format !== 'General') || (format === 'Time' && this.parent.isEdit); }; WorkbookNumberFormat.prototype.processCustomFill = function (format, cell, args, formatText) { var repeatChar = format[format.indexOf('*') + 1]; var codes = format.split('*' + repeatChar); if (args.rowIndex === undefined || args.dataUpdate) { formatText = formatText || this.processCustomNumberFormat({ format: codes.join(''), value: cell.value }, args); } else { var secText = void 0; if (codes[1]) { var cellVal = parseFloat(cell.value); if (cellVal < 0) { secText = this.processCustomNumberFormat({ format: codes[1], value: Math.abs(cellVal).toString() }, args); formatText = "-" + codes[0].split('\'').join(''); } else { secText = this.processCustomNumberFormat({ format: codes[1], value: cell.value }, args); formatText = codes[0].split('\'').join(''); } if (cellVal === 0) { secText = secText.split('0').join(''); } } else { formatText = formatText || this.processCustomNumberFormat({ format: codes[0], value: cell.value }, args); } args.isRowFill = true; this.setCell(args); this.parent.notify(rowFillHandler, { cell: cell, cellEle: args.td, rowIdx: args.rowIndex, colIdx: args.colIndex, beforeFillText: formatText, repeatChar: repeatChar, afterFillText: secText }); formatText = this.parent.isPrintingProcessing ? formatText + secText : formatText; } return formatText; }; WorkbookNumberFormat.prototype.processCustomDateTime = function (args, cell, isDate, formatSections) { var _this = this; if (this.localeObj.decimal !== '.' && cell.value && cell.value.toString().includes(this.localeObj.decimal)) { var cellVal = cell.value.replace(this.localeObj.decimal, '.'); if (isNumber(cellVal)) { cell.value = args.value = cellVal; } } var isCustomDate; var checkCustomDate = function () { var cellVal = cell.value.toString(); if (cellVal.includes(_this.localeObj.dateSeparator) || cellVal.indexOf('-') > 0 || cellVal.includes(_this.localeObj.timeSeparator)) { return true; } var formats = IntlBase.getDependables(cldrData, _this.parent.locale, null).dateObject; var months = formats.months['stand-alone'] && formats.months['stand-alone'].abbreviated; return months && !!Object.keys(months).find(function (key) { return cellVal.includes(months["" + key]); }); }; if (!isNumber(cell.value)) { isCustomDate = checkCustomDate(); if (!isCustomDate) { return this.processCustomText(cell, args, formatSections); } } else if (formatSections.length > 1 && parseFloat(cell.value) <= 0) { args.formatApplied = this.processCustomAccounting(cell, args, formatSections); if (args.formatApplied) { return args.result; } } var type; var custFormat = formatSections[0]; var intl = new Internationalization(); var formatDateTime = function (checkDate) { var isValidDate; var dateArgs; if (isCustomDate) { var noOfDays = void 0; if (cell.format.includes('[h]')) { var timeArr = cell.value.toString().split(':'); if (timeArr.length > 1 && Number(timeArr[0]) >= 24) { noOfDays = Number(timeArr[0]) / 24; timeArr[0] = '24'; cell.value = timeArr.join(':'); } } dateArgs = toDate(cell.value, new Internationalization(), _this.parent.locale, custFormat, cell); isValidDate = dateArgs.dateObj && dateArgs.dateObj.toString() !== 'Invalid Date'; if (isValidDate) { if (dateArgs.dateObj.getFullYear() < 1900) { return ''; } else { var dateIntVal = dateToInt(dateArgs.dateObj, cell.value.toString().includes(':'), dateArgs.type === 'time'); if (noOfDays >= 1) { dateIntVal += noOfDays; dateArgs.dateObj = intToDate(dateIntVal); } cell.value = dateIntVal.toString(); } } } else { if (_this.checkAndProcessNegativeValue(args, cell.value)) { args.formatApplied = true; return args.formattedText; } dateArgs = { dateObj: intToDate(parseFloat(cell.value)) }; isValidDate = dateArgs.dateObj && dateArgs.dateObj.toString() !== 'Invalid Date'; } if (isValidDate) { if (checkDate && isDate) { args.dateObj = dateArgs.dateObj; } args.formatApplied = true; var result = void 0; if (custFormat.startsWith('M/d/yyyy ')) { // While auto detect date time value, we will set this format only. custFormat = custFormat.split(' ').splice(1).join(' '); result = intl.formatDate(dateArgs.dateObj, { type: 'date', skeleton: 'yMd' }); if (custFormat) { var valArr = cell.value.toString().split('.'); var dateTimeObj = valArr.length === 2 ? intToDate(parseFloat(valArr[0] + 1 + '.' + valArr[1])) : dateArgs.dateObj; result += ' ' + intl.formatDate(dateTimeObj, { type: type, format: custFormat }); } } else { if (custFormat.includes(':') && cell.value.toString().includes('.')) { if (isDate) { if (custFormat.includes(' ')) { var dateTimeFormat = custFormat.trim(); var spaceIdx = dateTimeFormat.indexOf(':') - 1; while (spaceIdx > 0 && dateTimeFormat[spaceIdx] !== ' ') { spaceIdx--; } if (spaceIdx <= 0) { spaceIdx = dateTimeFormat.lastIndexOf(':') + 1; var isAmPmFound = dateTimeFormat.includes(' a'); while (spaceIdx < dateTimeFormat.length) { if (dateTimeFormat[spaceIdx] === ' ') { if (isAmPmFound && dateTimeFormat[spaceIdx + 1] === 'a') { spaceIdx++; isAmPmFound = false; } else { break; } } spaceIdx++; } if (spaceIdx < dateTimeFormat.length) { var timeFormat = dateTimeFormat.substring(0, spaceIdx); var dateFormat = dateTimeFormat.substring(spaceIdx + 1); if (dateFormat && timeFormat) { if (dateFormat.includes('m')) { dateFormat = dateFormat.split('m').join('M'); } var valArr = cell.value.toString().split('.'); result = intl.formatDate(intToDate(parseFloat(valArr[0] + 1 + '.' + valArr[1])), { type: 'time', format: timeFormat }) + ' ' + intl.formatDate(dateArgs.dateObj, { type: 'date', format: dateFormat }); } else { result = intl.formatDate(dateArgs.dateObj, { type: type, format: custFormat }); } } else { result = intl.formatDate(dateArgs.dateObj, { type: type, format: custFormat }); } } else { var dateFormat = dateTimeFormat.substring(0, spaceIdx); var timeFormat = dateTimeFormat.substring(spaceIdx + 1); if (dateFormat && timeFormat) { if (dateFormat.includes('m')) { dateFormat = dateFormat.split('m').join('M'); } var valArr = cell.value.toString().split('.'); result = intl.formatDate(dateArgs.dateObj, { type: 'date', format: dateFormat }) + ' ' + intl.formatDate(intToDate(parseFloat(valArr[0] + 1 + '.' + valArr[1])), { type: 'time', format: timeFormat }); } else { result = intl.formatDate(dateArgs.dateObj, { type: type, format: custFormat }); } } } else { result = intl.formatDate(dateArgs.dateObj, { type: type, format: custFormat }); } } else { var colonIdx = custFormat.indexOf(':'); if ((custFormat.endsWith(' a') || custFormat.endsWith('ss') || custFormat.startsWith('H')) && (colonIdx === 1 || colonIdx === 2)) { var valArr = cell.value.toString().split('.'); result = intl.formatDate(intToDate(parseFloat(valArr[0] + 1 + '.' + valArr[1])), { type: 'time', format: custFormat }); } else { result = intl.formatDate(dateArgs.dateObj, { type: type, format: custFormat }); } } } else { result = intl.formatDate(dateArgs.dateObj, { type: type, format: custFormat }); } custFormat = custFormat.toLowerCase(); if (custFormat.startsWith('[h]')) { var totalHours = (Number(cell.value.toString().split('.')[0]) * 24) + dateArgs.dateObj.getHours(); result = totalHours.toString() + result.slice(result.indexOf(']') + 1); } else if (custFormat.startsWith('[m')) { var totalMins = (Number(cell.value.toString().split('.')[0]) * 1440) + (dateArgs.dateObj.getHours() * 60) + dateArgs.dateObj.getMinutes(); result = totalMins.toString() + result.slice(result.indexOf(']') + 1); } else if (custFormat.startsWith('[s')) { result = ((Number(cell.value.toString().split('.')[0]) * 86400) + (((dateArgs.dateObj.getHours() * 60) + dateArgs.dateObj.getMinutes()) * 60) + dateArgs.dateObj.getSeconds()).toString(); } } if (isShortMeridian) { return result.replace(_this.localeObj.am, 'A').replace(_this.localeObj.pm, 'P'); } return result; } return ''; }; custFormat = custFormat.split('_(').join(' ').split('_)').join(' '); if (cell.format.indexOf('h') > -1) { custFormat = custFormat.split('h').join('H'); type = 'time'; } if (cell.format.indexOf('s') > -1) { type = 'time'; } var isShortMeridian = cell.format.indexOf('A/P') > -1; if (cell.format.indexOf('AM/PM') > -1 || isShortMeridian) { custFormat = custFormat.split('H').join('h'); custFormat = custFormat.split('A/P').join('AM/PM').split('AM/PM').join('a'); type = 'time'; } if (cell.format.indexOf('d') > -1) { type = 'date'; // Split the format with ' ' for replacing d with E only for a day of the week in the MMM d, yyyy ddd format var formatArr = custFormat.split(' '); var dayMatchStr = void 0; var splitFormat = void 0; var part = void 0; var separator = void 0; for (var formatIdx = 0; formatIdx < formatArr.length; formatIdx++) { separator = formatArr[formatIdx].includes(this.localeObj.dateSeparator) ? this.localeObj.dateSeparator : '-'; splitFormat = formatArr[formatIdx].split(separator); for (var index = 0; index < splitFormat.length; index++) { part = splitFormat[index]; dayMatchStr = part.match(/d/g); if (dayMatchStr && dayMatchStr.length > 2) { splitFormat[index] = part.split('d').join('E'); } } formatArr[formatIdx] = splitFormat.join(separator); } custFormat = formatArr.join(' '); } if (cell.format.indexOf('m') > -1) { if (cell.format.indexOf('s') > -1 || cell.format.indexOf('h') > -1) { type = 'time'; if (cell.format.includes(' ')) { var formatArr = custFormat.split(' '); if (formatArr[0].includes('d') || formatArr[0].includes('y')) { formatArr[0] = formatArr[0].split('m').join('M'); custFormat = formatArr.join(' '); } } } else { type = 'date'; custFormat = custFormat.split('m').join('M'); if (custFormat.includes('MMMMM')) { var prevFormat = custFormat; custFormat = 'MMMM'; var monthName = formatDateTime()[0]; custFormat = prevFormat.split('MMMMM').join('p'); return formatDateTime(args.checkDate).split('p').join(monthName); } } } return formatDateTime(args.checkDate); }; WorkbookNumberFormat.prototype.processCustomConditions = function (cell, args) { if (isNumber(cell.value)) { var formatArr = cell.format.split(';'); var val = Number(cell.value); var compareVal = void 0; var conditionNotMatch = void 0; var colorCode = void 0; for (var i = 0; i < formatArr.length; i++) { cell.format = formatArr[i]; colorCode = getColorCode(cell.format); if (colorCode) { cell.format = cell.format.split("[" + colorCode + "]").join(''); } if (cell.format.includes('[')) { compareVal = cell.format.split('[')[1].split(']')[0]; var ltEqualTo = compareVal.split('<='); var gtEqualTo = compareVal.split('>='); var lessThan = compareVal.split('<'); var greaterThan = compareVal.split('>'); if ((ltEqualTo.length === 2 && val <= Number(ltEqualTo[1])) || (gtEqualTo.length === 2 && val >= Number(gtEqualTo[1])) || (lessThan.length === 2 && val < Number(lessThan[1])) || (greaterThan.length === 2 && val > Number(greaterThan[1]))) { cell.format = formatArr[i].split("[" + compareVal + "]").join(''); conditionNotMatch = false; break; } conditionNotMatch = compareVal.split(/<=|>=|<|>/).length === 2; } else { cell.format = formatArr[i]; conditionNotMatch = false; break; } } if (conditionNotMatch) { this.removeFormatColor(args, { format: formatArr.join(''), style: cell.style }); return this.processCustomFill('*#', cell, args, '#####'); } return this.processCustomNumberFormat(cell, args); } else { return cell.value; } }; WorkbookNumberFormat.prototype.processCustomAccounting = function (cell, args, formats, format) { var cellVal = parseFloat(cell.value); if (cellVal < 0) { if (!formats[1].includes('@')) { format = formats[1]; } } else if (cellVal === 0 && formats[2] && !formats[2].includes('@')) { format = formats[2].includes(args.curSymbol + "0") ? formats[2].split('0').join('#') : formats[2]; } if (format) { args.result = this.processCustomNumberFormat({ format: cell.format, value: cellVal < 0 ? Math.abs(cellVal).toString() : cell.value, style: cell.style }, args, format); return true; } return false; }; WorkbookNumberFormat.prototype.processCustomText = function (cell, args, formatSections) { var cellVal = cell.value || cell.value ? cell.value.toString() : ''; var format; if (formatSections) { if (formatSections[3]) { format = formatSections[3]; } else if (formatSections[1] && formatSections[1].includes('@')) { format = formatSections[1]; } else { return cellVal; } } else { format = cell.format; } var result = this.processCustomNumberFormat({ format: format.split('@').join('#'), value: cellVal.split(cellVal).join('1') }, args); if (result) { result = result.split('1').join(cellVal); if (this.localeObj.decimal !== '.' && isNumber(result) && result.includes('.')) { result = result.replace('.', this.localeObj.decimal); } } return result; }; WorkbookNumberFormat.prototype.thousandSeparator = function (count, value) { while (count) { value = value / 1000; count--; } return value; }; WorkbookNumberFormat.prototype.getSeparatorCount = function (cell) { var count = 0; var codes = ['#', '0']; for (var i = 0; i < cell.format.length; i++) { if (cell.format[i] === '"' && cell.format[i - 1] !== '\\') { i = cell.format.indexOf('"', i + 1); } else if (cell.format[i] === ',' && !(codes.indexOf(cell.format[i + 1]) > -1)) { count++; } } return count; }; WorkbookNumberFormat.prototype.processDigits = function (cell, customFormat) { customFormat = customFormat.split('?').join('0'); var cellValue = cell.value.toString(); cellValue = this.getFormattedNumber(customFormat, parseFloat(cellValue)); if (cellValue && cellValue.includes(this.localeObj.decimal)) { var valArr = cellValue.split(this.localeObj.decimal); cellValue = valArr[0] + this.localeObj.decimal + valArr[1].split('0').join(' '); } return cellValue || cell.value; }; WorkbookNumberFormat.prototype.processFormatWithSpace = function (format, cell, cellValue) { var space = ' '; var args = { cell: cell, char: space, width: 0 }; this.parent.notify(getTextSpace, args); var spaceWidth = args.width; var count; var result = { format: format, formattedText: '' }; for (var i = 0; i < format.length; i++) { if (format[i] === '_') { args.char = format[i + 1]; this.parent.notify(getTextSpace, args); var textWidth = args.width; count = Math.round(textWidth / spaceWidth); format = format.replace(format[i] + format[i + 1], space.repeat(count)); } } var lastSpaceCount = format.length - format.trim().length; if (lastSpaceCount > 0) { result.formattedText = this.getFormattedNumber(format.trim(), cellValue); if (format[0] === ' ') { var frontSpaceCount = 1; var idx = 1; while (format[idx] === ' ') { frontSpaceCount++; idx++; } lastSpaceCount -= frontSpaceCount; result.formattedText = space.repeat(frontSpaceCount) + result.formattedText; } result.formattedText += space.repeat(lastSpaceCount); } else { result.formattedText = this.getFormattedNumber(format, cellValue); } result.format = format; return result; }; WorkbookNumberFormat.prototype.removeFormatColor = function (args, cell) { if (getCustomColors().indexOf(getColorCode(cell.format)) > -1) { args.color = cell.style && cell.style.color ? cell.style.color : ''; this.applyColor(args); } }; WorkbookNumberFormat.prototype.processCustomNumberFormat = function (cell, args, format) { if (!cell.format) { return ''; } var formattedText = cell.value; var numArgs = checkIsNumberAndGetNumber(cell, this.parent.locale, this.localeObj.group, this.localeObj.decimal); if (numArgs.isNumber) { var isFormatted = void 0; var isZeroFormat = void 0; cell.value = numArgs.value; var cellValue = parseFloat(cell.value.toString()); var customFormat = format || cell.format; if (cell.format.indexOf('[') > -1) { var colorCode = getColorCode(customFormat); if (colorCode) { customFormat = customFormat.split("[" + colorCode + "]").join(''); args.color = colorCode.toLowerCase(); this.applyColor(args); } else { this.removeFormatColor(args, cell); } } if (customFormat.indexOf('"') > -1 || customFormat.indexOf('\\') > -1) { customFormat = this.processText(customFormat); isZeroFormat = cellValue === 0 && !customFormat.includes('#') && !customFormat.includes('0'); if (isZeroFormat) { customFormat += '#'; } } var separatorCount = this.getSeparatorCount(cell); if (separatorCount) { isFormatted = true; var result = this.thousandSeparator(separatorCount, cellValue); if (customFormat.indexOf('.') === -1) { result = Math.round(result); } formattedText = this.getFormattedNumber(customFormat.split(',').join(''), r