jxls icon indicating copy to clipboard operation
jxls copied to clipboard

Support Multiple Row Merge

Open mannixsuo opened this issue 2 years ago • 3 comments

image mergeCells command is not easy to use if I need merge many rows , like the picture show, I want merge many rows in single command or less command than mergeCells.

mannixsuo avatar May 07 '22 02:05 mannixsuo

/**
 * merge rows from start to last cell,
 * last cell and first cell must at same line
 * <p>
 * jx:mergeRow(lastCell="Merge row ranges",
 * rows="Number of rows combined")
 */
public class MergeRowCommand extends AbstractCommand {

    public static final String COMMAND_NAME = "mergeRow";
    /**
     * rows to merge
     */
    private String rows;

    private Area area;

    public MergeRowCommand() {
    }

    @Override
    public Command addArea(Area area) {
        if (area == null) {
            return this;
        }
        if (area.getStartCellRef().getRow() != area.getAreaRef().getLastCellRef().getRow()) {
            throw new IllegalArgumentException("You can add only a single row to 'mergeRow' command");
        }
        if (getAreaList().size() >= 1) {
            throw new IllegalArgumentException("You can add only a single area to 'mergeRow' command");
        }
        this.area = area;
        return super.addArea(area);
    }

    @Override
    public String getName() {
        return COMMAND_NAME;
    }

    @Override
    public Size applyAt(CellRef cellRef, Context context) {
        area.applyAt(cellRef, context);
        int rowsToMerge = getExpressionValue(rows, context);
        if (rowsToMerge > 1) {
            int startRow = cellRef.getRow();
            int startCol = cellRef.getCol();
            Size size = area.getAreaRef().getSize();
            int width = size.getWidth();
            for (int i = 0; i < width; i++) {
                CellRef ref = new CellRef(cellRef.getSheetName(), startRow, startCol + i);
                getTransformer().mergeCells(ref, rowsToMerge, 1);
            }
        }
        return new Size(area.getSize().getWidth(), area.getSize().getHeight() + rowsToMerge - 1);
    }

    private int getExpressionValue(String expression, Context context) {
        if (expression != null && expression.trim().length() > 0) {
            Object obj = getTransformationConfig().getExpressionEvaluator().evaluate(expression, context.toMap());
            try {
                return Integer.parseInt(obj.toString());
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Expression: " + expression + " failed to resolve");
            }
        }
        return 0;
    }

    public String getRows() {
        return rows;
    }

    public void setRows(String rows) {
        this.rows = rows;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }
}

add comment in excel like this jx:rowMerge(lastCell="C2",rows="itemList.size()")

mannixsuo avatar May 07 '22 07:05 mannixsuo

I don't know whether the code is right or not, but it work for me ..

mannixsuo avatar May 07 '22 08:05 mannixsuo

Because of Corona, we are currently spending significantly less time on JXLS. Therefore an answer or processing can take longer.

SoltauFintel avatar May 08 '22 06:05 SoltauFintel