use of org.apache.poi.util.Beta in project poi by apache.
the class XSSFRow method copyRowFrom.
/**
* Copy the cells from srcRow to this row
* If this row is not a blank row, this will merge the two rows, overwriting
* the cells in this row with the cells in srcRow
* If srcRow is null, overwrite cells in destination row with blank values, styles, etc per cell copy policy
* srcRow may be from a different sheet in the same workbook
* @param srcRow the rows to copy from
* @param policy the policy to determine what gets copied
*/
@Beta
public void copyRowFrom(Row srcRow, CellCopyPolicy policy) {
if (srcRow == null) {
// srcRow is blank. Overwrite cells with blank values, blank styles, etc per cell copy policy
for (Cell destCell : this) {
final XSSFCell srcCell = null;
// FIXME: remove type casting when copyCellFrom(Cell, CellCopyPolicy) is added to Cell interface
((XSSFCell) destCell).copyCellFrom(srcCell, policy);
}
if (policy.isCopyMergedRegions()) {
// Remove MergedRegions in dest row
final int destRowNum = getRowNum();
int index = 0;
final Set<Integer> indices = new HashSet<Integer>();
for (CellRangeAddress destRegion : getSheet().getMergedRegions()) {
if (destRowNum == destRegion.getFirstRow() && destRowNum == destRegion.getLastRow()) {
indices.add(index);
}
index++;
}
getSheet().removeMergedRegions(indices);
}
if (policy.isCopyRowHeight()) {
// clear row height
setHeight((short) -1);
}
} else {
for (final Cell c : srcRow) {
final XSSFCell srcCell = (XSSFCell) c;
final XSSFCell destCell = createCell(srcCell.getColumnIndex(), srcCell.getCellTypeEnum());
destCell.copyCellFrom(srcCell, policy);
}
final XSSFRowShifter rowShifter = new XSSFRowShifter(_sheet);
final int sheetIndex = _sheet.getWorkbook().getSheetIndex(_sheet);
final String sheetName = _sheet.getWorkbook().getSheetName(sheetIndex);
final int srcRowNum = srcRow.getRowNum();
final int destRowNum = getRowNum();
final int rowDifference = destRowNum - srcRowNum;
final FormulaShifter shifter = FormulaShifter.createForRowCopy(sheetIndex, sheetName, srcRowNum, srcRowNum, rowDifference, SpreadsheetVersion.EXCEL2007);
rowShifter.updateRowFormulas(this, shifter);
// FIXME: is this something that rowShifter could be doing?
if (policy.isCopyMergedRegions()) {
for (CellRangeAddress srcRegion : srcRow.getSheet().getMergedRegions()) {
if (srcRowNum == srcRegion.getFirstRow() && srcRowNum == srcRegion.getLastRow()) {
CellRangeAddress destRegion = srcRegion.copy();
destRegion.setFirstRow(destRowNum);
destRegion.setLastRow(destRowNum);
getSheet().addMergedRegion(destRegion);
}
}
}
if (policy.isCopyRowHeight()) {
setHeight(srcRow.getHeight());
}
}
}
Aggregations