Reading CSV data with an empty column fails when using GetCellByIndex

Using Aspose.Cells 8.3.2.1, I am seeing an index out of range exception when attempting to read data from a CSV file that has an empty column if I use the GetCellByIndex method. If I use the bracket operator it works, and curiously if I use the bracket operator first and then attempt to use GetCellByIndex the call works.


Failing test:

[Fact]
public void Aspose_Cells_GetCellByIndex_Fails()
{
var fileName = @".\empty_column.csv";
var workbook = new Workbook(fileName, new LoadOptions(LoadFormat.CSV));
var worksheet = workbook.Worksheets[0];

ValidateGetCellByIndex(worksheet);
}

[Fact]
public void Aspose_Cells_GetCellByIndex_Works_After_Bracket_Operator()
{
var fileName = @".\empty_column.csv";
var workbook = new Workbook(fileName, new LoadOptions(LoadFormat.CSV));
var worksheet = workbook.Worksheets[0];

ValidateBracketOperator(worksheet);
ValidateGetCellByIndex(worksheet);
}

private void ValidateGetCellByIndex(Worksheet sheet)
{
var headers = new List();
for (int i = 0; i <= sheet.Cells.Rows[0].LastDataCell.Column; i++)
{
var header = sheet.Cells.Rows[0].GetCellByIndex(i).StringValue;
headers.Add(header);
}

Assert.Equal(“z”, sheet.Cells.Rows[2][4].StringValue);
Assert.Equal(6, headers.Count);
}

private void ValidateBracketOperator(Worksheet sheet)
{
var headers = new List();
for (int i = 0; i <= sheet.Cells.Rows[0].LastDataCell.Column; i++)
{
var header = sheet.Cells.Rows[0][i].StringValue;
headers.Add(header);
}

Assert.Equal(6, headers.Count);
}

Hi Barry,

Thanks for your posting and using Aspose.Cells.

We were able to observe this issue after testing this issue with the following sample code using the latest version: Aspose.Cells
for .NET v8.3.2.5
. GetCellByIndex throws exception on 6th iteration.

We have logged this issue in our database for investigation. We will look into it and fix this issue. Once, the issue is resolved or we have some other update for you, we will let you know asap.

This issue has been logged as

  • CELLSNET-43433 - Reading CSV data with an empty column fails when using GetCellByIndex

C#

var fileName = @“empty_column.csv”;

var workbook = new Workbook(fileName, new LoadOptions(LoadFormat.CSV));

var sheet = workbook.Worksheets[0];


var headers = new List<string>();

for (int i = 0; i <= sheet.Cells.Rows[0].LastDataCell.Column; i++)

{

//This line throws exception on i=5

var header = sheet.Cells.Rows[0].GetCellByIndex(i).StringValue;

headers.Add(header);

}

Hi.

Please use one of the following solutions to iterate the cells in the row:

a) Row.GetEnumerator() method.

for (IEnumerator ie = workbook.Worksheets[0].Cells.Rows[0].GetEnumerator(); ie.MoveNext(); )
{

Console.WriteLine(((Cell)ie.Current).StringValue);

}

b) Row.GetCellOrNull() method.

for (int i = 0; i <= workbook.Worksheets[0].Cells.Rows[0].LastDataCell.Column; i++)
{

//This line throws exception on i=5

Cell cell = workbook.Worksheets[0].Cells.Rows[0].GetCellOrNull(i);

if (cell != null)

{

Console.WriteLine(cell.StringValue);

}

}

For the Row.GetCellByIndex method, we are
considering to remove it.

Thank you.