all files / workbook/actions/ cell-format.js

94.77% Statements 435/459
87.98% Branches 300/341
95% Functions 19/20
94.77% Lines 435/459
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   1053× 1053×   831× 831× 831× 831× 264× 264× 264×     567×   831× 73×   831× 831×     831× 19×   831× 831×   831× 831× 831×       825× 825× 75× 75× 75×     823× 19× 19× 19×   823× 823× 823× 823× 14508× 14508× 14508× 14508×   823× 98× 135× 135× 135× 54× 54×   135× 135×     98×   823× 20× 20× 20× 31× 31× 31×     29×   31× 29×   31× 31× 31× 31× 31× 31× 31×   31× 31× 31×     20×   823× 75× 125× 125× 125× 61× 61×   125× 125×     75×   823× 83× 142× 142× 142× 142× 142× 142× 82× 82×   142× 142×     83×   823× 823× 823× 823× 551× 551× 551× 551× 551× 551× 551× 551×   551× 551× 551× 551× 551× 551× 551× 3246× 3246×   3245× 3245× 3245× 14075×   14074× 549× 41× 41× 41×     508×     14074× 14074× 14074× 27× 58× 100× 39×   100×       14074× 10263×   14074× 14074× 112×   13962× 4221× 4221× 15× 15× 15×     4221×     4221× 352× 352×           9741× 9741× 9741× 1334×             823× 41×   823× 823× 823× 143×   823× 35×   823× 73× 73× 73×     248×         10302× 10302× 200× 200×   10302× 253× 253×   10302× 253× 253×     10049×   10302× 10302× 10302× 10302× 10302× 10302× 195× 195× 195×     10107×     21387× 21387×     1664× 1664×                   48× 48× 48× 48× 48×       48× 48× 48×   47× 47× 47× 47× 47× 47×   40× 15×   25×   47×   47× 47× 47× 191× 307× 47×   260× 260× 260× 216×     216×   44× 38×             30×             257×     47× 47× 47×   19× 19× 17× 17× 17× 17× 17× 17× 17×   21× 21× 21× 21× 21× 21×     10×     22× 22× 22×                                 22× 13×   22× 15×   22× 15×   22× 13×   22×       28× 28×   28×   28×         14× 14×   14× 14×           10875× 10875× 10875× 10875×     10875× 10875× 10741× 38× 38× 38× 38×   10741× 193×   193× 11×   193× 41×     10741×           25055× 1989×   23066×     25055× 12530×   25055× 13582×   25055×         1053× 1053× 1053×   1050× 14× 14× 14×     74× 74×   74×   74× 74× 74× 74× 74×   74× 74× 74× 74× 74× 74× 74× 1657× 1657× 1657× 22384× 22384× 22384× 12225×         12225× 34×     22350× 12372×   1214× 1214× 1214× 1214× 1214× 1214× 1214× 17×   1214×   165× 101× 101×   165× 165× 165× 165× 165× 125× 125× 125× 14× 14× 14×       165×   15× 15×   10977× 10977× 10977× 485× 485× 485× 20×     10977×         74×     1050× 1050×   150870×        
define(["require", "exports", "../common/index", "../common/index", "../common/index", "../index", "../index", "../index", "../index"], function (require, exports, index_1, index_2, index_3, index_4, index_5, index_6, index_7) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WorkbookCellFormat = (function () {
        function WorkbookCellFormat(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        WorkbookCellFormat.prototype.format = function (args) {
            var _this = this;
            var sheet;
            var rng = args.range;
            if (rng && typeof rng === 'string' && rng.indexOf('!') > -1) {
                var lastIndex = rng.lastIndexOf('!');
                rng = rng.substring(lastIndex + 1);
                sheet = this.parent.sheets[index_4.getSheetIndex(this.parent, args.range.substring(0, lastIndex))];
            }
            else {
                sheet = this.parent.getActiveSheet();
            }
            if (rng === undefined) {
                rng = sheet.selectedRange;
            }
            var triggerEvt = typeof (rng) !== 'object' && args.onActionUpdate && !args.isUndoRedo;
            var eventArgs = {
                range: rng, style: Object.assign({}, args.style), requestType: 'CellFormat'
            };
            if (args.borderType) {
                eventArgs.borderType = args.borderType;
            }
            var style = {};
            var indexes = typeof (eventArgs.range) === 'object' ? eventArgs.range :
                index_2.getSwapRange(index_1.getRangeIndexes(eventArgs.range));
            var mergeBorderRows = [];
            var hasReadOnlyCells = index_1.isReadOnlyCells(this.parent, indexes);
            if (hasReadOnlyCells) {
                Iif (args.onActionUpdate) {
                    this.parent.notify(index_1.workbookReadonlyAlert, null);
                }
                return;
            }
            Object.assign(style, eventArgs.style, null, true);
            if (triggerEvt) {
                this.parent.trigger('beforeCellFormat', eventArgs);
                this.parent.notify('actionBegin', { eventArgs: eventArgs, action: 'format' });
                if (eventArgs.cancel) {
                    args.cancel = true;
                    return;
                }
            }
            if (args.borderType) {
                this.setTypedBorder(sheet, args.style.border, indexes, args.borderType, args.onActionUpdate, mergeBorderRows);
                delete args.style.border;
                delete eventArgs.style.border;
            }
            var i;
            var j;
            var props = { cell: null, rowIdx: 0, colIdx: 0, eventOnly: true, preventEvt: !triggerEvt };
            var triggerBeforeEvent = function (cellStyle) {
                props.cell = { style: cellStyle };
                props.rowIdx = i;
                props.colIdx = j;
                return index_5.updateCell(_this.parent, sheet, props);
            };
            if (eventArgs.style.borderTop !== undefined) {
                for (j = indexes[1]; j <= indexes[3]; j++) {
                    i = indexes[0];
                    Eif (!triggerBeforeEvent({ borderTop: eventArgs.style.borderTop })) {
                        if (!args.isUndoRedo) {
                            this.checkAdjacentBorder(sheet, 'borderBottom', i - 1, j);
                            this.checkFullBorder(sheet, 'borderBottom', i - 1, j);
                        }
                        this.checkFullBorder(sheet, 'borderTop', i, j);
                        this.setCellBorder(sheet, props.cell.style, i, j, args.onActionUpdate, j === indexes[3], null, null, args.isUndoRedo, mergeBorderRows);
                    }
                }
                delete eventArgs.style.borderTop;
            }
            if (eventArgs.style.borderBottom !== undefined) {
                var firstCell = void 0;
                var lastCell = void 0;
                for (j = indexes[1]; j <= indexes[3]; j++) {
                    i = indexes[0];
                    firstCell = index_4.getCell(i, j, sheet, false, true);
                    if (firstCell.rowSpan > 0) {
                        lastCell = index_4.getCell(indexes[2], indexes[1], sheet, false, true);
                    }
                    else {
                        lastCell = index_4.getCell(indexes[2], indexes[3], sheet, false, true);
                    }
                    if (!(firstCell.rowSpan > 1 && lastCell.rowSpan < 0)) {
                        i = indexes[2];
                    }
                    var mergeArgs = { range: [i, j, i, j] };
                    this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                    i = mergeArgs.range[0];
                    Eif (!triggerBeforeEvent({ borderBottom: eventArgs.style.borderBottom })) {
                        Eif (!args.isUndoRedo) {
                            this.checkAdjacentBorder(sheet, 'borderTop', indexes[2] + 1, j);
                            this.checkFullBorder(sheet, 'borderTop', indexes[2] + 1, j);
                        }
                        this.checkFullBorder(sheet, 'borderBottom', indexes[2], j);
                        this.setCellBorder(sheet, props.cell.style, i, j, args.onActionUpdate, j === indexes[3], null, null, args.isUndoRedo);
                        this.setBottomBorderPriority(sheet, indexes[2], j);
                    }
                }
                delete eventArgs.style.borderBottom;
            }
            if (eventArgs.style.borderLeft !== undefined) {
                for (var i_1 = indexes[0]; i_1 <= indexes[2]; i_1++) {
                    j = indexes[1];
                    Eif (!triggerBeforeEvent({ borderLeft: eventArgs.style.borderLeft })) {
                        if (!args.isUndoRedo) {
                            this.checkAdjacentBorder(sheet, 'borderRight', i_1, j - 1);
                            this.checkFullBorder(sheet, 'borderRight', i_1, j - 1);
                        }
                        this.checkFullBorder(sheet, 'borderLeft', i_1, j);
                        this.setCellBorder(sheet, props.cell.style, i_1, j, args.onActionUpdate, null, null, null, args.isUndoRedo);
                    }
                }
                delete eventArgs.style.borderLeft;
            }
            if (eventArgs.style.borderRight !== undefined) {
                for (var i_2 = indexes[0]; i_2 <= indexes[2]; i_2++) {
                    j = indexes[3];
                    var mergeArgs = { range: [i_2, j, i_2, j] };
                    this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                    j = mergeArgs.range[1];
                    Eif (!triggerBeforeEvent({ borderRight: eventArgs.style.borderRight })) {
                        if (!args.isUndoRedo) {
                            this.checkAdjacentBorder(sheet, 'borderLeft', i_2, j + 1);
                            this.checkFullBorder(sheet, 'borderLeft', i_2, j + 1);
                        }
                        this.checkFullBorder(sheet, 'borderRight', i_2, j);
                        this.setCellBorder(sheet, props.cell.style, i_2, j, args.onActionUpdate, null, null, null, args.isUndoRedo);
                    }
                }
                delete eventArgs.style.borderRight;
            }
            var border;
            var isFullBorder;
            var styleKeys = Object.keys(eventArgs.style);
            if (styleKeys.length) {
                var cell = void 0;
                var validation = void 0;
                var col = void 0;
                var parent_1 = this.parent;
                var activeSheet = parent_1.viewport && this.parent.getActiveSheet().id === sheet.id;
                var frozenRow = this.parent.frozenRowCount(sheet);
                var frozenCol = this.parent.frozenColCount(sheet);
                var viewport = [frozenRow + parent_1.viewport.topIndex, frozenCol + parent_1.viewport.leftIndex,
                    parent_1.viewport.bottomIndex, parent_1.viewport.rightIndex];
                var uiRefresh = void 0;
                var row = void 0;
                var checkHeight = void 0;
                var formatColor = void 0;
                var isFontColorApplied = styleKeys.indexOf('color') > -1;
                var isColorApplied = isFontColorApplied || styleKeys.indexOf('backgroundColor') > -1;
                for (i = indexes[0]; i <= indexes[2]; i++) {
                    row = index_5.getRow(sheet, i) || {};
                    if (row.isFiltered) {
                        continue;
                    }
                    uiRefresh = (i >= viewport[0] && i <= viewport[2]) || i < frozenRow;
                    checkHeight = false;
                    for (j = indexes[1]; j <= indexes[3]; j++) {
                        if (triggerBeforeEvent(eventArgs.style)) {
                            continue;
                        }
                        if (isFullBorder === undefined) {
                            if (eventArgs.style.border !== undefined) {
                                border = eventArgs.style.border;
                                delete eventArgs.style.border;
                                isFullBorder = true;
                            }
                            else {
                                isFullBorder = false;
                            }
                        }
                        cell = index_4.getCell(i, j, sheet, false, true);
                        col = sheet.columns[j];
                        if (cell.rowSpan > 1 || cell.colSpan > 1) {
                            for (var k = i, rowSpanLen = cell.rowSpan > 1 ? i + (cell.rowSpan - 1) : i; k <= rowSpanLen; k++) {
                                for (var l = j, colSpanLen = cell.colSpan > 1 ? j + (cell.colSpan - 1) : j; l <= colSpanLen; l++) {
                                    if (isFullBorder) {
                                        this.setFullBorder(sheet, border, indexes, k, l, args.onActionUpdate, true);
                                    }
                                    this.setCellStyle(sheet, k, l, eventArgs.style);
                                }
                            }
                        }
                        if (isFullBorder) {
                            this.setFullBorder(sheet, border, indexes, i, j, args.onActionUpdate, undefined, mergeBorderRows);
                        }
                        this.setCellStyle(sheet, i, j, eventArgs.style);
                        if (!activeSheet) {
                            continue;
                        }
                        if (uiRefresh && ((j >= viewport[1] && j <= viewport[3]) || j < frozenCol)) {
                            formatColor = null;
                            if (isFontColorApplied && cell.format && cell.format.includes('[')) {
                                var colorCode = index_6.getColorCode(cell.format);
                                Eif (colorCode) {
                                    formatColor = colorCode.toLowerCase();
                                }
                            }
                            this.parent.notify(index_1.applyCellFormat, { style: eventArgs.style, rowIdx: i, colIdx: j,
                                lastCell: j === indexes[3], isHeightCheckNeeded: true, manualUpdate: true, onActionUpdate: args.onActionUpdate,
                                formatColor: formatColor });
                            if (isColorApplied) {
                                validation = cell.validation || (index_7.checkColumnValidation(col, i, j) && col.validation);
                                if (validation && validation.isHighlighted) {
                                    this.parent.notify(index_7.updateHighlight, {
                                        rowIdx: i, colIdx: j, cell: cell, validation: validation, col: cell.validation && col
                                    });
                                }
                            }
                        }
                        else Eif (!row.customHeight) {
                            checkHeight = checkHeight || index_5.isHeightCheckNeeded(eventArgs.style, args.onActionUpdate);
                            if (checkHeight) {
                                this.parent.notify(index_1.applyCellFormat, { rowIdx: i, colIdx: j, lastCell: j === indexes[3], checkHeight: true,
                                    outsideViewport: !uiRefresh, onActionUpdate: args.onActionUpdate });
                            }
                        }
                    }
                }
            }
            if (isFullBorder) {
                eventArgs.style.border = border;
            }
            index_1.updateMergeBorder(this.parent, mergeBorderRows, [indexes[1], indexes[3]]);
            this.parent.setUsedRange(indexes[2], indexes[3]);
            if (args.refreshRibbon) {
                this.parent.notify(index_1.activeCellChanged, null);
            }
            if (sheet.conditionalFormats && sheet.conditionalFormats.length) {
                this.parent.notify(index_6.applyCF, { indexes: indexes });
            }
            if (triggerEvt) {
                eventArgs.style = style;
                eventArgs.range = sheet.name + "!" + rng;
                this.parent.notify('actionComplete', { eventArgs: eventArgs, action: 'format' });
            }
        };
        WorkbookCellFormat.prototype.setBottomBorderPriority = function (sheet, rowIdx, colIdx) {
            if (index_4.isHiddenRow(sheet, rowIdx + 1)) {
                var pIdx = this.skipHiddenRows(sheet, rowIdx + 1);
                var pCellStyle = this.parent.getCellStyleValue(['borderTop'], [pIdx, colIdx]).borderTop;
                Iif (pCellStyle !== '') {
                    sheet.rows[rowIdx].cells[colIdx].style.bottomPriority = true;
                }
            }
        };
        WorkbookCellFormat.prototype.setFullBorder = function (sheet, border, indexes, i, j, actionUpdate, modelUpdate, mergeBorderRows) {
            var style = {};
            if (i === indexes[0]) {
                this.checkAdjacentBorder(sheet, 'borderBottom', i - 1, j);
                this.checkFullBorder(sheet, 'borderBottom', i - 1, j);
            }
            if (j === indexes[1]) {
                this.checkAdjacentBorder(sheet, 'borderRight', i, j - 1);
                this.checkFullBorder(sheet, 'borderRight', i, j - 1);
            }
            if (j === indexes[3]) {
                this.checkAdjacentBorder(sheet, 'borderLeft', i, j + 1);
                this.checkFullBorder(sheet, 'borderLeft', i, j + 1);
            }
            else {
                this.checkAdjacentBorder(sheet, 'border', i, j + 1);
            }
            style.borderRight = border;
            style.borderTop = border;
            style.borderLeft = border;
            style.borderBottom = border;
            this.setCellBorder(sheet, style, i, j, actionUpdate, j === indexes[3], null, modelUpdate, undefined, mergeBorderRows);
            if (i === indexes[2]) {
                this.checkAdjacentBorder(sheet, 'borderTop', i + 1, j);
                this.checkFullBorder(sheet, 'borderTop', i + 1, j);
                this.setBottomBorderPriority(sheet, i, j);
            }
            else {
                this.checkAdjacentBorder(sheet, 'border', i + 1, j);
            }
        };
        WorkbookCellFormat.prototype.checkAdjacentBorder = function (sheet, prop, rowIdx, colIdx) {
            var style = {};
            if (this.parent.getCellStyleValue([prop], [rowIdx, colIdx])["" + prop] !== '') {
                style["" + prop] = undefined;
                this.setCellStyle(sheet, rowIdx, colIdx, style);
            }
        };
        WorkbookCellFormat.prototype.checkFullBorder = function (sheet, prop, rowIdx, colIdx) {
            var border = this.parent.getCellStyleValue(['border'], [rowIdx, colIdx]).border;
            Iif (border !== '') {
                var style_1 = { border: undefined };
                ['borderBottom', 'borderTop', 'borderLeft', 'borderRight'].forEach(function (value) {
                    if (value !== prop) {
                        style_1["" + value] = border;
                    }
                });
                this.setCellStyle(sheet, rowIdx, colIdx, style_1);
            }
        };
        WorkbookCellFormat.prototype.textDecorationActionUpdate = function (args) {
            var sheet = this.parent.getActiveSheet();
            var eventArgs = { range: sheet.selectedRange, style: args.style, requestType: 'CellFormat' };
            var indexes = index_2.getSwapRange(index_1.getRangeIndexes(sheet.selectedRange));
            var hasReadOnlyCells = index_1.isReadOnlyCells(this.parent, indexes);
            Iif (hasReadOnlyCells) {
                this.parent.notify(index_1.workbookReadonlyAlert, null);
                return;
            }
            this.parent.trigger('beforeCellFormat', eventArgs);
            this.parent.notify('actionBegin', { eventArgs: eventArgs, action: 'format' });
            if (eventArgs.cancel) {
                args.cancel = true;
                return;
            }
            var value = args.style.textDecoration.toLowerCase();
            var changedValue = value;
            var activeCellIndexes = index_1.getRangeIndexes(sheet.activeCell);
            var cellValue = this.parent.getCellStyleValue(['textDecoration'], activeCellIndexes).textDecoration.toLowerCase();
            var removeProp = false;
            if (cellValue === 'underline') {
                changedValue = value === 'underline' ? 'none' : 'underline line-through';
            }
            else if (cellValue === 'line-through') {
                changedValue = value === 'line-through' ? 'none' : 'underline line-through';
            }
            else if (cellValue === 'underline line-through') {
                changedValue = value === 'underline' ? 'line-through' : 'underline';
                removeProp = true;
            }
            if (changedValue === 'none') {
                removeProp = true;
            }
            var changedStyle = { textDecoration: changedValue };
            this.format({ style: changedStyle, range: activeCellIndexes, refreshRibbon: args.refreshRibbon, onActionUpdate: true });
            for (var i = indexes[0]; i <= indexes[2]; i++) {
                for (var j = indexes[1]; j <= indexes[3]; j++) {
                    if (i === activeCellIndexes[0] && j === activeCellIndexes[1]) {
                        continue;
                    }
                    changedStyle = {};
                    cellValue = this.parent.getCellStyleValue(['textDecoration'], [i, j]).textDecoration.toLowerCase();
                    if (cellValue === 'none') {
                        Iif (removeProp) {
                            continue;
                        }
                        changedStyle.textDecoration = value;
                    }
                    else if (cellValue === 'underline' || cellValue === 'line-through') {
                        if (removeProp) {
                            Eif (value === cellValue) {
                                changedStyle.textDecoration = 'none';
                            }
                            else {
                                continue;
                            }
                        }
                        else {
                            changedStyle.textDecoration = value !== cellValue ? 'underline line-through' : value;
                        }
                    }
                    else Eif (cellValue === 'underline line-through') {
                        if (removeProp) {
                            changedStyle.textDecoration = value === 'underline' ? 'line-through' : 'underline';
                        }
                        else {
                            continue;
                        }
                    }
                    this.format({ style: changedStyle, range: [i, j, i, j], refreshRibbon: args.refreshRibbon, onActionUpdate: true });
                }
            }
            eventArgs.range = sheet.name + '!' + eventArgs.range;
            eventArgs.style.textDecoration = changedValue;
            this.parent.notify('actionComplete', { eventArgs: eventArgs, action: 'format' });
        };
        WorkbookCellFormat.prototype.setTypedBorder = function (sheet, border, range, type, actionUpdate, mergeBorderRows) {
            var prevBorder;
            if (type === 'Outer') {
                for (var colIdx = range[1]; colIdx <= range[3]; colIdx++) {
                    this.checkAdjacentBorder(sheet, 'borderBottom', range[0] - 1, colIdx);
                    this.checkFullBorder(sheet, 'borderBottom', range[0] - 1, colIdx);
                    this.setCellBorder(sheet, { borderTop: border }, range[0], colIdx, actionUpdate, colIdx === range[3], undefined, undefined, undefined, mergeBorderRows);
                    this.checkAdjacentBorder(sheet, 'borderTop', range[2] + 1, colIdx);
                    this.checkFullBorder(sheet, 'borderTop', range[2] + 1, colIdx);
                    this.setCellBorder(sheet, { borderBottom: border }, range[2], colIdx, actionUpdate, colIdx === range[3], type);
                    this.setBottomBorderPriority(sheet, range[2], colIdx);
                }
                for (var rowIdx = range[0]; rowIdx <= range[2]; rowIdx++) {
                    this.checkAdjacentBorder(sheet, 'borderRight', rowIdx, range[1] - 1);
                    this.checkFullBorder(sheet, 'borderRight', rowIdx, range[1] - 1);
                    this.setCellBorder(sheet, { borderLeft: border }, rowIdx, range[1], actionUpdate);
                    this.checkAdjacentBorder(sheet, 'borderLeft', rowIdx, range[3] + 1);
                    this.checkFullBorder(sheet, 'borderLeft', rowIdx, range[3] + 1);
                    this.setCellBorder(sheet, { borderRight: border }, rowIdx, range[3], actionUpdate, null, type);
                }
            }
            else if (type === 'Inner') {
                var mergeArgs = { range: [range[0], range[1], range[0], range[1]] };
                this.parent.notify(index_1.mergedRange, mergeArgs);
                if (mergeArgs.range[0] === range[0] && mergeArgs.range[1] === range[1] &&
                    mergeArgs.range[2] === range[2] && mergeArgs.range[3] === range[3]) {
                    return;
                }
                for (var i = range[0]; i <= range[2]; i++) {
                    for (var j = range[1]; j <= range[3]; j++) {
                        var style = {};
                        prevBorder = this.parent.getCellStyleValue(['border'], [i, j]).border;
                        Iif (prevBorder !== '') {
                            style.border = undefined;
                            if (j === range[3] || j === range[1] || i === range[0] || i === range[2]) {
                                if (i === range[0]) {
                                    style.borderTop = prevBorder;
                                }
                                if (i === range[2]) {
                                    style.borderBottom = prevBorder;
                                }
                                if (j === range[3]) {
                                    style.borderRight = prevBorder;
                                }
                                if (j === range[1]) {
                                    style.borderLeft = prevBorder;
                                }
                            }
                        }
                        if (j !== range[3]) {
                            style.borderRight = border;
                        }
                        if (i !== range[0]) {
                            style.borderTop = border;
                        }
                        if (i !== range[2]) {
                            style.borderBottom = border;
                        }
                        if (j !== range[1]) {
                            style.borderLeft = border;
                        }
                        this.setCellBorder(sheet, style, i, j, actionUpdate, j === range[3], undefined, undefined, undefined, mergeBorderRows);
                    }
                }
            }
            else if (type === 'Vertical') {
                for (var i = range[0]; i <= range[2]; i++) {
                    for (var j = range[1]; j <= range[3]; j++) {
                        var style = { borderRight: border, borderLeft: border };
                        if (j === range[1]) {
                            this.checkAdjacentBorder(sheet, 'borderRight', i, j - 1);
                            this.checkFullBorder(sheet, 'borderRight', i, j - 1);
                        }
                        if (j === range[3]) {
                            this.checkAdjacentBorder(sheet, 'borderLeft', i, j + 1);
                            this.checkFullBorder(sheet, 'borderLeft', i, j + 1);
                        }
                        this.setCellBorder(sheet, style, i, j, actionUpdate);
                    }
                }
            }
            else {
                for (var i = range[0]; i <= range[2]; i++) {
                    for (var j = range[1]; j <= range[3]; j++) {
                        var style = { borderTop: border, borderBottom: border };
                        if (i === range[0]) {
                            this.checkAdjacentBorder(sheet, 'borderBottom', i - 1, j);
                            this.checkFullBorder(sheet, 'borderBottom', i - 1, j);
                        }
                        this.setCellBorder(sheet, style, i, j, actionUpdate, j === range[3]);
                        if (i === range[2]) {
                            this.checkAdjacentBorder(sheet, 'borderTop', i + 1, j);
                            this.checkFullBorder(sheet, 'borderTop', i + 1, j);
                            this.setBottomBorderPriority(sheet, i, j);
                        }
                    }
                }
            }
        };
        WorkbookCellFormat.prototype.setCellBorder = function (sheet, style, rowIdx, colIdx, actionUpdate, lastCell, type, modelUpdate, isUndoRedo, mergeBorderRows) {
            var cell = index_4.getCell(rowIdx, colIdx, sheet);
            var column = index_4.getColumn(sheet, colIdx);
            var row = index_5.getRow(sheet, rowIdx);
            Iif ((cell && cell.isReadOnly) || (column && column.isReadOnly) || (row && row.isReadOnly)) {
                return;
            }
            this.setCellStyle(sheet, rowIdx, colIdx, style);
            if (!modelUpdate && this.parent.getActiveSheet().id === sheet.id) {
                if (type === 'Outer' && (style.borderBottom || style.borderRight)) {
                    var mergeArgs = { range: [rowIdx, colIdx, rowIdx, colIdx] };
                    this.parent.notify(index_3.activeCellMergedRange, mergeArgs);
                    rowIdx = mergeArgs.range[0];
                    colIdx = mergeArgs.range[1];
                }
                if (isUndoRedo) {
                    if (style.borderTop === '' && this.parent.getCellStyleValue(['borderBottom'], [rowIdx - 1, colIdx]).borderBottom !== '') {
                        style.borderTop = this.parent.getCellStyleValue(['borderBottom'], [rowIdx - 1, colIdx]).borderBottom;
                    }
                    if (style.borderLeft === '' && this.parent.getCellStyleValue(['borderRight'], [rowIdx, colIdx - 1]).borderRight !== '') {
                        style.borderLeft = this.parent.getCellStyleValue(['borderRight'], [rowIdx, colIdx - 1]).borderRight;
                    }
                    if (style.borderRight === '' && this.parent.getCellStyleValue(['borderLeft'], [rowIdx, colIdx + 1]).borderLeft !== '') {
                        style.borderRight = this.parent.getCellStyleValue(['borderLeft'], [rowIdx, colIdx + 1]).borderLeft;
                    }
                }
                this.parent.notify(index_1.applyCellFormat, {
                    style: style, rowIdx: rowIdx, colIdx: colIdx, onActionUpdate: actionUpdate, first: '', lastCell: lastCell,
                    isHeightCheckNeeded: true, manualUpdate: true, mergeBorderRows: mergeBorderRows
                });
            }
        };
        WorkbookCellFormat.prototype.setCellStyle = function (sheet, rowIdx, colIdx, style) {
            if (!sheet.rows[rowIdx]) {
                sheet.rows[rowIdx] = { cells: [] };
            }
            else Iif (!sheet.rows[rowIdx].cells) {
                sheet.rows[rowIdx].cells = [];
            }
            if (!sheet.rows[rowIdx].cells[colIdx]) {
                sheet.rows[rowIdx].cells[colIdx] = {};
            }
            if (!sheet.rows[rowIdx].cells[colIdx].style) {
                sheet.rows[rowIdx].cells[colIdx].style = {};
            }
            Object.assign(sheet.rows[rowIdx].cells[colIdx].style, style, null, true);
        };
        WorkbookCellFormat.prototype.skipHiddenRows = function (sheet, startIdx) {
            startIdx++;
            Iif (index_4.isHiddenRow(sheet, startIdx)) {
                startIdx = this.skipHiddenRows(sheet, startIdx);
            }
            return startIdx;
        };
        WorkbookCellFormat.prototype.addEventListener = function () {
            this.parent.on(index_1.setCellFormat, this.format, this);
            this.parent.on(index_2.textDecorationUpdate, this.textDecorationActionUpdate, this);
            this.parent.on(index_3.clear, this.clearCellObj, this);
        };
        WorkbookCellFormat.prototype.removeEventListener = function () {
            if (!this.parent.isDestroyed) {
                this.parent.off(index_1.setCellFormat, this.format);
                this.parent.off(index_2.textDecorationUpdate, this.textDecorationActionUpdate);
                this.parent.off(index_3.clear, this.clearCellObj);
            }
        };
        WorkbookCellFormat.prototype.clearCellObj = function (options) {
            var lastIndex = options.range ? options.range.lastIndexOf('!') : 0;
            var clrRange = options.range ? (lastIndex > 0) ? options.range.substring(lastIndex + 1) : options.range
                : this.parent.getActiveSheet().selectedRange;
            var sheetIdx = (options.range && lastIndex > 0) ?
                index_4.getSheetIndex(this.parent, options.range.substring(0, lastIndex)) : this.parent.activeSheetIndex;
            var sheet = index_4.getSheet(this.parent, sheetIdx);
            var range = index_2.getSwapRange(index_3.getIndexesFromAddress(clrRange));
            var sRowIdx = range[0];
            var eRowIdx = range[2];
            var cf = sheet.conditionalFormats && sheet.conditionalFormats.length &&
                [].slice.call(sheet.conditionalFormats);
            var cfRule = [];
            var cfRefreshAll;
            var evtArgs;
            var sColIdx;
            var eColIdx;
            var isValExist;
            for (sRowIdx; sRowIdx <= eRowIdx; sRowIdx++) {
                sColIdx = range[1];
                eColIdx = range[3];
                for (sColIdx; sColIdx <= eColIdx; sColIdx++) {
                    var cell = index_4.getCell(sRowIdx, sColIdx, sheet);
                    var isReadonlyCell = index_1.isReadOnly(cell, index_4.getColumn(sheet, sColIdx), index_5.getRow(sheet, sRowIdx));
                    if (cell && (options.type === 'Clear All' || options.type === 'Clear Formats')) {
                        if (cell.rowSpan > 1 || cell.colSpan > 1) {
                            var mergeArgs = { range: [sRowIdx, sColIdx, sRowIdx, sColIdx] };
                            this.parent.notify(index_1.mergedRange, mergeArgs);
                            var mergedRanges = mergeArgs.range;
                            if (range[0] <= mergedRanges[0] && range[1] <= mergedRanges[1] && range[2] >= mergedRanges[2] &&
                                range[3] >= mergedRanges[3]) {
                                this.parent.notify(index_1.setMerge, { merge: false, range: mergedRanges, type: 'All', sheetIndex: sheetIdx,
                                    preventRefresh: sheetIdx !== this.parent.activeSheetIndex });
                            }
                        }
                        if (!!cell.rowSpan && cell.rowSpan !== 1 || !!cell.colSpan && cell.colSpan !== 1) {
                            continue;
                        }
                    }
                    if (cell && !isReadonlyCell) {
                        switch (options.type) {
                            case 'Clear Formats':
                                delete cell.format;
                                delete cell.rowSpan;
                                delete cell.style;
                                delete cell.wrap;
                                delete cell.colSpan;
                                delete cell.formattedText;
                                if (cell.hyperlink) {
                                    cell.style = { textDecoration: 'none', color: 'inherit' };
                                }
                                break;
                            case 'Clear Contents':
                                if (cell.hyperlink) {
                                    this.parent.notify(index_3.deleteHyperlink, { sheet: sheet, rowIdx: sRowIdx, colIdx: sColIdx, preventRefresh: true });
                                    cell.style = { textDecoration: 'underline', color: '#00e' };
                                }
                                isValExist = !!(cell.value || cell.formula);
                                delete cell.value;
                                delete cell.formula;
                                delete cell.formattedText;
                                if (isValExist) {
                                    evtArgs = { action: 'refreshCalculate', rowIndex: sRowIdx, colIndex: sColIdx, sheetIndex: sheetIdx };
                                    this.parent.notify(index_5.workbookFormulaOperation, evtArgs);
                                    if (cf && !cfRefreshAll) {
                                        cfRefreshAll = evtArgs.isFormulaDependent;
                                        Eif (!cfRefreshAll) {
                                            index_4.updateCFModel(cf, cfRule, sRowIdx, sColIdx);
                                        }
                                    }
                                }
                                break;
                            case 'Clear Hyperlinks':
                                delete cell.hyperlink;
                                break;
                            case 'Clear All':
                                isValExist = !!(cell.value || cell.formula);
                                index_4.setCell(sRowIdx, sColIdx, sheet, {}, false);
                                if (isValExist) {
                                    evtArgs = { action: 'refreshCalculate', rowIndex: sRowIdx, colIndex: sColIdx, sheetIndex: sheetIdx };
                                    this.parent.notify(index_5.workbookFormulaOperation, evtArgs);
                                    if (cf && !cfRefreshAll) {
                                        cfRefreshAll = evtArgs.isFormulaDependent;
                                    }
                                }
                                break;
                        }
                    }
                }
            }
            if ((cfRule.length || cfRefreshAll) && sheetIdx === this.parent.activeSheetIndex) {
                this.parent.notify(index_6.applyCF, { cfModel: !cfRefreshAll && cfRule, refreshAll: cfRefreshAll, isAction: true });
            }
        };
        WorkbookCellFormat.prototype.destroy = function () {
            this.removeEventListener();
            this.parent = null;
        };
        WorkbookCellFormat.prototype.getModuleName = function () {
            return 'workbookcellformat';
        };
        return WorkbookCellFormat;
    }());
    exports.WorkbookCellFormat = WorkbookCellFormat;
});