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

95.07% Statements 405/426
85.4% Branches 275/322
95% Functions 19/20
95.07% Lines 405/426
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   886× 886×   415× 415× 415× 415× 101× 101× 101×     314×   415× 63×   415× 415×     415× 16×   415× 415×   415× 415× 415× 415× 415× 63× 63× 63×     413× 16× 16× 16×   413× 413× 413× 413× 13045× 13045× 13045× 13045×   413× 35× 36× 36× 36× 28× 28×   36× 36×     35×   413× 19× 19× 19× 25× 25× 25×     23×   25× 23×   25× 25× 25× 25×   25× 25× 25×     19×   413× 27× 77× 77× 77× 69× 69×   77× 77×     27×   413× 38× 88× 88× 88× 84× 84×   88× 88×     38×   413× 413× 413× 413× 301× 301× 301× 301× 301×   301× 301× 301× 301× 301× 2523× 2523×   2522× 2522× 2522× 12819×   12818× 299× 41× 41× 41×     258×     12818× 12818× 12818× 12818×     12810× 26× 56× 96× 39×   96×       12810× 10202×   12810× 12810× 112×   12698× 3549× 3549×     3549×       9149× 9149× 9149× 1334×             413× 41×   413× 413× 133×   413× 61× 61× 61×     234×         10241× 10241× 195× 195×   10241× 245× 245×   10241× 245× 245×     9996×   10241× 10241× 10241× 10241× 10241× 10241× 190× 190× 190×     10051×     21210× 21210× 10× 10×     1389× 1389×                   43× 43× 43× 43× 43× 43× 43× 43× 43× 43×     42× 42× 42× 42× 42× 42× 42×   36× 13×   23×   42×   42×       42× 65× 81× 42×   39× 39× 39× 15×     15×   24× 18×             10×             36×           42× 42× 42× 42×     16× 16× 14× 14× 14× 14× 14× 14× 14×   17× 17× 17× 17× 17× 17×     22× 22× 22×                                 22× 13×   22× 15×   22× 15×   22× 13×   22×       13× 13×   13×   13×         14× 14×   14× 14×           10578× 10578× 10578× 10578×     10578× 10578× 10444× 31× 31× 31× 31×   10444× 20×   20×   20×     10444×           23494× 1711×   21783×     23494× 11945×   23494× 12713×   23494×         886× 886× 886×   885× 13× 13× 13×     47× 47×   47×   47× 47× 47× 47× 47×   47× 47× 47× 47× 47× 47× 47× 203× 203× 203× 389× 389× 389× 375×   107× 107× 107× 107× 107× 107× 107×   14×   14× 14× 14× 14× 14× 14× 14× 14×       14×     249× 249× 249× 249× 249× 249× 20×     249×         47×     885× 885×   126650×        
define(["require", "exports", "../common/index", "../common/index", "../common/index", "../index", "../index", "../index"], function (require, exports, index_1, index_2, index_3, index_4, index_5, index_6) {
    "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.lastIndexOf('!') > -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;
            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 isReadonlyCell = index_1.isReadOnly(index_4.getCell(indexes[0], indexes[1], sheet), index_4.getColumn(sheet, indexes[1]), index_5.getRow(sheet, indexes[0]));
            var isReadonlyColumn = index_4.getColumn(sheet, indexes[1]) ? index_4.getColumn(sheet, indexes[1]).isReadOnly : false;
            var isReadonlyRow = index_5.getRow(sheet, indexes[0]) ? index_5.getRow(sheet, indexes[0]).isReadOnly : false;
            Object.assign(style, eventArgs.style, null, true);
            if (triggerEvt && !isReadonlyCell && !isReadonlyColumn && !isReadonlyRow) {
                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);
                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);
                    }
                }
                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];
                    }
                    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];
                    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 cell;
            if (Object.keys(eventArgs.style).length) {
                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;
                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);
                        var readonlyColumn = index_4.getColumn(sheet, j);
                        var readOnlyRow = index_5.getRow(sheet, i);
                        if ((cell && cell.isReadOnly) || (readonlyColumn && readonlyColumn.isReadOnly) ||
                            (readOnlyRow && readOnlyRow.isReadOnly)) {
                            continue;
                        }
                        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);
                        }
                        this.setCellStyle(sheet, i, j, eventArgs.style);
                        if (!activeSheet) {
                            continue;
                        }
                        if (uiRefresh && ((j >= viewport[1] && j <= viewport[3]) || j < frozenCol)) {
                            formatColor = null;
                            if (eventArgs.style.color && 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 });
                        }
                        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;
            }
            this.parent.setUsedRange(indexes[2], indexes[3]);
            if (args.refreshRibbon) {
                this.parent.notify(index_1.activeCellChanged, null);
            }
            if (triggerEvt && !isReadonlyCell && !isReadonlyColumn && !isReadonlyRow) {
                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) {
            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);
            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 isReadonlyCell = index_1.isReadOnly(index_4.getCell(indexes[0], indexes[1], sheet), index_4.getColumn(sheet, indexes[1]), index_5.getRow(sheet, indexes[0]));
            var isReadonlyColumn = index_4.getColumn(sheet, indexes[1]) ? index_4.getColumn(sheet, indexes[1]).isReadOnly : false;
            var isReadonlyRow = index_5.getRow(sheet, indexes[0]) ? index_5.getRow(sheet, indexes[0]).isReadOnly : false;
            Eif (!isReadonlyCell && !isReadonlyColumn && !isReadonlyRow) {
                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;
            var changedValue = value;
            var activeCellIndexes = index_1.getRangeIndexes(sheet.activeCell);
            var cellValue = this.parent.getCellStyleValue(['textDecoration'], activeCellIndexes).textDecoration;
            var changedStyle;
            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;
            }
            this.format({
                style: { textDecoration: changedValue }, 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;
                    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
                    });
                }
            }
            Eif (!isReadonlyCell && !isReadonlyColumn && !isReadonlyRow) {
                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) {
            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]);
                    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') {
                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]);
                    }
                }
            }
            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) {
            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
                });
            }
        };
        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 && !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;
                                break;
                            case 'Clear Contents':
                                if (cell.hyperlink) {
                                    this.parent.notify(index_3.deleteHyperlink, { sheet: sheet, rowIdx: sRowIdx, colIdx: sColIdx, preventRefresh: true });
                                }
                                isValExist = !!(cell.value || cell.formula);
                                delete cell.value;
                                delete cell.formula;
                                delete cell.formattedText;
                                Eif (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);
                                Eif (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;
});