all files / workbook/base/ sheet.js

90.8% Statements 227/250
84.24% Branches 155/184
94.87% Functions 37/39
90.5% Lines 219/242
18 statements, 7 functions, 29 branches Ignored     
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392              1625×   3250× 3250×                                                                             3250×                                 977×             3600×                                                                                             37029× 37029×   37029× 39191× 36731× 36731×     37029×   808735× 808735× 815160× 808278× 808278×     808735×   2020× 2020×   507232× 507232× 512037× 507216×     16×   10562× 10562×   10616×   1208×   4906249×   1111× 1111× 2077×   1111× 1113×     1111×         1148× 1148× 2142×   1148×   1000× 1000× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228× 1228×   1228× 1228× 1221× 1221× 1221× 1221×   1228× 1228×   1000×   1228× 4414× 4230×     1228×   31016× 31016×                                 14×                        
/* istanbul ignore next */ 
var __extends = (this && this.__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 __());
    };
})();
/* istanbul ignore next */ 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "./data", "../common/index", "../common/index", "@syncfusion/ej2-base"], function (require, exports, data_1, index_1, index_2, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Range = (function (_super) {
        __extends(Range, _super);
        function Range() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        Range.prototype.setProperties = function (prop, muteOnChange) {
            var _this = this;
            Iif (this['parentObj'].isComplexArraySetter && this['controlParent'] && this['controlParent'].isAngular) {
                if (Object.keys(prop).length) {
                    if (this['parentObj']['currRangeIdx'] === undefined) {
                        this['parentObj']['currRangeIdx'] = 0;
                    }
                    else {
                        this['parentObj']['currRangeIdx'] += 1;
                    }
                    var range = this['parentObj'].ranges[this['parentObj']['currRangeIdx']];
                    if (range && range.info) {
                        this.info = range.info;
                    }
                    setTimeout(function () {
                        if (_this['parentObj']['currRangeIdx'] !== undefined) {
                            delete _this['parentObj']['currRangeIdx'];
                        }
                    });
                }
                else if (this['controlParent'].tagObjects[0].instance && this['controlParent'].tagObjects[0].instance.hasChanges
                    && !this['controlParent'].tagObjects[0].instance.isInitChanges) {
                    var sheetIdx = this['controlParent'].sheets.indexOf(this['parentObj']);
                    if (this['parentObj'].changedRangeIdx === undefined) {
                        var rangeIdx_1;
                        var tagObjects = this['controlParent'].tagObjects[0].instance.list[sheetIdx].tagObjects;
                        for (var i = 0; i < tagObjects.length; i++) {
                            if (tagObjects[i]['name'] === 'ranges') {
                                tagObjects[i]['instance'].list
                                    .forEach(function (range, idx) {
                                    if (range.hasChanges) {
                                        rangeIdx_1 = idx;
                                    }
                                });
                                break;
                            }
                        }
                        this['parentObj'].changedRangeIdx = rangeIdx_1;
                    }
                }
            }
            _super.prototype.setProperties.call(this, prop, muteOnChange);
        };
        __decorate([
            ej2_base_1.Property(null)
        ], Range.prototype, "dataSource", void 0);
        __decorate([
            ej2_base_1.Property('A1')
        ], Range.prototype, "startCell", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], Range.prototype, "query", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], Range.prototype, "fieldsOrder", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], Range.prototype, "showFieldAsHeader", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], Range.prototype, "template", void 0);
        __decorate([
            ej2_base_1.Property('A1')
        ], Range.prototype, "address", void 0);
        return Range;
    }(ej2_base_1.ChildProperty));
    exports.Range = Range;
    var UsedRange = (function (_super) {
        __extends(UsedRange, _super);
        function UsedRange() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        __decorate([
            ej2_base_1.Property(0)
        ], UsedRange.prototype, "rowIndex", void 0);
        __decorate([
            ej2_base_1.Property(0)
        ], UsedRange.prototype, "colIndex", void 0);
        return UsedRange;
    }(ej2_base_1.ChildProperty));
    exports.UsedRange = UsedRange;
    var Sheet = (function (_super) {
        __extends(Sheet, _super);
        function Sheet() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        __decorate([
            ej2_base_1.Property(null)
        ], Sheet.prototype, "rows", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], Sheet.prototype, "columns", void 0);
        __decorate([
            ej2_base_1.Complex({}, index_2.ProtectSettings)
        ], Sheet.prototype, "protectSettings", void 0);
        __decorate([
            ej2_base_1.Collection([], Range)
        ], Sheet.prototype, "ranges", void 0);
        __decorate([
            ej2_base_1.Collection([], index_1.ConditionalFormat)
        ], Sheet.prototype, "conditionalFormats", void 0);
        __decorate([
            ej2_base_1.Property(0)
        ], Sheet.prototype, "index", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], Sheet.prototype, "name", void 0);
        __decorate([
            ej2_base_1.Property(100)
        ], Sheet.prototype, "rowCount", void 0);
        __decorate([
            ej2_base_1.Property(100)
        ], Sheet.prototype, "colCount", void 0);
        __decorate([
            ej2_base_1.Property('A1:A1')
        ], Sheet.prototype, "selectedRange", void 0);
        __decorate([
            ej2_base_1.Property('A1')
        ], Sheet.prototype, "activeCell", void 0);
        __decorate([
            ej2_base_1.Complex({}, UsedRange)
        ], Sheet.prototype, "usedRange", void 0);
        __decorate([
            ej2_base_1.Property('A1')
        ], Sheet.prototype, "topLeftCell", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], Sheet.prototype, "showHeaders", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], Sheet.prototype, "showGridLines", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], Sheet.prototype, "isProtected", void 0);
        __decorate([
            ej2_base_1.Property('Visible')
        ], Sheet.prototype, "state", void 0);
        __decorate([
            ej2_base_1.Property(0)
        ], Sheet.prototype, "frozenRows", void 0);
        __decorate([
            ej2_base_1.Property(0)
        ], Sheet.prototype, "frozenColumns", void 0);
        __decorate([
            ej2_base_1.Property('A1')
        ], Sheet.prototype, "paneTopLeftCell", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], Sheet.prototype, "password", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], Sheet.prototype, "standardHeight", void 0);
        return Sheet;
    }(ej2_base_1.ChildProperty));
    exports.Sheet = Sheet;
    function getSheetIndex(context, name) {
        var idx;
        if (name.startsWith('\'') && name.endsWith('\'')) {
            name = name.replace(/''/g, '\'').replace(/^'|'$/g, '');
        }
        for (var i = 0; i < context.sheets.length; i++) {
            if (context.sheets[i].name.toLowerCase() === name.toLowerCase()) {
                idx = i;
                break;
            }
        }
        return idx;
    }
    exports.getSheetIndex = getSheetIndex;
    function getSheetIndexFromId(context, id) {
        var idx;
        for (var i = 0; i < context.sheets.length; i++) {
            if (context.sheets[i].id === id) {
                idx = i;
                break;
            }
        }
        return idx;
    }
    exports.getSheetIndexFromId = getSheetIndexFromId;
    function getSheetNameFromAddress(address) {
        var sheetRefIndex = address.lastIndexOf('!');
        return sheetRefIndex > -1 ? address.substring(0, sheetRefIndex).replace(/'/gi, '') : address.replace(/'/gi, '');
    }
    exports.getSheetNameFromAddress = getSheetNameFromAddress;
    function getSheetIndexByName(context, name, info) {
        var len = info.length;
        for (var i = 0; i < len; i++) {
            if (info[i].sheet.toUpperCase() === name.toUpperCase()) {
                return info[i].index;
            }
        }
        return -1;
    }
    exports.getSheetIndexByName = getSheetIndexByName;
    function updateSelectedRange(context, range, sheet, isMultiRange) {
        if (sheet === void 0) { sheet = {}; }
        context.setSheetPropertyOnMute(sheet, 'selectedRange', isMultiRange ? sheet.selectedRange + ' ' + range : range);
    }
    exports.updateSelectedRange = updateSelectedRange;
    function getSelectedRange(sheet) {
        return sheet && sheet.selectedRange || 'A1';
    }
    exports.getSelectedRange = getSelectedRange;
    function getSingleSelectedRange(sheet) {
        return sheet.selectedRange.split(' ')[0];
    }
    exports.getSingleSelectedRange = getSingleSelectedRange;
    function getSheet(context, idx) {
        return context.sheets[idx];
    }
    exports.getSheet = getSheet;
    function getSheetNameCount(context) {
        var name = [];
        context.sheets.forEach(function (sheet) {
            name.push(sheet.name.toLowerCase());
        });
        for (var i = 0; i < name.length; i++) {
            if (name.indexOf('sheet' + context.sheetNameCount) > -1) {
                context.sheetNameCount++;
            }
            else {
                return context.sheetNameCount++;
            }
        }
        return context.sheetNameCount++;
    }
    exports.getSheetNameCount = getSheetNameCount;
    function getMaxSheetId(sheets) {
        var cnt = 0;
        sheets.forEach(function (sheet) {
            cnt = Math.max(sheet.id, cnt);
        });
        return cnt + 1;
    }
    exports.getMaxSheetId = getMaxSheetId;
    function initSheet(context, sheet, isImport) {
        var sheets = sheet ? sheet : context.sheets;
        sheets.forEach(function (sheet) {
            sheet.id = sheet.id || 0;
            sheet.name = sheet.name || '';
            context.setSheetPropertyOnMute(sheet, 'rowCount', sheet.rowCount || 100);
            context.setSheetPropertyOnMute(sheet, 'colCount', sheet.colCount || 100);
            context.setSheetPropertyOnMute(sheet, 'topLeftCell', sheet.topLeftCell || 'A1');
            context.setSheetPropertyOnMute(sheet, 'activeCell', sheet.activeCell || 'A1');
            context.setSheetPropertyOnMute(sheet, 'selectedRange', sheet.selectedRange || sheet.activeCell + ':' + sheet.activeCell);
            context.setSheetPropertyOnMute(sheet, 'usedRange', sheet.usedRange || { rowIndex: 0, colIndex: 0 });
            context.setSheetPropertyOnMute(sheet, 'ranges', sheet.ranges ? sheet.ranges : []);
            context.setSheetPropertyOnMute(sheet, 'rows', (sheet.rows && ej2_base_1.extend([], sheet.rows, null, true)) || []);
            context.setSheetPropertyOnMute(sheet, 'columns', sheet.columns || []);
            context.setSheetPropertyOnMute(sheet, 'showHeaders', ej2_base_1.isUndefined(sheet.showHeaders) ? true : sheet.showHeaders);
            context.setSheetPropertyOnMute(sheet, 'showGridLines', ej2_base_1.isUndefined(sheet.showGridLines) ? true : sheet.showGridLines);
            context.setSheetPropertyOnMute(sheet, 'state', sheet.state || 'Visible');
            sheet.maxHgts = sheet.maxHgts || [];
            sheet.isImportProtected = sheet.isProtected && isImport;
            sheet.protectSettings = sheet.protectSettings || { selectCells: false, formatCells: false, formatRows: false, formatColumns: false,
                insertLink: false };
            sheet.isProtected = sheet.isProtected || false;
            if (!sheet.paneTopLeftCell || sheet.paneTopLeftCell === 'A1') {
                sheet.frozenRows = sheet.frozenRows ? sheet.frozenRows : 0;
                sheet.frozenColumns = sheet.frozenColumns ? sheet.frozenColumns : 0;
                var indexes = index_1.getCellIndexes(sheet.topLeftCell);
                context.setSheetPropertyOnMute(sheet, 'paneTopLeftCell', index_2.getCellAddress(sheet.frozenRows ? indexes[0] + sheet.frozenRows : indexes[0], sheet.frozenColumns ? indexes[1] + sheet.frozenColumns : indexes[1]));
            }
            data_1.processIdx(sheet.columns);
            initRow(sheet.rows);
        });
        data_1.processIdx(sheets, true, context);
    }
    exports.initSheet = initSheet;
    function initRow(rows) {
        rows.forEach(function (row) {
            if (row && row.cells) {
                data_1.processIdx(row.cells);
            }
        });
        data_1.processIdx(rows);
    }
    function getSheetName(context, idx) {
        if (idx === void 0) { idx = context.activeSheetIndex; }
        return getSheet(context, idx).name;
    }
    exports.getSheetName = getSheetName;
    function moveSheet(context, position, sheetIndexes, action, isFromUpdateAction) {
        var needRefresh = !!sheetIndexes;
        sheetIndexes = sheetIndexes || [context.activeSheetIndex];
        var sheetName = getSheetName(context);
        position = getNextPrevVisibleSheetIndex(context.sheets, position, context.activeSheetIndex > position);
        var args = {
            action: 'moveSheet', eventArgs: { position: position, sheetIndexes: sheetIndexes, cancel: false }
        };
        if (action) {
            context.trigger('actionBegin', args);
        }
        Eif (!args.eventArgs.cancel) {
            context.notify(index_1.moveSheetHandler, { prevIndex: context.activeSheetIndex, currentIndex: position });
            sheetIndexes.forEach(function (sIdx, idx) {
                context.sheets.splice(position + idx, 0, context.sheets.splice(sIdx + (position > sIdx ? -1 * idx : 0), 1)[0]);
            });
            context.setProperties({
                activeSheetIndex: isFromUpdateAction ? getSheetIndex(context, sheetName)
                    : (position > sheetIndexes[0] ? position - (sheetIndexes.length - 1) : position)
            }, true);
            context.notify(index_1.moveOrDuplicateSheet, { refresh: needRefresh });
            if (action) {
                delete args.eventArgs.cancel;
                context.trigger('actionComplete', args);
            }
        }
    }
    exports.moveSheet = moveSheet;
    function duplicateSheet(context, sheetIndex, action, isFromUpdateAction) {
        sheetIndex = ej2_base_1.isUndefined(sheetIndex) ? context.activeSheetIndex : sheetIndex;
        var args = {
            action: 'duplicateSheet', eventArgs: { sheetIndex: sheetIndex, cancel: false }
        };
        if (action) {
            context.trigger('actionBegin', args);
        }
        Eif (!args.eventArgs.cancel) {
            var originalSheet = getSheet(context, sheetIndex);
            var sheet = ej2_base_1.extend({}, originalSheet.properties ?
                originalSheet.properties : originalSheet, {}, true);
            sheet.id = getMaxSheetId(context.sheets);
            var name_1 = sheet.name;
            if (/^\(\d+\)$/.test('(' + name_1.split(' (')[1])) {
                name_1 = name_1.split(' (')[0];
            }
            var sheetNames_1 = [];
            context.sheets.forEach(function (sheet) {
                sheetNames_1.push(sheet.name);
            });
            for (var i = 2;; i++) {
                Eif (sheetNames_1.indexOf(name_1 + ' (' + i + ')') === -1) {
                    sheet.name = name_1 + ' (' + i + ')';
                    break;
                }
            }
            context.notify(index_1.duplicateSheetFilterHandler, { sheetIndex: sheetIndex, newSheetIndex: sheetIndex + 1 });
            context.createSheet(sheetIndex + 1, [sheet]);
            context.notify(index_1.workbookFormulaOperation, { action: 'addSheet', sheetName: 'Sheet' + sheet.id, visibleName: sheet.name, sheetId: sheet.id });
            if (!isFromUpdateAction) {
                context.setProperties({ activeSheetIndex: sheetIndex + 1 }, true);
            }
            context.notify(index_1.moveOrDuplicateSheet, { refresh: true, isDuplicate: true });
            if (action) {
                delete args.eventArgs.cancel;
                context.trigger('actionComplete', args);
            }
        }
    }
    exports.duplicateSheet = duplicateSheet;
    function getNextPrevVisibleSheetIndex(sheets, startIndex, isPrevious) {
        for (var i = startIndex; isPrevious ? i >= 0 : i < sheets.length; isPrevious ? i-- : i++) {
            Eif (!(sheets[i].state === 'Hidden' || sheets[i].state === 'VeryHidden')) {
                startIndex = i;
                break;
            }
        }
        return startIndex;
    }
});