/ Published in: Groovy
Table as a MAP with 2D keys (row and column).
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Table { } } } def endValue = value } cells.put(computeKey(rowName, colName), endValue) } } } } } } } } result << row } result } } } } [rowName, colName] } } } assertEquals 2, table.rowCount } assertEquals 2, table.colCount } table.addRow('r3') assertEquals 3, table.rowCount } table.addCol('c3') assertEquals 3, table.colCount } table.addCol('c1') assertEquals(['c1', 'c2'], table.colNames) } table.addRow('r1') assertEquals(['r1', 'r2'], table.rowNames) } table.put('r1', 'c3', '') assertEquals(['c1', 'c2', 'c3'], table.colNames) } table.put('r3', 'c1', '') assertEquals(['r1', 'r2', 'r3'], table.rowNames) } } table.put('r1', 'c1', 'value') } table.put('r1', 'c1', 'value') table.put('r1', 'c1'){ it + '2'} } table.fill(1) } table.put('r1', 'c1', 2) table.fillIfNull(1) } table.put('r1', 'c1', 'r1c1') table.put('r1', 'c2', 'r1c2') table.put('r2', 'c1', 'r2c1') table.put('r2', 'c2', 'r2c2') table.topLeft = 'x' assertEquals( [['x', 'c1', 'c2'], ['r1', 'r1c1', 'r1c2'], ['r2', 'r2c1', 'r2c2']], table.toListOfLists() ) } table.fill(1) assertEquals( [['r1', 'c1', 1], ['r1', 'c2', 1], ['r2', 'c1', 1], ['r2', 'c2', 1]], result) } }