public void TestCopyCellFrom_CellCopyPolicy_mergeHyperlink() { //setUp_testCopyCellFrom_CellCopyPolicy(); IWorkbook wb = srcCell.Sheet.Workbook; ICreationHelper createHelper = wb.GetCreationHelper(); srcCell.SetCellValue("URL LINK"); IHyperlink link = createHelper.CreateHyperlink(HyperlinkType.Url); link.Address = ("http://poi.apache.org/"); destCell.Hyperlink = (link); // Set link cell style (optional) ICellStyle hlinkStyle = wb.CreateCellStyle(); IFont hlinkFont = wb.CreateFont(); hlinkFont.Underline = FontUnderlineType.Single; hlinkFont.Color = (IndexedColors.Blue.Index); hlinkStyle.SetFont(hlinkFont); destCell.CellStyle = (hlinkStyle); // Pre-condition assumptions. This test is broken if either of these Assert.Fail. Assert.AreSame(srcCell.Sheet, destCell.Sheet, "unit test assumes srcCell and destCell are on the same sheet"); Assert.IsNull(srcCell.Hyperlink); // Merge hyperlink - since srcCell doesn't have a hyperlink, destCell's hyperlink is not overwritten (cleared). CellCopyPolicy policy = new CellCopyPolicy.Builder().MergeHyperlink(true).CopyHyperlink(false).Build(); destCell.CopyCellFrom(srcCell, policy); Assert.IsNull(srcCell.Hyperlink); Assert.IsNotNull(destCell.Hyperlink); Assert.AreSame(link, destCell.Hyperlink); List <IHyperlink> links; links = srcCell.Sheet.GetHyperlinkList(); Assert.AreEqual(1, links.Count, "number of hyperlinks on sheet"); Assert.AreEqual(new CellReference(destCell).FormatAsString(), (links[(0)] as XSSFHyperlink).CellRef, "source hyperlink"); // Merge destCell's hyperlink to srcCell. Since destCell does have a hyperlink, this should copy destCell's hyperlink to srcCell. srcCell.CopyCellFrom(destCell, policy); Assert.IsNotNull(srcCell.Hyperlink); Assert.IsNotNull(destCell.Hyperlink); links = srcCell.Sheet.GetHyperlinkList(); Assert.AreEqual(2, links.Count, "number of hyperlinks on sheet"); Assert.AreEqual(new CellReference(destCell).FormatAsString(), (links[(0)] as XSSFHyperlink).CellRef, "dest hyperlink"); Assert.AreEqual(new CellReference(srcCell).FormatAsString(), (links[(1)] as XSSFHyperlink).CellRef, "source hyperlink"); wb.Close(); }
/** * 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); } } }