Repository navigation
feat(worksheet): insert and delete whole rows - #318
Conversation
insertRows / deleteRows move everything that carries a row position and rewrite every formula in the workbook that points at the edited sheet, following Excel's rules. Edits that would leave a wrong file are rejected before the first mutation. Closes #313 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MNFNHrh8yhz9PQ3bWTPBHo
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MNFNHrh8yhz9PQ3bWTPBHo
|
Thanks for taking this on, and so quickly. I ran this branch (858268c) on the template I described in #313. The new rows, the As far as I can see, the loader keeps the calc chain as a passthrough part, and the edit doesn't touch it. The chain lists formula cells by address, so once the totals move down it still names their old cells, which no longer hold formulas. Excel then offers to repair the file, with this log: Same result after const wb = await loadWorkbook(fromFile('reference/openpyxl/openpyxl/tests/data/genuine/sample.xlsx'));
const ws = getSheet(wb, 'Sheet3 - Formulas');
if (ws) insertRows(wb, ws, 1);
await saveWorkbook(wb, toFile('out.xlsx'));Since Excel rebuilds a missing chain, one option might be to drop it when rows are inserted or deleted: the part, its content-type override and its workbook relationship. You'll know better what fits the codebase. A smaller thing I noticed: a rewritten formula keeps its cached value, and Excel shows cached values as they are when it opens a file last saved by a recent Excel. I'd expect a |
|
@Riyooo Thank you for checking! This is a bit tricky to handle, so I really appreciate you testing it. I'll try to make some further adjustments based on your results. |
…s in row edits - Drop the calc chain and set fullCalcOnLoad, so Excel neither asks to repair the file nor shows stale cached results. - Move pivot tables kept as passthrough parts instead of refusing the edit. - Keep ranges ending at row 1048576 pinned on deletion. - Grow ranges covering a table's data body when rows land at its first data row or totals row. - Grow conditional formats and validations ending right above inserted rows, and split them where relative references no longer agree, as Excel does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for testing it on the real template. Fixed in 5300449. Calc chain: both edits now drop Cached values: both edits also set I also ran about 120 insert/delete cases through real Excel and compared its output with ours. That found a few more mismatches, now fixed: ranges ending at row 1048576 stay pinned on delete, ranges covering a table body grow when rows are inserted at its first data row or totals row, conditional formats and validations ending right above an insertion grow over it, and they split the way Excel splits them when relative references cross the edit. Pivot tables the model doesn't carry now move with their rows instead of making the edit throw. Every output opened in Excel without a repair prompt. |
|
That fixes it on my side. I re-ran 5300449 on the same template: the files open in Excel without the repair prompt, and the totals come out right after both |
Summary
Adds
insertRows(wb, ws, at, count?)anddeleteRows(wb, ws, at, count?)to@office-kit/xlsx/worksheet. They behave like Excel's Insert/Delete ▸ Sheet Rows: everything with a row position moves, and every formula in the workbook that points at the sheet is rewritten.Motivation
Filling templates whose sections grow or shrink at export time needs whole-row edits that keep
SUMranges, merges, row heights, print areas and defined names in step. Nothing in the public API does this today;copyRange/moveRangemove cells only. Spec agreed in #313 (comment).Closes #313
Changes
insertRows/deleteRows(src/worksheet/structure.ts).xm:sqref, modelled pivot tables and consolidation sources.xm:f. References move with their cells ($ones too), straddling ranges grow or shrink, deleted references become#REF!; whole-column, 3-D and external references are left alone. The rewrite splices the original text, so spacing and casing survive (src/formula/structure.ts).OpenXmlSchemaErrorbefore any change, and formula rewrites are computed before the first mutation. Rejected: cutting through an array formula / data table / pivot report, deleting a table's header or totals row, all its data rows or the whole table, deleting a pivot's whole source, a scenario changing cell or a consolidation source, pushing cells past row 1048576, and sheets with passthrough pivots or OLE objects / form controls.Testing
tests/formula/structure.test.ts: reference rewriting (anchors, ranges, flipped ranges, row ranges, quoting, 3-D / external / structured / names,#REF!, whitespace preserved).tests/worksheet/structure.test.ts: the issue's CV template (SUM, merges, heights, styles, print area, names), cross-sheet formulas, DV / CF / hyperlinks / notes, tables, charts / anchors / sparklines / pivots, shared and array formulas, re-anchored rule formulas, every rejection leaving the workbook unchanged, save +validateXlsx+ reload..xlsx/.xlsmfixtures in the repo: insert 3 rows at 2 and delete 2 rows at 2 on every sheet, save, validate. No save failures and no validation regressions; the only throws were the documented rejections.pnpm prepublishOnlypasses (API baseline updated for the two new exports).Breaking changes
None
Checklist
pnpm changesetand committedthe result.
marked accordingly.
comments.
PR represents real work that warrants a maintainer's review, and I am
willing to defend each line in review.
🤖 Generated with Claude Code
https://claude.ai/code/session_01MNFNHrh8yhz9PQ3bWTPBHo