InsertColumns() does not update named ranges

Hi,



I have noticed that InsertColumns() does not update named ranges when it is executed in a named range.



For example, I have a named range in A1:B2 and when InsertColumns() is called for column B, the named range’s column count is still 2.



On the other hand, if InsertRows() is called instead for row 2, the named range’s row count becomes 3.



The behavior between InsertRows() and InsertColumns() is inconsistent and InsertColumns() should update name ranges’ properties when it inserts a column with-in them.



Here’s a sample snippet:



var grid = (new Workbook()).Worksheets[0];



var range = grid.Cells.CreateRange(“A1”, “B2”);



range.Name = “TestNamedRange”;



var lastRowIndex = range.FirstRow + range.RowCount - 1;

var lastColumnIndex = range.FirstColumn + range.ColumnCount - 1;

Console.WriteLine(“RowCount before InsertRows(): {0}”, range.RowCount);

grid.Cells.InsertRows(lastRowIndex, 1);

Console.WriteLine(“RowCount after InsertRows(): {0}”, range.RowCount);



Console.WriteLine(“ColumnCount before InsertColumns(): {0}”, range.ColumnCount);

grid.Cells.InsertColumns(lastColumnIndex, 1);

Console.WriteLine(“ColumnCount after InsertColumns(): {0}”, range.ColumnCount); // ColumnCount should be 3 instead of 2



-Steve

Hi Steve,

Cells.CreateRange just creates a snapshot of the range.

Please get the correct range with Workbook.Worksheets.GetRangeByName method again after you insert/delete Rows/Columns.See following codes:

Workbook workbook = new Workbook();
Cells cells = workbook.Worksheets[0].Cells;
Range range = cells.CreateRange("A1", "B2");
range.Name = "TestNamedRange";
cells.InsertColumn(1);
range = workbook.Worksheets.GetRangeByName("TestNamedRange");
Console.WriteLine(range.ColumnCount);

This is not true.



Try this:



Workbook workbook = new Workbook();

Cells cells = workbook.Worksheets[0].Cells;

Range range = cells.CreateRange(“A1”, “B2”);

range.Name = “TestNamedRange”;

var lastRowIndex = range.FirstRow + range.RowCount - 1;

cells.InsertRows(lastRowIndex, 1);

Console.WriteLine(range.RowCount);



Notice how RowCount is updated without range is being reassigned. Why is there such discrepency between InsertRows() and InsertColumns()?

Hi,

Thanks your info.

After checking this issue again, I have found the problem. We will fix the bug soon.

Hi,

Please try this fix.

We will update the named rang when we insert Rows/Columns.

Simon,



Thanks for the fix, however, there’s still a bug with the updating.



InsertColumns() allows us to insert multiple columns at once. However it seems like InsertColumns() increments the ColumnCount of the named range by only 1 each time. Whether we insert 10 columns or 100 columns at once, the ColumnCount only increments by 1 each time.



See this sample:



var grid = (new Workbook()).Worksheets[0];



var range = grid.Cells.CreateRange(“A1”, “B2”);



range.Name = “TestNamedRange”;





Console.WriteLine(“ColumnCount before InsertColumns(): {0}”, range.ColumnCount);

grid.Cells.InsertColumns(range.FirstColumn + 1, 1);

Console.WriteLine(“ColumnCount should be 3 and it’s actually {0}”, range.ColumnCount);



grid.Cells.InsertColumns(range.FirstColumn + 1, 2);

Console.WriteLine(“ColumnCount should be 5 and it’s actually {0}”, range.ColumnCount);



grid.Cells.InsertColumns(range.FirstColumn + 1, 3);

Console.WriteLine(“ColumnCount should be 8 and it’s actually {0}”, range.ColumnCount);



-Steve

Hi Steve,

Thanks for pointing it out.

Yes, you are right! we found the issue.

We will fix it soon.

Thank you.

Hi Steve,

Please try this fix.

We have fixed this bug and another bug of saving SpreadMl file after inserting columns in your another post.

This is working great now … thanks!

The issues you have found earlier (filed as 6784) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Laurence.