Iterate Cells

ReoGrid provides a method named IterateCells which is used to iterate through all valid cells. ReoGrid uses a paging-indexed two-dimensional array to manage all cells in memory. IterateCells method can skip the empty cells, empty index-pages and merged cells in order to get better performance.

Define:

public void IterateCells(string addressOrName, Func<int, int, ReoGridCell, bool> iterator)
public void IterateCells(RangePosition range, Func<int, int, ReoGridCell, bool> iterator)
public void IterateCells(int row, int col, int rows, int cols, Func<int, int, ReoGridCell, bool> iterator)

Usage (C#):

sheet.IterateCells(range, (row, col, cell) =>
{
  // return true to continue iterate, return false to abort
  return true;
});

Usage (VB.NET):

sheet.IterateCells(range, Function(row, col, cell)
  ' return true to continue iterate, return false to abort
  Return True
End Function)

Skip conditions

Iterate Cells

IterateCells method skips the following objects:

  • Empty cells – A cell without any data and own styles set, it will be skipped during iteration
  • Empty index-pages – An index-page without any cells attached, it will be skipped during iteration
  • Invalid cells – A cell merged by another cell, it will become invalid and skipped by this method

Sample

A sample to sum values through inside a specified range. (the code-behind of sum function)

public static double Sum(Worksheet sheet, RangePosition range)
{
  double val = 0;

  sheet.IterateCells(range, (row, col, cell) =>
  {
    if (ScriptRunningMachine.IsNumber(cell.Data))
    {
      val += ScriptRunningMachine.GetDoubleValue(cell.Data);
    }
    return true;
  });

  return val;
}

See Also


Next: Script Execution