From: PJ Fanning Date: Fri, 24 Dec 2021 11:11:44 +0000 (+0000) Subject: preserve rich text in cell copy X-Git-Tag: REL_5_2_0~49 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dfb4af31e718b46cab77a4e5b3fd0d3f9eadd6a5;p=poi.git preserve rich text in cell copy git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896348 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtilCopy.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtilCopy.java index 24e53c68f9..720218ace6 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtilCopy.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtilCopy.java @@ -17,8 +17,10 @@ package org.apache.poi.ss.tests.util; +import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.BaseTestCellUtilCopy; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class TestXSSFCellUtilCopy extends BaseTestCellUtilCopy { @@ -27,4 +29,15 @@ public class TestXSSFCellUtilCopy extends BaseTestCellUtilCopy { protected Workbook createNewWorkbook() { return new XSSFWorkbook(); } + + @Override + protected boolean compareRichText(RichTextString rts1, RichTextString rts2) { + if (rts1 instanceof XSSFRichTextString && rts2 instanceof XSSFRichTextString) { + XSSFRichTextString xrts1 = (XSSFRichTextString)rts1; + XSSFRichTextString xrts2 = (XSSFRichTextString)rts2; + return xrts1.getCTRst().xmlText().equals(xrts2.getCTRst().xmlText()); + } else { + return super.compareRichText(rts1, rts2); + } + } } diff --git a/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtilCopy.java b/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtilCopy.java index 1396448a4c..ea214e815d 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtilCopy.java +++ b/poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtilCopy.java @@ -64,6 +64,26 @@ public abstract class BaseTestCellUtilCopy { assertEquals(CellType.NUMERIC, destCell.getCellType()); } + @Test + public final void testCopyCellFrom_CellCopyPolicy_richTextValue() { + setUp_testCopyCellFrom_CellCopyPolicy(); + Workbook wb = srcCell.getSheet().getWorkbook(); + CreationHelper creationHelper = wb.getCreationHelper(); + RichTextString rts = creationHelper.createRichTextString("text 123"); + Font font = wb.createFont(); + font.setFontHeight((short) 999); + font.setFontName("Muriel"); + rts.applyFont(0, 3, font); + srcCell.setCellFormula(null); + srcCell.setCellValue(rts); + + // Paste values only + final CellCopyPolicy policy = new CellCopyPolicy.Builder().cellFormula(false).build(); + CellUtil.copyCell(srcCell, destCell, policy, new CellCopyContext()); + assertEquals(CellType.STRING, destCell.getCellType()); + assertTrue(compareRichText(rts, destCell.getRichStringCellValue())); + } + @Test public final void testCopyCellFrom_CellCopyPolicy_formulaWithUnregisteredUDF() { setUp_testCopyCellFrom_CellCopyPolicy(); @@ -165,4 +185,7 @@ public abstract class BaseTestCellUtilCopy { protected abstract Workbook createNewWorkbook(); + protected boolean compareRichText(RichTextString rts1, RichTextString rts2) { + return rts1.getString().equals(rts2.getString()); + } }