aardio 文档

dotNet.ReoGrid 扩展库 - 遍历单元格

ReoGrid 提供了一个名为 IterateCells 的方法用于遍历所有有效单元格。ReoGrid 使用分页索引的二维数组来管理内存中的所有单元格。IterateCells 方法可以跳过空单元格、空索引页和合并单元格以获得更好的性能。

方法定义

// 通过地址或名称遍历单元格
IterateCells = function(addressOrName, iterator);

// 通过范围对象遍历单元格  
IterateCells = function(range, iterator);

// 通过行列范围遍历单元格
IterateCells = function(row, col, rows, cols, iterator);

使用方法 (aardio)

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;
}

代码说明:

  1. IterateCells 方法接受一个回调函数,该函数会被传入当前单元格的行号、列号和单元格对象
  2. 回调函数返回 true 继续遍历,返回 false 终止遍历
  3. 在 aardio 中可以直接使用 type() 函数判断数据类型,比 C# 中的 ScriptRunningMachine.IsNumber 更简洁
  4. 数值相加时不需要像 C# 那样进行类型转换,aardio 会自动处理数值类型

Markdown 格式