final int tgtRow = target.getRowIndex();
final int tgtCol = target.getColumnIndex();
final int nRow = tRow - tgtRow;
final int nCol = lCol - tgtCol;
if (nRow != 0 || nCol != 0) { //if target not the left-top one, move to left-top
final ChangeInfo info = BookHelper.moveRange(sheet, tgtRow, tgtCol, tgtRow, tgtCol, nRow, nCol);
if (info != null) {
changes.addAll(info.getMergeChanges());
last.addAll(info.getToEval());
all.addAll(info.getAffected());
}
}
final CellStyle source = target.getCellStyle();
style = source.getIndex() == 0 ? null : sheet.getWorkbook().createCellStyle();
if (style != null) {
style.cloneStyleFrom(source);
style.setBorderLeft(CellStyle.BORDER_NONE);
style.setBorderTop(CellStyle.BORDER_NONE);
style.setBorderRight(CellStyle.BORDER_NONE);
style.setBorderBottom(CellStyle.BORDER_NONE);
target.setCellStyle(style); //set all cell in the merged range to CellStyle of the target minus border
}
//1st row (exclude 1st cell)
for (int c = lCol + 1; c <= rCol; ++c) {
final Cell cell = getOrCreateCell(sheet, tRow, c);
cell.setCellStyle(style); //set all cell in the merged range to CellStyle of the target minus border
final Set<Ref>[] refs = BookHelper.setCellValue(cell, (RichTextString) null);
if (refs != null) {
last.addAll(refs[0]);
all.addAll(refs[1]);
}
}
//2nd row and after
for(int r = tRow+1; r <= bRow; ++r) {
for(int c = lCol; c <= rCol; ++c) {
final Cell cell = getOrCreateCell(sheet, r, c);
cell.setCellStyle(style); //set all cell in the merged range to CellStyle of the target minus border
final Set<Ref>[] refs = BookHelper.setCellValue(cell, (RichTextString) null);
if (refs != null) {
last.addAll(refs[0]);
all.addAll(refs[1]);
}
}
}
}
sheet.addMergedRegion(new CellRangeAddress(tRow, bRow, lCol, rCol));
final Ref mergeArea = new AreaRefImpl(tRow, lCol, bRow, rCol, BookHelper.getRefSheet((Book)sheet.getWorkbook(), sheet));
all.add(mergeArea);
changes.add(new MergeChange(null, mergeArea));
return new ChangeInfo(last, all, changes);
}