"Transpose columns into rows" operation now supports specifying the ending column to be the last column regardless of its name.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2337 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-10-19 13:42:50 +00:00
parent 223074bb25
commit f4b2ee3715
2 changed files with 81 additions and 43 deletions

View File

@ -91,7 +91,12 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation {
writer.object(); writer.object();
writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass()));
writer.key("description"); writer.value("Transpose cells in " + _columnCount + " column(s) starting with " + _startColumnName + " into rows"); writer.key("description"); writer.value(
_columnCount > 0 ?
("Transpose cells in " + _columnCount +
" column(s) starting with " + _startColumnName + " into rows") :
("Transpose cells in columns starting with " +
_startColumnName + " into rows"));
writer.key("startColumnName"); writer.value(_startColumnName); writer.key("startColumnName"); writer.value(_startColumnName);
writer.key("columnCount"); writer.value(_columnCount); writer.key("columnCount"); writer.value(_columnCount);
writer.key("combinedColumnName"); writer.value(_combinedColumnName); writer.key("combinedColumnName"); writer.value(_combinedColumnName);
@ -103,7 +108,11 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation {
@Override @Override
protected String getBriefDescription(Project project) { protected String getBriefDescription(Project project) {
return "Transpose cells in " + _columnCount + " column(s) starting with " + _startColumnName + " into rows"; return _columnCount > 0 ?
("Transpose cells in " + _columnCount +
" column(s) starting with " + _startColumnName + " into rows") :
("Transpose cells in columns starting with " +
_startColumnName + " into rows");
} }
@Override @Override
@ -117,46 +126,72 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation {
List<Column> newColumns = new ArrayList<Column>(); List<Column> newColumns = new ArrayList<Column>();
List<Column> oldColumns = project.columnModel.columns; List<Column> oldColumns = project.columnModel.columns;
int columnsLeftToTranspose = _columnCount;
int startColumnIndex = oldColumns.size(); int startColumnIndex = oldColumns.size();
for (int c = 0; c < oldColumns.size(); c++) { int columnCount = _columnCount;
Column column = oldColumns.get(c); if (_columnCount > 0) {
if (columnsLeftToTranspose == 0) { int columnsLeftToTranspose = _columnCount;
// This column is beyond the columns to transpose for (int c = 0; c < oldColumns.size(); c++) {
Column column = oldColumns.get(c);
Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel()); if (columnsLeftToTranspose == 0) {
newColumn.setName(column.getName()); // This column is beyond the columns to transpose
newColumns.add(newColumn); Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
} else if (columnsLeftToTranspose < _columnCount) { newColumn.setName(column.getName());
// This column is a column to transpose, but not the first
// nothing to do newColumns.add(newColumn);
} else if (columnsLeftToTranspose < _columnCount) {
columnsLeftToTranspose--; // This column is a column to transpose, but not the first
} else if (_startColumnName.equals(column.getName())) { // nothing to do
// This is the first column to transpose
columnsLeftToTranspose--;
startColumnIndex = c; } else if (_startColumnName.equals(column.getName())) {
// This is the first column to transpose
String columnName = _combinedColumnName != null && _combinedColumnName.length() > 0 ? _combinedColumnName : column.getName();
Column newColumn = new Column(newColumns.size(), columnName); startColumnIndex = c;
newColumns.add(newColumn); String columnName = _combinedColumnName != null && _combinedColumnName.length() > 0 ? _combinedColumnName : column.getName();
Column newColumn = new Column(newColumns.size(), columnName);
columnsLeftToTranspose--;
} else { newColumns.add(newColumn);
// This column is before all columns to transpose
columnsLeftToTranspose--;
Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel()); } else {
newColumn.setName(column.getName()); // This column is before all columns to transpose
newColumns.add(newColumn); Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
newColumn.setName(column.getName());
newColumns.add(newColumn);
}
} }
} else {
for (int c = 0; c < oldColumns.size(); c++) {
Column column = oldColumns.get(c);
if (_startColumnName.equals(column.getName())) {
// This is the first column to transpose
startColumnIndex = c;
String columnName = _combinedColumnName != null && _combinedColumnName.length() > 0 ?
_combinedColumnName : column.getName();
Column newColumn = new Column(newColumns.size(), columnName);
newColumns.add(newColumn);
break;
} else {
// This column is before all columns to transpose
Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
newColumn.setName(column.getName());
newColumns.add(newColumn);
}
}
columnCount = oldColumns.size() - startColumnIndex;
} }
List<Row> oldRows = project.rows; List<Row> oldRows = project.rows;
List<Row> newRows = new ArrayList<Row>(oldRows.size() * _columnCount); List<Row> newRows = new ArrayList<Row>(oldRows.size() * columnCount);
for (int r = 0; r < oldRows.size(); r++) { for (int r = 0; r < oldRows.size(); r++) {
Row oldRow = project.rows.get(r); Row oldRow = project.rows.get(r);
Row firstNewRow = new Row(newColumns.size()); Row firstNewRow = new Row(newColumns.size());
@ -170,7 +205,7 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation {
if (c < startColumnIndex) { if (c < startColumnIndex) {
firstNewRow.setCell(c, cell); firstNewRow.setCell(c, cell);
} else if (c == startColumnIndex || c < startColumnIndex + _columnCount) { } else if (c == startColumnIndex || c < startColumnIndex + columnCount) {
Cell newCell; Cell newCell;
if (cell == null || cell.value == null) { if (cell == null || cell.value == null) {
@ -196,7 +231,7 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation {
transposedCells++; transposedCells++;
} else { } else {
firstNewRow.setCell(c - _columnCount + 1, cell); firstNewRow.setCell(c - columnCount + 1, cell);
} }
} }
} }

View File

@ -293,11 +293,14 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) {
for (var k = j + 1; k < columns.length; k++) { for (var k = j + 1; k < columns.length; k++) {
var column2 = columns[k]; var column2 = columns[k];
var option = $('<option>').attr("value", k - j + 1).text(column2.name).appendTo(elmts.toColumnSelect); $('<option>').attr("value", k - j + 1).text(column2.name).appendTo(elmts.toColumnSelect);
if (k == columns.length - 1) {
option.attr("selected", "true");
}
} }
$('<option>')
.attr("value", "-1")
.attr("selected", "true")
.text("(last column)")
.appendTo(elmts.toColumnSelect);
}; };
populateToColumn(); populateToColumn();