all files / workbook/actions/ merge.js

91.33% Statements 474/519
87.52% Branches 463/529
100% Functions 17/17
91.3% Lines 472/517
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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733   885× 885×   178× 178× 44× 44× 44×     178× 140×   178× 178× 167×   178× 166× 166× 97×     12× 17× 17×         178× 178× 178× 44×   178× 167× 167× 40×       195× 195× 195× 195× 195× 195× 195× 195× 195× 195× 195× 195×   195× 573× 573× 141×   573× 1881× 1881× 148× 148×   1881× 242× 242×   1881× 1313× 1313× 1313×   1313× 145×     1881× 195× 159× 123×   159× 107×   159× 159× 159×       1686× 1686× 1686× 1686× 1191× 872×   1191× 897× 897×       1722× 1722× 1722×     573×   195× 159× 121× 121× 121× 121× 17×     16×   17×     121× 121× 121×   38× 35×     195× 177×     177×     49177× 49177× 49177× 49177× 20752× 1142× 1142×     20752× 749× 749×     20752× 20752× 20742× 711×   20742× 1569×         12859×     12859× 12788×   71× 54×   17×       12788× 12788× 12788× 12788× 12788× 12788× 12788× 12788× 6954×   6954×   6954× 6954× 6954× 190× 190× 186× 186×     6954× 199× 199× 187× 187×         12788× 12788× 12788× 10422×   12788× 2244×   10544× 18411× 18411× 6362× 6362× 6362× 485× 485×     6362× 533× 533×     6362× 6362× 6362×   6362×         10544× 10544× 10544× 18412× 18412× 6590× 6590× 6590× 500× 500×       6590×         10544× 32544× 32544× 9325× 9325× 9325× 216× 216×       9325× 282× 282×       9325× 9325× 9325×       9325×                                                                                   21× 21× 21× 21× 21×       21×           21× 21× 21×       21×               54× 54× 54× 54× 54× 54× 50×   50×   50× 20× 20×   50× 25× 25×     25× 18×   50× 50× 50×   50×       54× 54×   50× 151× 151× 119× 119× 119×     119×       119× 119× 119×       119×             50× 50× 151× 151× 111× 111× 111×   111×   111× 111× 111×       111×             50× 50× 50× 249× 249× 153× 153× 153×       153×           153× 153× 153×       153×                           20× 20× 19× 19× 19×     19×       19× 19× 19×   19×           20× 20× 18× 18× 18×     18×   18× 18× 18×           35× 35× 29× 29× 29×       29× 13× 13×       29× 29× 29×     29×                   885× 885× 885× 885×   884× 884×   884× 12× 12× 12× 12×     126650×        
define(["require", "exports", "../base/index", "./../common/index", "./../common/index", "./../common/index", "@syncfusion/ej2-base"], function (require, exports, index_1, index_2, index_3, index_4, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookMerge = (function () {
        function WorkbookMerge(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookMerge.prototype.merge = function (args) {
            args.sheetIndex = ej2_base_1.isUndefined(args.sheetIndex) ? this.parent.activeSheetIndex : args.sheetIndex;
            if (args.isAction) {
                this.parent.notify('actionBegin', { eventArgs: args, action: 'merge' });
                Eif (!args.model) {
                    args.model = [];
                }
            }
            if (typeof args.range === 'string') {
                args.range = index_2.getRangeIndexes(args.range);
            }
            var range = args.range = index_2.getSwapRange(args.range);
            if (!args.skipChecking) {
                this.mergedRange(args);
            }
            if (!args.merge || args.type === 'All') {
                this.mergeAll(args);
                if (args.refreshRibbon) {
                    this.parent.notify(index_3.activeCellChanged, null);
                }
            }
            else if (args.type === 'Horizontally') {
                for (var rowIdx = args.range[0], endIdx = args.range[2]; rowIdx <= endIdx; rowIdx++) {
                    args.range = [rowIdx, range[1], rowIdx, range[3]];
                    this.mergeAll(args, rowIdx - range[0]);
                }
            }
            else Eif (args.type === 'Vertically') {
                for (var colIdx = args.range[1], endIdx = args.range[3]; colIdx <= endIdx; colIdx++) {
                    args.range = [range[0], colIdx, range[2], colIdx];
                    this.mergeAll(args, 0, colIdx - range[1]);
                }
            }
            args.range = range;
            this.parent.setUsedRange(args.range[2], args.range[3]);
            if (args.isAction) {
                this.parent.notify('actionComplete', { eventArgs: args, action: 'merge' });
            }
            if (args.sheetIndex === this.parent.activeSheetIndex) {
                this.parent.notify('selectRange', { address: index_1.getSheet(this.parent, args.sheetIndex).selectedRange, skipChecking: true });
                if (this.parent.chartColl && this.parent.chartColl.length) {
                    this.parent.notify(index_4.refreshChart, { range: args.range });
                }
            }
        };
        WorkbookMerge.prototype.mergeAll = function (args, startRow, startCol) {
            if (startRow === void 0) { startRow = 0; }
            if (startCol === void 0) { startCol = 0; }
            var rowSpan = 0;
            var cell;
            args.range = args.range;
            var colSpan;
            var cellValue;
            var refreshAllCF;
            var format;
            var modelCell;
            var sheet = ej2_base_1.isUndefined(args.sheetIndex) ? this.parent.getActiveSheet() : index_1.getSheet(this.parent, args.sheetIndex);
            var updateObj = { cell: new Object(), rowIdx: args.range[0], colIdx: args.range[1], valChange: !args.merge,
                preventEvt: true, uiRefresh: !args.preventRefresh, skipFormatCheck: true };
            for (var rowIdx = args.range[0], rIdx = startRow; rowIdx <= args.range[2]; rowIdx++, rIdx++) {
                colSpan = 0;
                if (args.isAction && !args.model[rIdx]) {
                    args.model.push({ cells: [] });
                }
                for (var colIdx = args.range[1], cIdx = startCol; colIdx <= args.range[3]; colIdx++, cIdx++) {
                    cell = index_1.getCell(rowIdx, colIdx, sheet);
                    if (cell && (cell.value || cell.value === 0 || cell.formula) && !cellValue) {
                        cellValue = cell.formula || cell.value;
                        format = cell.format;
                    }
                    if (args.isAction && args.merge) {
                        modelCell = args.model[rIdx].cells[cIdx] = {};
                        ej2_base_1.extend(modelCell, cell, null, true);
                    }
                    if (cell) {
                        delete cell.rowSpan;
                        delete cell.colSpan;
                        modelCell = !args.merge && !args.isAction && args.model && args.model[rIdx] &&
                            args.model[rIdx].cells[cIdx];
                        if (modelCell) {
                            index_1.setCell(rowIdx, colIdx, sheet, modelCell);
                        }
                    }
                    if (rowIdx === args.range[0] && colIdx === args.range[1]) {
                        if (args.merge) {
                            if (args.range[3] - args.range[1] > 0) {
                                updateObj.cell.colSpan = (args.range[3] - args.range[1]) + 1;
                            }
                            if (args.range[2] - args.range[0] > 0) {
                                updateObj.cell.rowSpan = (args.range[2] - args.range[0]) + 1;
                            }
                            index_3.updateCell(this.parent, sheet, updateObj);
                            updateObj.valChange = updateObj.mergedCells = true;
                            continue;
                        }
                    }
                    else {
                        updateObj.rowIdx = rowIdx;
                        updateObj.colIdx = colIdx;
                        updateObj.cell = {};
                        if (args.merge) {
                            if (rowIdx !== args.range[0]) {
                                updateObj.cell.rowSpan = -rowSpan;
                            }
                            if (colIdx !== args.range[1]) {
                                colSpan++;
                                updateObj.cell.colSpan = -colSpan;
                            }
                        }
                    }
                    index_3.updateCell(this.parent, sheet, updateObj);
                    Eif (!refreshAllCF) {
                        refreshAllCF = updateObj.isFormulaDependent;
                    }
                }
                rowSpan++;
            }
            if (args.merge) {
                if (cellValue || cellValue === 0) {
                    delete updateObj.mergedCells;
                    updateObj.cell = {};
                    var curCell = index_1.getCell(args.range[0], args.range[1], sheet);
                    if (!curCell || (!curCell.value && !curCell.formula)) {
                        if (index_3.checkIsFormula(cellValue)) {
                            updateObj.cell.formula = cellValue;
                        }
                        else {
                            updateObj.cell.value = cellValue;
                        }
                        if (format) {
                            updateObj.cell.format = format;
                        }
                    }
                    updateObj.rowIdx = args.range[0];
                    updateObj.colIdx = args.range[1];
                    index_3.updateCell(this.parent, sheet, updateObj);
                }
                else if (!args.preventRefresh) {
                    this.parent.notify(index_2.applyMerge, { rowIdx: args.range[0], colIdx: args.range[1] });
                }
            }
            if (!args.preventRefresh) {
                this.refreshCF(sheet, args.range[0], args.range[1], refreshAllCF);
            }
        };
        WorkbookMerge.prototype.refreshCF = function (sheet, rowIdx, colIdx, refreshAll) {
            if (sheet.conditionalFormats && sheet.conditionalFormats.length) {
                this.parent.notify(index_3.applyCF, { indexes: [rowIdx, colIdx], refreshAll: refreshAll, isAction: true });
            }
        };
        WorkbookMerge.prototype.activeCellRange = function (args) {
            args.range = args.range;
            var sheet = this.parent.getActiveSheet();
            var cell = index_1.getCell(args.range[0], args.range[1], sheet);
            if (cell) {
                if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                    args.range[0] += cell.rowSpan;
                    if (args.insertCount) {
                        args.range[0] -= args.insertCount;
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                    args.range[1] += cell.colSpan;
                    if (args.insertCount) {
                        args.range[1] -= args.insertCount;
                    }
                }
                cell = index_1.getCell(args.range[0], args.range[1], sheet);
                if (cell) {
                    if (cell.rowSpan > 1 && (args.range[0] + (cell.rowSpan - 1) >= args.range[2] || args.insertCount)) {
                        args.range[2] = args.range[0] + (cell.rowSpan - 1);
                    }
                    if (cell.colSpan > 1 && (args.range[1] + (cell.colSpan - 1) >= args.range[3] || args.insertCount)) {
                        args.range[3] = args.range[1] + (cell.colSpan - 1);
                    }
                }
            }
        };
        WorkbookMerge.prototype.mergedRange = function (args) {
            Iif (typeof (args.range) === 'string') {
                args.range = index_2.getRangeIndexes(args.range);
            }
            if (args.range[0] <= args.range[2] && args.range[1] <= args.range[3]) {
                this.forward(args);
            }
            else if (args.range[0] >= args.range[2] && args.range[1] >= args.range[3]) {
                this.reverse(args);
            }
            else if (args.range[0] < args.range[2] && args.range[1] > args.range[3]) {
                this.forwardReverse(args);
            }
            else Eif (args.range[0] > args.range[2] && args.range[1] < args.range[3]) {
                this.reverseForward(args);
            }
        };
        WorkbookMerge.prototype.forward = function (args) {
            args.range = args.range;
            var sheet = ej2_base_1.isUndefined(args.sheetIndex) ? this.parent.getActiveSheet() : index_1.getSheet(this.parent, args.sheetIndex);
            var cell = index_1.getCell(args.range[0], args.range[1], sheet);
            var endRowIdx;
            var endColIdx;
            var rowIdx = endRowIdx = args.range[0];
            var colIdx = endColIdx = args.range[1];
            if (cell) {
                if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                    rowIdx = endRowIdx = args.range[0] + cell.rowSpan;
                }
                if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                    colIdx = endColIdx = args.range[1] + cell.colSpan;
                }
                cell = index_1.getCell(rowIdx, colIdx, sheet);
                Eif (cell) {
                    if (cell.rowSpan > 1) {
                        endRowIdx += (cell.rowSpan - 1);
                        if (rowIdx + (cell.rowSpan - 1) >= args.range[2]) {
                            args.range[2] = args.range[0];
                            args.range[2] = rowIdx + (cell.rowSpan - 1);
                        }
                    }
                    if (cell.colSpan > 1) {
                        endColIdx += (cell.colSpan - 1);
                        if (colIdx + (cell.colSpan - 1) >= args.range[3]) {
                            args.range[3] = args.range[1];
                            args.range[3] = colIdx + (cell.colSpan - 1);
                        }
                    }
                }
            }
            args.range[0] = rowIdx;
            args.range[1] = colIdx;
            if (args.range[0] === rowIdx && args.range[1] === colIdx && args.range[2] === endRowIdx && args.range[3] === endColIdx) {
                args.isActiveCell = true;
            }
            if (args.skipChecking) {
                return;
            }
            for (var i = args.range[1]; i <= args.range[3]; i++) {
                cell = index_1.getCell(args.range[2], i, sheet);
                if (cell) {
                    rowIdx = args.range[2];
                    colIdx = i;
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        colIdx += cell.colSpan;
                        if (colIdx < args.range[1]) {
                            args.range[1] = colIdx;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rowIdx += cell.rowSpan;
                        if (rowIdx < args.range[0]) {
                            args.range[0] = rowIdx;
                        }
                    }
                    cell = index_1.getCell(rowIdx, colIdx, sheet);
                    Eif (cell) {
                        if (cell.colSpan > 1 && colIdx + (cell.colSpan - 1) > args.range[3]) {
                            args.range[3] = colIdx;
                            args.range[3] = colIdx + (cell.colSpan - 1);
                        }
                        if (cell.rowSpan > 1 && rowIdx + (cell.rowSpan - 1) > args.range[2]) {
                            args.range[2] = rowIdx;
                            args.range[2] = rowIdx + (cell.rowSpan - 1);
                        }
                    }
                }
            }
            var startRowIdx;
            var startColIdx;
            for (var i = args.range[1]; i <= args.range[3]; i++) {
                cell = index_1.getCell(args.range[0], i, sheet);
                if (cell) {
                    startColIdx = i;
                    startRowIdx = args.range[0];
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        startColIdx += cell.colSpan;
                        Iif (startColIdx < args.range[1]) {
                            args.range[1] = startColIdx;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        startRowIdx += cell.rowSpan;
                        Eif (startRowIdx < args.range[0]) {
                            args.range[0] = startRowIdx;
                        }
                    }
                }
            }
            for (var i = args.range[0]; i <= args.range[2]; i++) {
                cell = index_1.getCell(i, args.range[3], sheet);
                if (cell) {
                    rowIdx = i;
                    colIdx = args.range[3];
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rowIdx += cell.rowSpan;
                        Iif (rowIdx < args.range[0]) {
                            args.range[0] = rowIdx;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        colIdx += cell.colSpan;
                        Iif (colIdx < args.range[1]) {
                            args.range[1] = colIdx;
                        }
                    }
                    cell = index_1.getCell(rowIdx, colIdx, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && rowIdx + (cell.rowSpan - 1) > args.range[2]) {
                            args.range[2] = rowIdx;
                            args.range[2] = rowIdx + (cell.rowSpan - 1);
                        }
                        if (cell.colSpan > 1 && colIdx + (cell.colSpan - 1) > args.range[3]) {
                            args.range[3] = colIdx;
                            args.range[3] = colIdx + (cell.colSpan - 1);
                        }
                    }
                }
            }
        };
        WorkbookMerge.prototype.forwardReverse = function (args) {
            var sheet = this.parent.getActiveSheet();
            args.range = args.range;
            var colIndex = args.range[1];
            var cell = index_1.getCell(args.range[0], args.range[1], sheet);
            var rowIndex = args.range[0];
            Eif (cell) {
                if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                    colIndex += cell.colSpan;
                    Eif (args.range[3] >= colIndex) {
                        args.range[3] = colIndex;
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                    rowIndex += cell.rowSpan;
                    Eif (rowIndex < args.range[0]) {
                        args.range[0] = rowIndex;
                    }
                }
                cell = index_1.getCell(rowIndex, colIndex, sheet);
                Eif (cell) {
                    if (cell.rowSpan > 1 && rowIndex + (cell.rowSpan - 1) >= args.range[2]) {
                        args.range[2] = rowIndex + (cell.rowSpan - 1);
                    }
                    if (cell.colSpan > 1 && colIndex + (cell.colSpan - 1) >= args.range[1]) {
                        args.range[1] = colIndex + (cell.colSpan - 1);
                    }
                }
            }
            args.range[0] = rowIndex;
            if (args.skipChecking) {
                return;
            }
            var rowIdx;
            var cellIdx;
            for (var i = args.range[3]; i <= args.range[1]; i++) {
                cell = index_1.getCell(args.range[2], i, sheet);
                Eif (cell) {
                    cellIdx = i;
                    rowIdx = args.range[2];
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rowIdx += cell.rowSpan;
                        Iif (rowIdx < args.range[0]) {
                            args.range[0] = rowIdx;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        cellIdx += cell.colSpan;
                        Iif (cellIdx < args.range[3]) {
                            args.range[3] = cellIdx;
                        }
                    }
                    cell = index_1.getCell(rowIdx, cellIdx, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && rowIdx + (cell.rowSpan - 1) > args.range[2]) {
                            args.range[2] = rowIdx + (cell.rowSpan - 1);
                        }
                        Iif (cell.colSpan > 1 && cellIdx + (cell.colSpan - 1) > args.range[1]) {
                            args.range[1] = cellIdx + (cell.colSpan - 1);
                        }
                    }
                }
            }
            var startRowIndex;
            for (var i = args.range[3]; i <= args.range[1]; i++) {
                cell = index_1.getCell(args.range[0], i, sheet);
                Eif (cell) {
                    cellIdx = i;
                    startRowIndex = args.range[0];
                    Iif (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        startRowIndex += cell.rowSpan;
                        if (startRowIndex < args.range[0]) {
                            args.range[0] = startRowIndex;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        cellIdx += cell.colSpan;
                    }
                    cell = index_1.getCell(startRowIndex, cellIdx, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && startRowIndex + (cell.rowSpan - 1) > args.range[2]) {
                            args.range[2] = startRowIndex + (cell.rowSpan - 1);
                        }
                        Iif (cell.colSpan > 1 && cellIdx + (cell.colSpan - 1) > args.range[1]) {
                            args.range[1] = cellIdx;
                            args.range[1] = cellIdx + (cell.colSpan - 1);
                        }
                    }
                }
            }
            var colIdx;
            for (var i = args.range[0]; i <= args.range[2]; i++) {
                cell = index_1.getCell(i, args.range[3], sheet);
                Eif (cell) {
                    startRowIndex = i;
                    colIdx = args.range[3];
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        startRowIndex += cell.rowSpan;
                        Iif (startRowIndex < args.range[0]) {
                            args.range[0] = startRowIndex;
                        }
                    }
                    Iif (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        colIdx += cell.colSpan;
                        if (colIdx < args.range[3]) {
                            args.range[3] = colIdx;
                        }
                    }
                    cell = index_1.getCell(startRowIndex, colIdx, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && startRowIndex + (cell.rowSpan - 1) > args.range[2]) {
                            args.range[2] = startRowIndex;
                            args.range[2] = startRowIndex + (cell.rowSpan - 1);
                        }
                        Iif (cell.colSpan > 1 && colIdx + (cell.colSpan - 1) > args.range[1]) {
                            args.range[1] = colIdx;
                            args.range[1] = colIdx + (cell.colSpan - 1);
                        }
                    }
                }
            }
        };
        WorkbookMerge.prototype.reverse = function (args) {
            args.range = args.range;
            var colnIdx = args.range[1];
            var sheet = ej2_base_1.isUndefined(args.sheetIndex) ? this.parent.getActiveSheet() : index_1.getSheet(this.parent, args.sheetIndex);
            var cell = index_1.getCell(args.range[0], args.range[1], sheet);
            var rowIdx = args.range[0];
            if (cell) {
                if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                    colnIdx += cell.colSpan;
                }
                if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                    rowIdx += cell.rowSpan;
                }
                if (args.range[2] >= rowIdx) {
                    args.range[2] = rowIdx;
                    args.isActiveCell = true;
                }
                if (args.range[3] >= colnIdx) {
                    args.range[3] = colnIdx;
                    if (args.range[2] === rowIdx) {
                        args.isActiveCell = true;
                    }
                }
                else if (args.isActiveCell) {
                    args.isActiveCell = false;
                }
                cell = index_1.getCell(rowIdx, colnIdx, sheet);
                Eif (cell) {
                    if (cell.rowSpan > 1 && rowIdx + (cell.rowSpan - 1) >= args.range[0]) {
                        args.range[0] = rowIdx;
                        args.range[0] = rowIdx + (cell.rowSpan - 1);
                    }
                    if (cell.colSpan > 1 && colnIdx + (cell.colSpan - 1) >= args.range[1]) {
                        args.range[1] = colnIdx;
                        args.range[1] = colnIdx + (cell.colSpan - 1);
                    }
                }
            }
            var colIdx = args.range[3];
            if (args.skipChecking) {
                return;
            }
            for (var i = args.range[3]; i <= args.range[1]; i++) {
                cell = index_1.getCell(args.range[2], i, sheet);
                if (cell) {
                    colIdx = i;
                    rowIdx = args.range[2];
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rowIdx += cell.rowSpan;
                        Eif (rowIdx < args.range[2]) {
                            args.range[2] = rowIdx;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        colIdx += cell.colSpan;
                        Iif (colIdx < args.range[3]) {
                            args.range[3] = colIdx;
                        }
                    }
                    cell = index_1.getCell(rowIdx, colIdx, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && rowIdx + (cell.rowSpan - 1) > args.range[0]) {
                            args.range[0] = rowIdx;
                            args.range[0] = rowIdx + (cell.rowSpan - 1);
                        }
                        Iif (cell.colSpan > 1 && colIdx + (cell.colSpan - 1) > args.range[1]) {
                            args.range[1] = colIdx;
                            args.range[1] = colIdx + (cell.colSpan - 1);
                        }
                    }
                }
            }
            colIdx = args.range[3];
            for (var i = args.range[3]; i <= args.range[1]; i++) {
                cell = index_1.getCell(args.range[0], i, sheet);
                if (cell) {
                    colIdx = i;
                    rowIdx = args.range[0];
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        colIdx += cell.colSpan;
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rowIdx += cell.rowSpan;
                    }
                    cell = index_1.getCell(rowIdx, colIdx, sheet);
                    Eif (cell) {
                        Iif (cell.colSpan > 1 && colIdx + (cell.colSpan - 1) > args.range[1]) {
                            args.range[1] = colIdx;
                            args.range[1] = colIdx + (cell.colSpan - 1);
                        }
                        Iif (cell.rowSpan > 1 && rowIdx + (cell.rowSpan - 1) > args.range[0]) {
                            args.range[0] = rowIdx;
                            args.range[0] = rowIdx + (cell.rowSpan - 1);
                        }
                    }
                }
            }
            var cellIndex;
            var rIdx;
            for (var i = args.range[2]; i <= args.range[0]; i++) {
                cell = index_1.getCell(i, args.range[3], sheet);
                if (cell) {
                    rIdx = i;
                    cellIndex = args.range[3];
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rIdx += cell.rowSpan;
                        Iif (rIdx < args.range[2]) {
                            args.range[2] = rIdx;
                        }
                    }
                    Iif (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        cellIndex += cell.colSpan;
                        if (cellIndex < args.range[3]) {
                            args.range[3] = cellIndex;
                        }
                    }
                    cell = index_1.getCell(rIdx, cellIndex, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && rIdx + (cell.rowSpan - 1) > args.range[0]) {
                            args.range[0] = rIdx;
                            args.range[0] = rIdx + (cell.rowSpan - 1);
                        }
                        if (cell.colSpan > 1 && cellIndex + (cell.colSpan - 1) > args.range[1]) {
                            args.range[1] = cellIndex;
                            args.range[1] = cellIndex + (cell.colSpan - 1);
                        }
                    }
                }
            }
        };
        WorkbookMerge.prototype.reverseForward = function (args) {
            args.range = args.range;
            var sheet = ej2_base_1.isUndefined(args.sheetIndex) ? this.parent.getActiveSheet() : index_1.getSheet(this.parent, args.sheetIndex);
            var rIdx = args.range[0];
            var cIdx = args.range[1];
            var cell = index_1.getCell(args.range[0], args.range[1], sheet);
            if (cell) {
                if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                    rIdx += cell.rowSpan;
                    if (args.range[2] >= rIdx) {
                        args.range[2] = rIdx;
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                    cIdx += cell.colSpan;
                }
                cell = index_1.getCell(rIdx, cIdx, sheet);
                Eif (cell) {
                    if (cell.rowSpan > 1 && rIdx + (cell.rowSpan - 1) >= args.range[0]) {
                        args.range[0] = rIdx;
                        args.range[0] = rIdx + (cell.rowSpan - 1);
                    }
                    if (cell.colSpan > 1 && cIdx + (cell.colSpan - 1) >= args.range[3]) {
                        args.range[3] = args.range[1];
                        args.range[3] = cIdx + (cell.colSpan - 1);
                    }
                }
            }
            if (args.skipChecking) {
                return;
            }
            var cIndex = args.range[3];
            var rIndex;
            for (var i = args.range[1]; i <= args.range[3]; i++) {
                cell = index_1.getCell(args.range[2], i, sheet);
                if (cell) {
                    rIndex = args.range[2];
                    cIndex = i;
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rIndex += cell.rowSpan;
                        Eif (rIndex < args.range[2]) {
                            args.range[2] = rIndex;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        cIndex += cell.colSpan;
                        Iif (cIndex < args.range[1]) {
                            args.range[1] = cIndex;
                        }
                    }
                    cell = index_1.getCell(rIndex, cIndex, sheet);
                    Eif (cell) {
                        if (cell.colSpan > 1 && cIndex + (cell.colSpan - 1) > args.range[3]) {
                            args.range[3] = cIndex + (cell.colSpan - 1);
                        }
                        Iif (cell.rowSpan > 1 && (cell.rowSpan - 1) + rIndex > args.range[0]) {
                            args.range[0] = (cell.rowSpan - 1) + rIndex;
                        }
                    }
                }
            }
            var sRowIdx;
            var sColIdx;
            for (var i = args.range[1]; i <= args.range[3]; i++) {
                cell = index_1.getCell(args.range[0], i, sheet);
                if (cell) {
                    sColIdx = i;
                    sRowIdx = args.range[0];
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        sColIdx += cell.colSpan;
                        if (sColIdx < args.range[1]) {
                            args.range[1] = sColIdx;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        sRowIdx += cell.rowSpan;
                    }
                    cell = index_1.getCell(sRowIdx, sColIdx, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && sRowIdx + (cell.rowSpan - 1) > args.range[0]) {
                            args.range[0] = sRowIdx + (cell.rowSpan - 1);
                        }
                    }
                }
            }
            var cellIndex;
            for (var i = args.range[2]; i <= args.range[0]; i++) {
                cell = index_1.getCell(i, args.range[3], sheet);
                if (cell) {
                    rIndex = i;
                    cellIndex = args.range[3];
                    if (!ej2_base_1.isNullOrUndefined(cell.rowSpan) && cell.rowSpan < 0) {
                        rIndex += cell.rowSpan;
                        Iif (rIndex < args.range[2]) {
                            args.range[2] = rIndex;
                        }
                    }
                    if (!ej2_base_1.isNullOrUndefined(cell.colSpan) && cell.colSpan < 0) {
                        cellIndex += cell.colSpan;
                        Iif (cellIndex < args.range[1]) {
                            args.range[1] = cellIndex;
                        }
                    }
                    cell = index_1.getCell(rIndex, cellIndex, sheet);
                    Eif (cell) {
                        Iif (cell.rowSpan > 1 && (cell.rowSpan - 1) + rIndex > args.range[0]) {
                            args.range[0] = (cell.rowSpan - 1) + rIndex;
                        }
                        if (cell.colSpan > 1 && (cell.colSpan - 1) + cellIndex > args.range[3]) {
                            args.range[3] = cellIndex;
                            args.range[3] = (cell.colSpan - 1) + cellIndex;
                        }
                    }
                }
            }
        };
        WorkbookMerge.prototype.insertHandler = function (args) {
            this.activeCellRange(args);
            args.range = args.range;
            if (args.insertModel === 'Row') {
                args.range[2] += args.insertCount;
            }
            else {
                args.range[3] += args.insertCount;
            }
            args.preventRefresh = true;
            args.merge = true;
            this.mergeAll(args);
        };
        WorkbookMerge.prototype.addEventListener = function () {
            this.parent.on(index_2.setMerge, this.merge, this);
            this.parent.on(index_2.mergedRange, this.mergedRange, this);
            this.parent.on(index_2.activeCellMergedRange, this.activeCellRange, this);
            this.parent.on(index_3.insertMerge, this.insertHandler, this);
        };
        WorkbookMerge.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookMerge.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_2.setMerge, this.merge);
                this.parent.off(index_2.mergedRange, this.mergedRange);
                this.parent.off(index_2.activeCellMergedRange, this.activeCellRange);
                this.parent.off(index_3.insertMerge, this.insertHandler);
            }
        };
        WorkbookMerge.prototype.getModuleName = function () {
            return 'workbookmerge';
        };
        return WorkbookMerge;
    }());
    exports.WorkbookMerge = WorkbookMerge;
});