ReoGrid 提供了一个名为 IterateCells 的方法用于遍历所有有效单元格。ReoGrid 使用分页索引的二维数组来管理内存中的所有单元格。IterateCells 方法可以跳过空单元格、空索引页和合并单元格以获得更好的性能。
// 通过地址或名称遍历单元格
IterateCells = function(addressOrName, iterator);
// 通过范围对象遍历单元格
IterateCells = function(range, iterator);
// 通过行列范围遍历单元格
IterateCells = function(row, col, rows, cols, iterator);
sheet1.IterateCells(range, function(row, col, cell){
// 返回 true 继续遍历,返回 false 终止遍历
return true;
});

IterateCells 方法会跳过以下对象:
以下是一个计算指定范围内数值总和的示例(sum函数的实现代码):
// 计算指定范围内数值的总和
Sum = function(sheet1, range){
var val = 0;
sheet1.IterateCells(range, function(row, col, cell){
if(type(cell.Data) == type.number){
val += cell.Data;
}
return true;
});
return val;
}
IterateCells 方法接受一个回调函数,该函数会被传入当前单元格的行号、列号和单元格对象true 继续遍历,返回 false 终止遍历type() 函数判断数据类型,比 C# 中的 ScriptRunningMachine.IsNumber 更简洁