Search in sources :

Example 21 with Beta

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());
        }
    }
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFRowShifter(org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter) FormulaShifter(org.apache.poi.ss.formula.FormulaShifter) HashSet(java.util.HashSet) Beta(org.apache.poi.util.Beta)

Aggregations

Beta (org.apache.poi.util.Beta)21 AreaReference (org.apache.poi.ss.util.AreaReference)7 XmlOptions (org.apache.xmlbeans.XmlOptions)7 IOException (java.io.IOException)4 QName (javax.xml.namespace.QName)4 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)4 XmlException (org.apache.xmlbeans.XmlException)4 CTPivotField (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotField)4 CTPivotFields (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields)4 OutputStream (java.io.OutputStream)3 Cell (org.apache.poi.ss.usermodel.Cell)3 Row (org.apache.poi.ss.usermodel.Row)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 CellReference (org.apache.poi.ss.util.CellReference)2 CTItems (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItems)2 CTWorksheetSource (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource)2 HashSet (java.util.HashSet)1 FormulaShifter (org.apache.poi.ss.formula.FormulaShifter)1 CellCopyPolicy (org.apache.poi.ss.usermodel.CellCopyPolicy)1 Name (org.apache.poi.ss.usermodel.Name)1