跳到主要内容

表格操作

循环读取

def tab = 数据表
// rows获取行数属性、cols获取列数
for (i = 1; i <= tab.rows; i++) {
// 读取指定单元格的值
print(tab.Cell(i, "列索引").value)

// 给指定单元格赋值
tab.Cell(i, "索引").value = 100
tab.Cell(i, 1).value = 100
}

获取行、列和单元格

可以通过索引和绝对行列值、以及混合的方式获取单元格,再进行操作。示例如下:

// 获取单元格
数据表.Cell("行索引", "列索引")
数据表.Cell(1, 1)
数据表.Cell("行索引", 1)
数据表.Cell(1, "列索引")
// 获取列
数据表.Columns("列索引1", "列索引2", "列索引6")
数据表.Columns(1, 2, 4)
数据表.Columns(1)
// 获取行
数据表.Rows("行索引1", "行索引2", "行索引6")
数据表.Rows(1, 2, 4)
数据表.Rows(1)
// 获取连续单元格
数据表.Cells("起点行索引1", "起点列索引A", "终点行索引2", "终点列索引B")
数据表.Cells(1, 1, 5, 5)
数据表.Cells(1, 1, "*", "*") // 如果是*号,表示所有

操作单元格

// 写入数据
数据表.Cell("行索引", "列索引").value = 100
数据表.Cell("行索引", "列索引").value += 100
// 增加和删除行
数据表.appendRow(v1, v2, vn)
数据表.cutRow(n)
数据表.cutRow("行索引")
// 清除表
数据表.ClearAll() // 清除行索引和所有的值,不清除列索引
数据表.Clear() // 清除值,不清除行索引和列索引
数据表.Cell(...).clear()
数据表.Cells(...).clear()
//将数据表A表单元格区域的值粘贴到数据表B表指定的起点单元格
数据表A.Cells(...).copyTo(数据表B, "目标表行索引", "目标表列索引")
数据表A.Cells(...).copyTo(数据表B, 1, 1)
// 排序
数据表.sort(1, 2, 5, "up", "down", "down")
数据表.sort("colIndex1", "colIndex2", "colIndex3", "up", "down", "down")
// 查找
数据表.cursorRow = 1
def ifFind = 数据表.find("col1", "col2", "coln", "A")
def ifFind = 数据表.find(1, 2, n, "A")
// 找到后返回true,
// 数据表的光标 数据表.cursorRow; 数据表.cursorCol 定位到找的单元格的行列值
// 没找到返回false,数据表.cursorRow为-1,数据表.cursorCol也为-1
// 设置列索引和行索引的名称
数据表.Cell(n, 0).value = "new row index"
数据表.Cell(n, 11).value = "new column index"
// 修改列数据格式、单元格颜色、字体颜色
数据表.Columns("列名1", "列名2", ..., "列名n").DataType = "Integer"
数据表.Columns("列名1", "列名2", ..., "列名n").Color = "##FF0000"
数据表.Columns("列名1", "列名2", ..., "列名n").FontColor = "##FF0000"
// 修改单元格颜色
数据表.Cell().Color = "颜色"
数据表.Cell().FontColor = "颜色"
数据表.Cells().Color = "颜色"
数据表.Cells().FontColor = "颜色" // 给所有的cell设置颜色

统计单元格

支持 summaxminaveragestd(标准差)。

// 以sum为例,其它用法相同
数据表.sum() // 全表求和
数据表.Columns(...).sum() // 列求和
数据表.Cells(...).sum() // 单元格区域求和
数据表.Cell(...).sum() // 单元格求和

SQL查询语句

def sql = "select * from '数据表'"
def records = 资源表.query(sql)
for (i = 0; i < records.size(); i++) {
def record = records.get(i)
print(record.get("B"))
}