EPPlusSoftware / EPPlus

EPPlus-Excel spreadsheets for .NET
https://epplussoftware.com
Other
1.81k stars 277 forks source link

Range object's address is not updated after inserting cells #473

Open craigbrown opened 3 years ago

craigbrown commented 3 years ago

If I have a range that refers to B3, and then I insert cells, I expect that the range is updated to either C3 (if shifting right) or B4 (if shifting down). But the range object just stays pointing at B3.

Is that expected behaviour? It's different to how it works in VBA/Interop so just wanted to check whether this was intentional.

[TestMethod]
public void CheckRangeAddressAfterInsertingCells()
{
    using (var pck = new ExcelPackage())
    {
        // Create a worksheet
        var wks = pck.Workbook.Worksheets.Add("Sheet1");

        // Get a range
        var rng = wks.Cells["B3:B5"];
        Assert.AreEqual("B3:B5", rng.LocalAddress);

        // Insert cells to the left
        rng.Insert(eShiftTypeInsert.Right);

        // Check the range has shifted right
        Assert.AreEqual("C3:C5", rng.LocalAddress);
    }
}
JanKallman commented 3 years ago

No, the range is no updated when inserting or deleting in a range. As this is the current behavior we can't really change it, but we will look at returning the inserted/deleted range from the methods instead, as they are of declared void as of today. For example:

[TestMethod]
public void CheckRangeAddressAfterInsertingCells()
{
    using (var pck = new ExcelPackage())
    {
        // Create a worksheet
        var wks = pck.Workbook.Worksheets.Add("Sheet1");

        // Get a range
        var rng = wks.Cells["B3:B5"];
        Assert.AreEqual("B3:B5", rng.LocalAddress);

        // Insert cells to the left
        var insertedRng = rng.Insert(eShiftTypeInsert.Right);

        // Check the range has shifted right
        Assert.AreEqual("C3:C5", insertedRng.LocalAddress);
    }
}
craigbrown commented 3 years ago

OK - that wouldn't technically be the inserted range that you're returning though. The rng object currently automatically becomes the inserted range, and you'd be returning the range that rng was previously pointing to.

It would make more sense for the inserted range to be returned and rng to continue pointing to the same cells which have been shifted right. But if that's too much of a breaking change this is an ok compromise, although some might find it confusing.

JanKallman commented 3 years ago

Yes, insertedRng will be set to the range updated after the insert or delete operation. rng will still point to B3:B5. Otherwise we will have a breaking change.

SebastianCramer commented 3 years ago

Same effect applies when defining a ExcelNamedRange and then inserting a full row before that using sheet.InsertRow(). The Address Attribute of the range gets updated to the original Row +1. Start and End Attributes of the Range are not.

This is a breaking change compared to v5.6.4 which I used previously.

SebastianCramer commented 1 year ago

Is there any ETA on this?