all files / workbook/common/ address.js

100% Statements 75/75
100% Branches 28/28
100% Functions 16/16
100% Lines 75/75
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   3909165× 3909165× 3909165× 3909161× 3909161× 3909161× 3909161× 18× 18×   3909161× 7818410× 7818410× 7818410×     3909165×   7895929×   7896172× 7896172× 7896172× 11036956×   7896172×   7804715×   24038×     12388049× 12388049× 3361428×       9026621×     20240×   20957× 20957×   21×   332×   1795× 1795× 1137×     658×   1795×   72853× 72853× 248×   72853× 250×   72853×   498× 498× 498×   21617×      
define(["require", "exports", "@syncfusion/ej2-base", "../base/index"], function (require, exports, ej2_base_1, index_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function getRangeIndexes(range) {
        var cellindexes;
        var indexes = [];
        if (range) {
            range = range.lastIndexOf('!') > -1 ? range.substring(range.lastIndexOf('!') + 1) : range;
            range = range.indexOf(':') === -1 ? range + ':' + range : range;
            var containsAlphabetsAndDigits = new RegExp(/^(?=.*[a-zA-Z])(?=.*\d)/g);
            if (!containsAlphabetsAndDigits.test(range)) {
                var refArr = range.split(':');
                range = ej2_base_1.isNullOrUndefined(range.match(/[0-9]/)) ? (refArr[0] + '1:' + refArr[1] + '1') : ('A' + refArr[0] + ':A' + refArr[1]);
            }
            range.split(':').forEach(function (address) {
                cellindexes = getCellIndexes(address);
                indexes.push(cellindexes[0]);
                indexes.push(cellindexes[1]);
            });
        }
        return indexes;
    }
    exports.getRangeIndexes = getRangeIndexes;
    function getCellIndexes(address) {
        return [parseInt(address.match(/\d+/)[0], 10) - 1, getColIndex(address.match(/[A-Z]+/i)[0].toUpperCase())];
    }
    exports.getCellIndexes = getCellIndexes;
    function getColIndex(text) {
        var colIdx = 0;
        text = text.split('').reverse().join('');
        for (var i = text.length - 1; i >= 0; i--) {
            colIdx += (text[i].charCodeAt(0) - 64) * (Math.pow(26, i));
        }
        return colIdx - 1;
    }
    exports.getColIndex = getColIndex;
    function getCellAddress(sRow, sCol) {
        return getColumnHeaderText(sCol + 1) + (sRow + 1);
    }
    exports.getCellAddress = getCellAddress;
    function getRangeAddress(range) {
        return getCellAddress(range[0], range[1]) + ':' + (!ej2_base_1.isNullOrUndefined(range[2]) ?
            getCellAddress(range[2], range[3]) : getCellAddress(range[0], range[1]));
    }
    exports.getRangeAddress = getRangeAddress;
    function getColumnHeaderText(colIndex) {
        var alphabet = 'Z';
        if (colIndex / 26 > 1) {
            return getColumnHeaderText((colIndex % 26 === 0) ? (colIndex / 26 - 1) : Math.floor(colIndex / 26))
                + String.fromCharCode((colIndex % 26) === 0 ? alphabet.charCodeAt(0) : 64 + (colIndex % 26));
        }
        else {
            return String.fromCharCode(64 + (colIndex));
        }
    }
    exports.getColumnHeaderText = getColumnHeaderText;
    function getIndexesFromAddress(address) {
        return getRangeIndexes(getRangeFromAddress(address));
    }
    exports.getIndexesFromAddress = getIndexesFromAddress;
    function getRangeFromAddress(address) {
        var sheetRefIndex = address.lastIndexOf('!');
        return sheetRefIndex > -1 ? address.substring(sheetRefIndex + 1) : address;
    }
    exports.getRangeFromAddress = getRangeFromAddress;
    function getAddressFromSelectedRange(sheet) {
        return sheet.name + '!' + sheet.selectedRange;
    }
    exports.getAddressFromSelectedRange = getAddressFromSelectedRange;
    function getAddressInfo(context, address) {
        return { sheetIndex: getSheetIndexFromAddress(context, address), indices: getIndexesFromAddress(address) };
    }
    exports.getAddressInfo = getAddressInfo;
    function getSheetIndexFromAddress(context, address) {
        var sIdx;
        if (address.indexOf('!') > -1) {
            sIdx = index_1.getSheetIndex(context, index_1.getSheetNameFromAddress(address));
        }
        else {
            sIdx = context.activeSheetIndex;
        }
        return sIdx;
    }
    exports.getSheetIndexFromAddress = getSheetIndexFromAddress;
    function getSwapRange(range) {
        var clonedRange = range.slice();
        if (range[0] > range[2]) {
            swap(clonedRange, 0, 2);
        }
        if (range[1] > range[3]) {
            swap(clonedRange, 1, 3);
        }
        return clonedRange;
    }
    exports.getSwapRange = getSwapRange;
    function swap(range, x, y) {
        var tmp = range[x];
        range[x] = range[y];
        range[y] = tmp;
    }
    function isSingleCell(range) {
        return range[0] === range[2] && range[1] === range[3];
    }
    exports.isSingleCell = isSingleCell;
});