From af0085fd32306b1bc93b933ae0b9bcfe8bf5d835 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 20 Sep 2010 20:10:14 +0000 Subject: [PATCH] Fix bug #47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@999096 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xssf/model/StylesTable.java | 6 +- .../poi/xssf/usermodel/XSSFCellStyle.java | 43 +++++++- .../poi/xssf/usermodel/TestXSSFCellStyle.java | 98 +++++++++++++++++-- 4 files changed, 137 insertions(+), 11 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index cad9c5d4e1..736f3549b4 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one 49931 - Avoid concurrency problems when re-ordering multiple HSSF header records for a PageSettingsBlock 49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships 48018 - Improve HWPF handling of lists in documents read and then saved, by preserving order better diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 0230d660fd..6a4a7678cb 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -183,6 +183,8 @@ public class StylesTable extends POIXMLDocumentPart { * registration is requested. * This allows people to create several fonts * then customise them later. + * Note - End Users probably want to call + * {@link XSSFFont#registerTo(StylesTable)} */ public int putFont(XSSFFont font, boolean forceRegistration) { int idx = -1; @@ -193,8 +195,10 @@ public class StylesTable extends POIXMLDocumentPart { if (idx != -1) { return idx; } + + idx = fonts.size(); fonts.add(font); - return fonts.size() - 1; + return idx; } public int putFont(XSSFFont font) { return putFont(font, false); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index d2d17bd57b..6358168c67 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel; +import org.apache.poi.POIXMLException; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; @@ -31,11 +32,13 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; +import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle; @@ -132,8 +135,44 @@ public class XSSFCellStyle implements CellStyle { public void cloneStyleFrom(CellStyle source) { if(source instanceof XSSFCellStyle) { XSSFCellStyle src = (XSSFCellStyle)source; - _cellXf.set(src.getCoreXf()); - _cellStyleXf.set(src.getStyleXf()); + + // Is it on our Workbook? + if(src._stylesSource == _stylesSource) { + // Nice and easy + _cellXf.set(src.getCoreXf()); + _cellStyleXf.set(src.getStyleXf()); + } else { + // Copy the style + try { + _cellXf = CTXf.Factory.parse( + src.getCoreXf().toString() + ); + } catch(XmlException e) { + throw new POIXMLException(e); + } + + // Copy the format + String fmt = src.getDataFormatString(); + setDataFormat( + (new XSSFDataFormat(_stylesSource)).getFormat(fmt) + ); + + // Copy the font + try { + CTFont ctFont = CTFont.Factory.parse( + src.getFont().getCTFont().toString() + ); + XSSFFont font = new XSSFFont(ctFont); + font.registerTo(_stylesSource); + setFont(font); + } catch(XmlException e) { + throw new POIXMLException(e); + } + } + + // Clear out cached details + _font = null; + _cellAlignment = null; } else { throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle"); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index f6c63c96a9..02b34ae85e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -19,16 +19,25 @@ package org.apache.poi.xssf.usermodel; import junit.framework.TestCase; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.VerticalAlignment; -import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.HorizontalAlignment; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.hssf.usermodel.HSSFCellStyle; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; public class TestXSSFCellStyle extends TestCase { @@ -586,12 +595,85 @@ public class TestXSSFCellStyle extends TestCase { * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook */ public void testCloneStyleSameWB() { - // TODO + XSSFWorkbook wb = new XSSFWorkbook(); + assertEquals(1, wb.getNumberOfFonts()); + + XSSFFont fnt = wb.createFont(); + fnt.setFontName("TestingFont"); + assertEquals(2, wb.getNumberOfFonts()); + + XSSFCellStyle orig = wb.createCellStyle(); + orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT); + orig.setFont(fnt); + orig.setDataFormat((short)18); + + assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment()); + assertTrue(fnt == orig.getFont()); + assertTrue(18 == orig.getDataFormat()); + + XSSFCellStyle clone = wb.createCellStyle(); + assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment()); + assertFalse(fnt == clone.getFont()); + assertFalse(18 == clone.getDataFormat()); + + clone.cloneStyleFrom(orig); + assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment()); + assertTrue(fnt == clone.getFont()); + assertTrue(18 == clone.getDataFormat()); + assertEquals(2, wb.getNumberOfFonts()); } /** * Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks */ public void testCloneStyleDiffWB() { - // TODO - } + XSSFWorkbook wbOrig = new XSSFWorkbook(); + assertEquals(1, wbOrig.getNumberOfFonts()); + assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size()); + + XSSFFont fnt = wbOrig.createFont(); + fnt.setFontName("TestingFont"); + assertEquals(2, wbOrig.getNumberOfFonts()); + assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size()); + + XSSFDataFormat fmt = wbOrig.createDataFormat(); + fmt.getFormat("MadeUpOne"); + fmt.getFormat("MadeUpTwo"); + + XSSFCellStyle orig = wbOrig.createCellStyle(); + orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT); + orig.setFont(fnt); + orig.setDataFormat(fmt.getFormat("Test##")); + + assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment()); + assertTrue(fnt == orig.getFont()); + assertTrue(fmt.getFormat("Test##") == orig.getDataFormat()); + + assertEquals(2, wbOrig.getNumberOfFonts()); + assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size()); + + + // Now a style on another workbook + XSSFWorkbook wbClone = new XSSFWorkbook(); + assertEquals(1, wbClone.getNumberOfFonts()); + assertEquals(0, wbClone.getStylesSource().getNumberFormats().size()); + + XSSFDataFormat fmtClone = wbClone.createDataFormat(); + XSSFCellStyle clone = wbClone.createCellStyle(); + + assertEquals(1, wbClone.getNumberOfFonts()); + assertEquals(0, wbClone.getStylesSource().getNumberFormats().size()); + + assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment()); + assertFalse("TestingFont" == clone.getFont().getFontName()); + + clone.cloneStyleFrom(orig); + + assertEquals(2, wbClone.getNumberOfFonts()); + assertEquals(1, wbClone.getStylesSource().getNumberFormats().size()); + + assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment()); + assertEquals("TestingFont", clone.getFont().getFontName()); + assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat()); + assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##")); + } } -- 2.39.5