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

Merge cells after row insert #1377

Merged
merged 3 commits into from
Oct 5, 2020
Merged

Merge cells after row insert #1377

merged 3 commits into from
Oct 5, 2020

Conversation

curtcommander
Copy link
Contributor

@curtcommander curtcommander commented Jul 7, 2020

Summary

Merges aren't preserved when cells containing merges get overwritten after row inserts. Instead, the value of a merged cell gets applied to all cells in the merge separately.

A1 and B1 C1
merged cell text

becomes

A1 B1 C1
merged cell text merged cell text

This is because the values for the previously merged cells are pulled from the source row (rSrc.values in spliceRows). The source row's values repeat the merged value for each cell in the merge. For example, the source row values for the row in the first table would be ["merged cell text", "merged cell text"]. These values are what gets applied to the cells in the destination row (rDst), and the merge is lost.

The extra lines I wrote in spliceRows to fix this reapply merges to cells while accounting for the offset caused by a row insert.

Test plan

I added a test to spec/unit/doc/worksheet.merge.spec for this issue and verified that it passes with the changes I made.

Copy link
Member

@Siemienik Siemienik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice done

@Siemienik Siemienik merged commit 834e893 into exceljs:master Oct 5, 2020
@calumpeak
Copy link

calumpeak commented Oct 13, 2020

@curtcommander @Siemienik does this also cover a case where merged cells below rows which have been inserted also exhibit the same behaviour (value of merged cell gets applied to all cells)? As this is a bit of a blocker currently - using pre-existing excel files as a template and editing them using excelJS, but the data preservation goes out the window on merged cells when I do :(

@curtcommander
Copy link
Contributor Author

@calumpeak Which version of exceljs are you using? The current latest version (4.2.0) preserves merges below rows that have been inserted, whereas 4.1.0 does not. I just tested this.

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.

3 participants