private static void ConfirmAreaCopy(AreaPtg aptg, int firstRowCopied, int lastRowCopied, int rowOffset, int expectedFirstRow, int expectedLastRow, bool expectedChanged) { AreaPtg copyPtg = (AreaPtg)aptg.Copy(); // clone so we can re-use aptg in calling method Ptg[] ptgs = { copyPtg, }; FormulaShifter fs = FormulaShifter.CreateForRowCopy(0, null, firstRowCopied, lastRowCopied, rowOffset, SpreadsheetVersion.EXCEL2007); bool actualChanged = fs.AdjustFormula(ptgs, 0); // DeletedAreaRef if (expectedFirstRow < 0 || expectedLastRow < 0) { Assert.AreEqual(typeof(AreaErrPtg), ptgs[0].GetType(), "Reference should have shifted off worksheet, producing #REF! error: " + ptgs[0]); return; } Assert.AreEqual(expectedChanged, actualChanged, "Should this AreaPtg change due to row copy?"); Assert.AreEqual(copyPtg, ptgs[0], "AreaPtgs should be modified in-place when a row containing the AreaPtg is copied"); // expected to change in place (although this is not a strict requirement) Assert.AreEqual(expectedFirstRow, copyPtg.FirstRow, "AreaPtg first row"); Assert.AreEqual(expectedLastRow, copyPtg.LastRow, "AreaPtg last row"); }
/** * 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); } } }