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

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

Open
craigbrown opened this issue Aug 27, 2021 · 5 comments
Open

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

craigbrown opened this issue Aug 27, 2021 · 5 comments
Assignees
Labels
alt-enhancement alternative enchancement label for organization purposes

Comments

@craigbrown
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link

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
Copy link

Is there any ETA on this?

@OssianEPPlus OssianEPPlus added the alt-enhancement alternative enchancement label for organization purposes label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
alt-enhancement alternative enchancement label for organization purposes
Projects
None yet
Development

No branches or pull requests

4 participants