Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Multiple Row Merge #195

Open
mannixsuo opened this issue May 7, 2022 · 3 comments
Open

Support Multiple Row Merge #195

mannixsuo opened this issue May 7, 2022 · 3 comments
Labels
enhancement New feature or request mergecells jx:mergeCells

Comments

@mannixsuo
Copy link

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 mannixsuo added the enhancement New feature or request label May 7, 2022
@mannixsuo
Copy link
Author

/**
 * 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
Copy link
Author

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

@SoltauFintel
Copy link
Member

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

@SoltauFintel SoltauFintel added the mergecells jx:mergeCells label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mergecells jx:mergeCells
Projects
None yet
Development

No branches or pull requests

2 participants