Getting row indexes on Range
After trying a lot of different things, and close to giving up, the following
solved it:
(The range.EntireRow.Select() is not mandatory, and you should check that
Selection does in fact return a Range.)
private List<int getSelectedRows(Worksheet sheet)
{
List<int listRet = new List<int();
Range range = (Range)sheet.Application.Selection;
range.EntireRow.Select();
range = (Range)sheet.Application.Selection;
for (int i=1; i<=range.Areas.Count; i++)
{
Range internalRange = range.Areas.get_Item(i);
int firstId = internalRange.Row;
int lastId = firstId + internalRange.Rows.Count;
for (int j = firstId; j < lastId; j++)
{
if (!listRet.Contains(j))
{
listRet.Add(j);
}
}
}
return listRet;
}
|