When applying cell protection to a Range, this is handled on a cell-by-cell basis. That is very inefficient for large ranges.
For example the following test produces an OutOfMemoryException on my machine. The test sets the first 6 columns of the sheet to be protected. The way ClosedXML does this is to enumerate each cell in the range, then set the protection style. And because each column is considered as a range with 1048576 cells, this requires a lot of processing.
When doing this in Excel or LibreOffice, it's fast and the resulting file is small. So apparently there is a way to do it more efficiently.
Possibly this issue applies to other types of styling as well (e.g. setting the background of a whole column), that should be tested further.
```
using ClosedXML.Excel;
using NUnit.Framework;
namespace ClosedXML_Tests
{
[TestFixture]
public class LargeRangeTests
{
[Test]
public void CanCreateLargeRange()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");
var range = ws1.Range("A:F");
Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");
range.Style.Protection.Locked = true;
Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked");
}
}
}
```
Update: it applies to other forms of styling as well, like setting the background of the whole column. The test below also leads to an OutOfMemoryException.
```
[Test]
public void CanSetColumnsBackground()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");
var range = ws1.Range("A:F");
Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");
range.Style.Fill.BackgroundColor = XLColor.RadicalRed;
Assert.AreEqual(range.LastRow().Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last row is red");
}
```
For example the following test produces an OutOfMemoryException on my machine. The test sets the first 6 columns of the sheet to be protected. The way ClosedXML does this is to enumerate each cell in the range, then set the protection style. And because each column is considered as a range with 1048576 cells, this requires a lot of processing.
When doing this in Excel or LibreOffice, it's fast and the resulting file is small. So apparently there is a way to do it more efficiently.
Possibly this issue applies to other types of styling as well (e.g. setting the background of a whole column), that should be tested further.
```
using ClosedXML.Excel;
using NUnit.Framework;
namespace ClosedXML_Tests
{
[TestFixture]
public class LargeRangeTests
{
[Test]
public void CanCreateLargeRange()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");
var range = ws1.Range("A:F");
Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");
range.Style.Protection.Locked = true;
Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked");
}
}
}
```
Update: it applies to other forms of styling as well, like setting the background of the whole column. The test below also leads to an OutOfMemoryException.
```
[Test]
public void CanSetColumnsBackground()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");
var range = ws1.Range("A:F");
Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");
range.Style.Fill.BackgroundColor = XLColor.RadicalRed;
Assert.AreEqual(range.LastRow().Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last row is red");
}
```