From d01745284b0db244ce4d1a4ad5e314205e94f920 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 7 Nov 2011 19:39:23 +0000 Subject: [PATCH] Added option to "transpose columns into rows" operation for filling in other columns. git-svn-id: http://google-refine.googlecode.com/svn/trunk@2367 7d457c2a-affb-35e4-300a-418c747d4874 --- .../cell/TransposeColumnsIntoRowsCommand.java | 9 +++-- .../TransposeColumnsIntoRowsOperation.java | 40 +++++++++++++++---- .../views/data-table/menu-edit-cells.js | 3 +- .../transpose-columns-into-rows.html | 4 ++ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/main/src/com/google/refine/commands/cell/TransposeColumnsIntoRowsCommand.java b/main/src/com/google/refine/commands/cell/TransposeColumnsIntoRowsCommand.java index 8f84b4478..db26bd954 100644 --- a/main/src/com/google/refine/commands/cell/TransposeColumnsIntoRowsCommand.java +++ b/main/src/com/google/refine/commands/cell/TransposeColumnsIntoRowsCommand.java @@ -58,6 +58,7 @@ public class TransposeColumnsIntoRowsCommand extends Command { String startColumnName = request.getParameter("startColumnName"); int columnCount = Integer.parseInt(request.getParameter("columnCount")); boolean ignoreBlankCells = Boolean.parseBoolean(request.getParameter("ignoreBlankCells")); + boolean fillDown = Boolean.parseBoolean(request.getParameter("fillDown")); String combinedColumnName = request.getParameter("combinedColumnName"); if (combinedColumnName != null) { @@ -65,16 +66,16 @@ public class TransposeColumnsIntoRowsCommand extends Command { String separator = request.getParameter("separator"); op = new TransposeColumnsIntoRowsOperation( startColumnName, columnCount, - combinedColumnName, prependColumnName, separator, - ignoreBlankCells); + ignoreBlankCells, fillDown, + combinedColumnName, prependColumnName, separator); } else { String keyColumnName = request.getParameter("keyColumnName"); String valueColumnName = request.getParameter("valueColumnName"); op = new TransposeColumnsIntoRowsOperation( startColumnName, columnCount, - keyColumnName, valueColumnName, - ignoreBlankCells); + ignoreBlankCells, fillDown, + keyColumnName, valueColumnName); } Process process = op.createProcess(project, new Properties()); diff --git a/main/src/com/google/refine/operations/cell/TransposeColumnsIntoRowsOperation.java b/main/src/com/google/refine/operations/cell/TransposeColumnsIntoRowsOperation.java index 3a8b0f0a9..e35cfc87e 100644 --- a/main/src/com/google/refine/operations/cell/TransposeColumnsIntoRowsOperation.java +++ b/main/src/com/google/refine/operations/cell/TransposeColumnsIntoRowsOperation.java @@ -55,6 +55,7 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { final protected String _startColumnName; final protected int _columnCount; final protected boolean _ignoreBlankCells; + final protected boolean _fillDown; final protected String _combinedColumnName; final protected boolean _prependColumnName; @@ -69,18 +70,20 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { return new TransposeColumnsIntoRowsOperation( obj.getString("startColumnName"), obj.getInt("columnCount"), + JSONUtilities.getBoolean(obj, "ignoreBlankCells", true), + JSONUtilities.getBoolean(obj, "fillDown", false), combinedColumnName, obj.getBoolean("prependColumnName"), - obj.getString("separator"), - obj.getBoolean("ignoreBlankCells") + obj.getString("separator") ); } else { return new TransposeColumnsIntoRowsOperation( obj.getString("startColumnName"), obj.getInt("columnCount"), + JSONUtilities.getBoolean(obj, "ignoreBlankCells", true), + JSONUtilities.getBoolean(obj, "fillDown", false), obj.getString("keyColumnName"), - obj.getString("valueColumnName"), - obj.getBoolean("ignoreBlankCells") + obj.getString("valueColumnName") ); } } @@ -88,14 +91,16 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { public TransposeColumnsIntoRowsOperation( String startColumnName, int columnCount, + boolean ignoreBlankCells, + boolean fillDown, String combinedColumnName, boolean prependColumnName, - String separator, - boolean ignoreBlankCells + String separator ) { _startColumnName = startColumnName; _columnCount = columnCount; _ignoreBlankCells = ignoreBlankCells; + _fillDown = fillDown; _combinedColumnName = combinedColumnName; _prependColumnName = prependColumnName; @@ -108,13 +113,15 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { public TransposeColumnsIntoRowsOperation( String startColumnName, int columnCount, + boolean ignoreBlankCells, + boolean fillDown, String keyColumnName, - String valueColumnName, - boolean ignoreBlankCells + String valueColumnName ) { _startColumnName = startColumnName; _columnCount = columnCount; _ignoreBlankCells = ignoreBlankCells; + _fillDown = fillDown; _combinedColumnName = null; _prependColumnName = false; @@ -134,6 +141,7 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { writer.key("startColumnName"); writer.value(_startColumnName); writer.key("columnCount"); writer.value(_columnCount); writer.key("ignoreBlankCells"); writer.value(_ignoreBlankCells); + writer.key("fillDown"); writer.value(_fillDown); if (_combinedColumnName != null) { writer.key("combinedColumnName"); writer.value(_combinedColumnName); writer.key("prependColumnName"); writer.value(_prependColumnName); @@ -266,6 +274,7 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { for (int r = 0; r < oldRows.size(); r++) { Row oldRow = project.rows.get(r); Row firstNewRow = new Row(newColumns.size()); + int firstNewRowIndex = newRows.size(); newRows.add(firstNewRow); @@ -325,6 +334,21 @@ public class TransposeColumnsIntoRowsOperation extends AbstractOperation { cell); } } + + if (_fillDown) { + for (int r2 = firstNewRowIndex + 1; r2 < newRows.size(); r2++) { + Row newRow = newRows.get(r2); + for (int c = 0; c < newColumns.size(); c++) { + Column column = newColumns.get(c); + int cellIndex = column.getCellIndex(); + + Cell cellToCopy = firstNewRow.getCell(cellIndex); + if (cellToCopy != null && newRow.getCell(cellIndex) == null) { + newRow.setCell(cellIndex, cellToCopy); + } + } + } + } } return new HistoryEntry( diff --git a/main/webapp/modules/core/scripts/views/data-table/menu-edit-cells.js b/main/webapp/modules/core/scripts/views/data-table/menu-edit-cells.js index dbcaf7184..a2a7455d2 100644 --- a/main/webapp/modules/core/scripts/views/data-table/menu-edit-cells.js +++ b/main/webapp/modules/core/scripts/views/data-table/menu-edit-cells.js @@ -253,7 +253,8 @@ DataTableColumnHeaderUI.extendMenu(function(column, columnHeaderUI, menu) { var config = { startColumnName: elmts.fromColumnSelect[0].value, columnCount: elmts.toColumnSelect[0].value, - ignoreBlankCells: elmts.ignoreBlankCellsCheckbox[0].checked + ignoreBlankCells: elmts.ignoreBlankCellsCheckbox[0].checked, + fillDown: elmts.fillDownCheckbox[0].checked }; var mode = dialog.find('input[name="transpose-dialog-column-choices"]:checked')[0].value; diff --git a/main/webapp/modules/core/scripts/views/data-table/transpose-columns-into-rows.html b/main/webapp/modules/core/scripts/views/data-table/transpose-columns-into-rows.html index 9a06b90b4..c36f1b45d 100644 --- a/main/webapp/modules/core/scripts/views/data-table/transpose-columns-into-rows.html +++ b/main/webapp/modules/core/scripts/views/data-table/transpose-columns-into-rows.html @@ -54,6 +54,10 @@ + + + +