Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix splicecolumn mergedcells #2625

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

roshanreacts
Copy link

Summary

I was facing an issue when we use spliceColumns when there are merged cells in the sliced column range. Which resulted in breaking the excel output as the row.slice was deleting the values of the merged cells and wasn't moving the other merged cells position as per the deleted columns.

Test plan

Create a sheet with 2 different merged cells in a row then try to add a spliceColumns line after that with out any insert. Let's say if the spliceColumn is in first merged range it will delete the merged cell and doesn't move the other merged cell range, as per the old code. This fix is applied at row.splice as this method is responsible for moving the cells when we call spliceColumns.

Related to source code (for typings update)

To fix the above issue I have filtered all the merged cells which start before the spliceColumn range and end after and also the merged cells which starts are ends after the column range (ignored the merged cells which starts and ends before the splice range as they don't get effected).

const mergeItems = this.worksheet._merges.filter(
      merge => merge.model.left > start || (merge.model.left < start && merge.model.right > start)
    );

The above code will pull the mergedCells from the sheet. Then only in case of remove cells we iterate through the merged Cells and move them count range. When there is no insert I have added a logic to move the merged cells.

mergeItems.forEach(mergeItem => {
        const {model, range} = mergeItem;
        const leftCol = model.left < start ? model.left : model.left - count;
        const rightCol = model.right - count;
        const topRow = model.top;
        const bottomRow = model.bottom;
        const newMergeRange = `${this.getColumn(leftCol).letter}${topRow}:${
          this.getColumn(rightCol).letter
        }${bottomRow}`;
        this.unMergeCells(range);
        if (model.left > start) {
          // Move cell value in the top left corner before merging
          const sourceCell = this.getCell(topRow, model.left);
          const targetCell = this.getCell(topRow, leftCol);
          targetCell.value = sourceCell.value;
          sourceCell.value = null;
          targetCell.style = sourceCell.style;
          sourceCell.style = {};
        }

        this.mergeCells(newMergeRange);
      });

Also ignored the row.splice if the dest cell is merged with the following code

if (cDst.isMerged) {
            return;
          }

Other than the above rest of all are the format changes from prettier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants