-
Notifications
You must be signed in to change notification settings - Fork 97
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
Labels
Comments
/**
* 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 |
I don't know whether the code is right or not, but it work for me .. |
Because of Corona, we are currently spending significantly less time on JXLS. Therefore an answer or processing can take longer. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
.The text was updated successfully, but these errors were encountered: