Exemple #1
0
        public void TestCopyCellFrom_CellCopyPolicy_default()
        {
            setUp_testCopyCellFrom_CellCopyPolicy();

            // default copy policy
            CellCopyPolicy policy = new CellCopyPolicy();

            destCell.CopyCellFrom(srcCell, policy);

            Assert.AreEqual(CellType.Formula, destCell.CellType);
            Assert.AreEqual("2+3", destCell.CellFormula);
            Assert.AreEqual(srcCell.CellStyle, destCell.CellStyle);
        }
Exemple #2
0
        /// <summary>
        /// Copy cell value, formula and style, from srcCell per cell copy policy
        ///  If srcCell is null, clears the cell value and cell style per cell copy policy
        ///
        /// This does not shift references in formulas. Use {@link XSSFRowShifter} to shift references in formulas.
        /// </summary>
        /// <param name="srcCell">The cell to take value, formula and style from</param>
        /// <param name="policy">The policy for copying the information, see {@link CellCopyPolicy}</param>
        /// <exception cref="ArgumentException">if copy cell style and srcCell is from a different workbook</exception>
        public void CopyCellFrom(ICell srcCell, CellCopyPolicy policy)
        {
            // Copy cell value (cell type is updated implicitly)
            if (policy.IsCopyCellValue)
            {
                if (srcCell != null)
                {
                    CellType copyCellType = srcCell.CellType;
                    if (copyCellType == CellType.Formula && !policy.IsCopyCellFormula)
                    {
                        // Copy formula result as value
                        // FIXME: Cached value may be stale
                        copyCellType = srcCell.CachedFormulaResultType;
                    }
                    switch (copyCellType)
                    {
                    case CellType.Boolean:
                        SetCellValue(srcCell.BooleanCellValue);
                        break;

                    case CellType.Error:
                        SetCellErrorValue(srcCell.ErrorCellValue);
                        break;

                    case CellType.Formula:
                        SetCellFormula(srcCell.CellFormula);
                        break;

                    case CellType.Numeric:
                        // DataFormat is not copied unless policy.isCopyCellStyle is true
                        if (DateUtil.IsCellDateFormatted(srcCell))
                        {
                            SetCellValue(srcCell.DateCellValue);
                        }
                        else
                        {
                            SetCellValue(srcCell.NumericCellValue);
                        }
                        break;

                    case CellType.String:
                        SetCellValue(srcCell.StringCellValue);
                        break;

                    case CellType.Blank:
                        SetBlank();
                        break;

                    default:
                        throw new ArgumentException("Invalid cell type " + srcCell.CellType);
                    }
                }
                else
                { //srcCell is null
                    SetBlank();
                }
            }

            // Copy CellStyle
            if (policy.IsCopyCellStyle)
            {
                if (srcCell != null)
                {
                    CellStyle = (srcCell.CellStyle);
                }
                else
                {
                    // clear cell style
                    CellStyle = (null);
                }
            }


            if (policy.IsMergeHyperlink)
            {
                // if srcCell doesn't have a hyperlink and destCell has a hyperlink, don't clear destCell's hyperlink
                IHyperlink srcHyperlink = srcCell.Hyperlink;
                if (srcHyperlink != null)
                {
                    Hyperlink = new XSSFHyperlink(srcHyperlink);
                }
            }
            else if (policy.IsCopyHyperlink)
            {
                // overwrite the hyperlink at dest cell with srcCell's hyperlink
                // if srcCell doesn't have a hyperlink, clear the hyperlink (if one exists) at destCell
                IHyperlink srcHyperlink = srcCell.Hyperlink;
                if (srcHyperlink == null)
                {
                    Hyperlink = (null);
                }
                else
                {
                    Hyperlink = new XSSFHyperlink(srcHyperlink);
                }
            }
        }
Exemple #3
0
        /**
         * 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
         */

        public void CopyRowFrom(IRow srcRow, CellCopyPolicy policy)
        {
            if (srcRow == null)
            {
                // srcRow is blank. Overwrite cells with blank values, blank styles, etc per cell copy policy
                foreach (ICell destCell in this)
                {
                    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
                    int           destRowNum = RowNum;
                    int           index      = 0;
                    HashSet <int> indices    = new HashSet <int>();
                    foreach (CellRangeAddress destRegion in Sheet.MergedRegions)
                    {
                        if (destRowNum == destRegion.FirstRow && destRowNum == destRegion.LastRow)
                        {
                            indices.Add(index);
                        }
                        index++;
                    }
                    (Sheet as XSSFSheet).RemoveMergedRegions(indices.ToList());
                }
                if (policy.IsCopyRowHeight)
                {
                    // clear row height
                    Height = ((short)-1);
                }
            }
            else
            {
                foreach (ICell c in srcRow)
                {
                    XSSFCell srcCell  = (XSSFCell)c;
                    XSSFCell destCell = CreateCell(srcCell.ColumnIndex, srcCell.CellType) as XSSFCell;
                    destCell.CopyCellFrom(srcCell, policy);
                }
                XSSFRowShifter rowShifter    = new XSSFRowShifter(_sheet);
                int            sheetIndex    = _sheet.Workbook.GetSheetIndex(_sheet);
                String         sheetName     = _sheet.Workbook.GetSheetName(sheetIndex);
                int            srcRowNum     = srcRow.RowNum;
                int            destRowNum    = RowNum;
                int            rowDifference = destRowNum - srcRowNum;
                FormulaShifter shifter       = FormulaShifter.CreateForRowCopy(sheetIndex, sheetName, srcRowNum, srcRowNum, rowDifference, SpreadsheetVersion.EXCEL2007);
                rowShifter.UpdateRowFormulas(this, shifter);
                // Copy merged regions that are fully contained on the row
                // FIXME: is this something that rowShifter could be doing?
                if (policy.IsCopyMergedRegions)
                {
                    foreach (CellRangeAddress srcRegion in srcRow.Sheet.MergedRegions)
                    {
                        if (srcRowNum == srcRegion.FirstRow && srcRowNum == srcRegion.LastRow)
                        {
                            CellRangeAddress destRegion = srcRegion.Copy();
                            destRegion.FirstRow = (destRowNum);
                            destRegion.LastRow  = (destRowNum);
                            Sheet.AddMergedRegion(destRegion);
                        }
                    }
                }
                if (policy.IsCopyRowHeight)
                {
                    Height = (srcRow.Height);
                }
            }
        }