diff --git a/src/integrationtest/org/apache/poi/TestAllFiles.java b/src/integrationtest/org/apache/poi/TestAllFiles.java index 85b058084..784b9baa2 100644 --- a/src/integrationtest/org/apache/poi/TestAllFiles.java +++ b/src/integrationtest/org/apache/poi/TestAllFiles.java @@ -103,12 +103,12 @@ public class TestAllFiles { HANDLERS.put(".vsd", new HDGFFileHandler()); // Visio - ooxml (currently unsupported) - HANDLERS.put(".vsdm", new NullFileHandler()); - HANDLERS.put(".vsdx", new NullFileHandler()); - HANDLERS.put(".vssm", new NullFileHandler()); - HANDLERS.put(".vssx", new NullFileHandler()); - HANDLERS.put(".vstm", new NullFileHandler()); - HANDLERS.put(".vstx", new NullFileHandler()); + HANDLERS.put(".vsdm", new XDGFFileHandler()); + HANDLERS.put(".vsdx", new XDGFFileHandler()); + HANDLERS.put(".vssm", new XDGFFileHandler()); + HANDLERS.put(".vssx", new XDGFFileHandler()); + HANDLERS.put(".vstm", new XDGFFileHandler()); + HANDLERS.put(".vstx", new XDGFFileHandler()); // POIFS HANDLERS.put(".ole2", new POIFSFileHandler()); diff --git a/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java b/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java index 7b0821dcc..1a8cbf6a0 100644 --- a/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java +++ b/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java @@ -20,17 +20,10 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.io.InputStream; -import java.util.List; import org.apache.poi.POIXMLDocument; -import org.apache.poi.openxml4j.exceptions.OpenXML4JException; -import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.openxml4j.opc.PackageAccess; -import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.junit.Ignore; -import org.junit.Test; public final class POIXMLDocumentHandler { protected void handlePOIXMLDocument(POIXMLDocument doc) throws Exception { @@ -44,33 +37,15 @@ public final class POIXMLDocumentHandler { protected static boolean isEncrypted(InputStream stream) throws IOException { if (POIFSFileSystem.hasPOIFSHeader(stream)) { POIFSFileSystem poifs = new POIFSFileSystem(stream); - if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { - return true; + try { + if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { + return true; + } + } finally { + poifs.close(); } throw new IOException("wrong file format or file extension for OO XML file"); } return false; } - - // a test-case to test this locally without executing the full TestAllFiles - @Ignore("POIXMLDocument cannot handle this Visio file currently...") - @Test - public void test() throws Exception { - OPCPackage pkg = OPCPackage.open("test-data/diagram/test.vsdx", PackageAccess.READ); - try { - handlePOIXMLDocument(new TestPOIXMLDocument(pkg)); - } finally { - pkg.close(); - } - } - - private final static class TestPOIXMLDocument extends POIXMLDocument { - public TestPOIXMLDocument(OPCPackage pkg) { - super(pkg); - } - - public List getAllEmbedds() throws OpenXML4JException { - return null; - } - } } diff --git a/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java b/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java new file mode 100644 index 000000000..4c4fd6088 --- /dev/null +++ b/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java @@ -0,0 +1,75 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.stress; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.poi.POIXMLDocument; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; +import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; +import org.apache.poi.util.PackageHelper; +import org.junit.Test; + +public class XDGFFileHandler extends AbstractFileHandler { + @Override + public void handleFile(InputStream stream) throws Exception { + // ignore password protected files + if (POIXMLDocumentHandler.isEncrypted(stream)) return; + + TestXDGFXMLDocument doc = new TestXDGFXMLDocument(stream); + new POIXMLDocumentHandler().handlePOIXMLDocument(doc); + } + + @Override + public void handleExtracting(File file) throws Exception { + // TODO: extraction/actual operations not supported yet + } + + // a test-case to test this locally without executing the full TestAllFiles + @Test + public void test() throws Exception { + OPCPackage pkg = OPCPackage.open("test-data/diagram/test.vsdx", PackageAccess.READ); + try { + TestXDGFXMLDocument doc = new TestXDGFXMLDocument(pkg); + new POIXMLDocumentHandler().handlePOIXMLDocument(doc); + } finally { + pkg.close(); + } + } + + // TODO: Get rid of this when full visio ooxml support is added + private final static class TestXDGFXMLDocument extends POIXMLDocument { + public TestXDGFXMLDocument(OPCPackage pkg) { + super(pkg, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); + } + + public TestXDGFXMLDocument(InputStream is) throws IOException { + this(PackageHelper.open(is)); + } + + public List getAllEmbedds() throws OpenXML4JException { + return new ArrayList(); + } + } +} \ No newline at end of file diff --git a/src/java/org/apache/poi/hssf/record/CFRule12Record.java b/src/java/org/apache/poi/hssf/record/CFRule12Record.java index c9d0f174b..e7974a092 100644 --- a/src/java/org/apache/poi/hssf/record/CFRule12Record.java +++ b/src/java/org/apache/poi/hssf/record/CFRule12Record.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.record; import java.util.Arrays; +import org.apache.poi.hssf.record.cf.ColorGradientFormatting; import org.apache.poi.hssf.record.cf.IconMultiStateFormatting; import org.apache.poi.hssf.record.cf.Threshold; import org.apache.poi.hssf.record.common.FtrHeader; @@ -56,9 +57,9 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { private byte[] template_params; private IconMultiStateFormatting multistate; + private ColorGradientFormatting color_gradient; // TODO Parse these - private byte[] gradient_data; private byte[] databar_data; private byte[] filter_data; @@ -176,7 +177,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { byte type = getConditionType(); if (type == CONDITION_TYPE_COLOR_SCALE) { - gradient_data = in.readRemainder(); + color_gradient = new ColorGradientFormatting(in); } else if (type == CONDITION_TYPE_DATA_BAR) { databar_data = in.readRemainder(); } else if (type == CONDITION_TYPE_FILTER) { @@ -201,6 +202,21 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { return multistate; } + public boolean containsColorGradientBlock() { + return (color_gradient != null); + } + public ColorGradientFormatting getColorGradientFormatting() { + return color_gradient; + } + public ColorGradientFormatting createColorGradientFormatting() { + if (color_gradient != null) return color_gradient; + + // Convert, setup and return + setConditionType(CONDITION_TYPE_COLOR_SCALE); + color_gradient = new ColorGradientFormatting(); + return color_gradient; + } + /** * get the stack of the scale expression as a list * @@ -261,7 +277,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { byte type = getConditionType(); if (type == CONDITION_TYPE_COLOR_SCALE) { - out.write(gradient_data); + color_gradient.serialize(out); } else if (type == CONDITION_TYPE_DATA_BAR) { out.write(databar_data); } else if (type == CONDITION_TYPE_FILTER) { @@ -285,7 +301,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { byte type = getConditionType(); if (type == CONDITION_TYPE_COLOR_SCALE) { - len += gradient_data.length; + len += color_gradient.getDataLength(); } else if (type == CONDITION_TYPE_DATA_BAR) { len += databar_data.length; } else if (type == CONDITION_TYPE_FILTER) { @@ -319,9 +335,11 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { buffer.append(" .priority =").append(priority).append("\n"); buffer.append(" .template_type =").append(template_type).append("\n"); buffer.append(" .template_params=").append(HexDump.toHex(template_params)).append("\n"); - buffer.append(" .gradient_data =").append(HexDump.toHex(gradient_data)).append("\n"); buffer.append(" .databar_data =").append(HexDump.toHex(databar_data)).append("\n"); buffer.append(" .filter_data =").append(HexDump.toHex(filter_data)).append("\n"); + if (color_gradient != null) { + buffer.append(color_gradient); + } if (multistate != null) { buffer.append(multistate); } diff --git a/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java b/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java new file mode 100644 index 000000000..3b58af74e --- /dev/null +++ b/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java @@ -0,0 +1,144 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.record.cf; + +import org.apache.poi.util.BitField; +import org.apache.poi.util.BitFieldFactory; +import org.apache.poi.util.LittleEndianInput; +import org.apache.poi.util.LittleEndianOutput; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; + +/** + * Color Gradient / Color Scale Conditional Formatting Rule Record. + * (Called Color Gradient in the file format docs, but more commonly + * Color Scale in the UI) + */ +public final class ColorGradientFormatting implements Cloneable { + private static POILogger log = POILogFactory.getLogger(ColorGradientFormatting.class); + + private byte options = 0; + private Threshold[] thresholds; + private byte[] colors; // TODO Decode + + private static BitField clamp = BitFieldFactory.getInstance(0x01); + private static BitField background = BitFieldFactory.getInstance(0x02); + + public ColorGradientFormatting() { + options = 3; + thresholds = new Threshold[3]; + } + public ColorGradientFormatting(LittleEndianInput in) { + in.readShort(); // Ignored + in.readByte(); // Reserved + int numI = in.readByte(); + int numG = in.readByte(); + if (numI != numG) { + log.log(POILogger.WARN, "Inconsistent Color Gradient defintion, found " + numI + " vs " + numG + " entries"); + } + options = in.readByte(); + + // TODO Are these correct? + thresholds = new Threshold[numI]; + for (int i=0; i 0) { + this.value = null; + } } public Double getValue() { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java new file mode 100644 index 000000000..6e82072d4 --- /dev/null +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java @@ -0,0 +1,75 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.usermodel; + +import org.apache.poi.hssf.record.CFRule12Record; +import org.apache.poi.hssf.record.cf.ColorGradientFormatting; +import org.apache.poi.hssf.record.cf.Threshold; +import org.apache.poi.ss.usermodel.Color; +import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold; + +/** + * High level representation for Color Scale / Color Gradient + * Formatting component of Conditional Formatting settings + */ +public final class HSSFColorScaleFormatting implements org.apache.poi.ss.usermodel.ColorScaleFormatting { + private final HSSFSheet sheet; + private final CFRule12Record cfRule12Record; + private final ColorGradientFormatting colorFormatting; + + protected HSSFColorScaleFormatting(CFRule12Record cfRule12Record, HSSFSheet sheet) { + this.sheet = sheet; + this.cfRule12Record = cfRule12Record; + this.colorFormatting = this.cfRule12Record.getColorGradientFormatting(); + } + + public int getNumControlPoints() { + return colorFormatting.getNumControlPoints(); + } + public void setNumControlPoints(int num) { + colorFormatting.setNumControlPoints(num); + } + + public Color[] getColors() { + return null; // TODO + } + public void setColors(Color[] colors) { + // TODO + } + + public HSSFConditionalFormattingThreshold[] getThresholds() { + Threshold[] t = colorFormatting.getThresholds(); + HSSFConditionalFormattingThreshold[] ht = new HSSFConditionalFormattingThreshold[t.length]; + for (int i=0; inull otherwise */ public HSSFIconMultiStateFormatting getMultiStateFormatting() { return getMultiStateFormatting(false); } - /** * create a new icon / multi-state formatting object if it does not exist, * otherwise just return the existing object. @@ -209,6 +210,37 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin return getMultiStateFormatting(true); } + private HSSFColorScaleFormatting getColorScaleFormatting(boolean create) { + CFRule12Record cfRule12Record = getCFRule12Record(create); + ColorGradientFormatting colorFormatting = cfRule12Record.getColorGradientFormatting(); + if (colorFormatting != null) + { + return new HSSFColorScaleFormatting(cfRule12Record, sheet); + } + else if( create ) + { + colorFormatting = cfRule12Record.createColorGradientFormatting(); + return new HSSFColorScaleFormatting(cfRule12Record, sheet); + } + else + { + return null; + } + } + /** + * @return color scale / gradient formatting object if defined, null otherwise + */ + public HSSFColorScaleFormatting getColorScaleFormatting() { + return getColorScaleFormatting(false); + } + /** + * create a new color scale / gradient formatting object if it does not exist, + * otherwise just return the existing object. + */ + public HSSFColorScaleFormatting createColorScaleFormatting() { + return getColorScaleFormatting(true); + } + /** * @return - the conditiontype for the cfrule */ diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index 0e18ee112..0352d5c87 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -57,6 +57,15 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart implements Close protected POIXMLDocument(OPCPackage pkg) { super(pkg); + init(pkg); + } + + protected POIXMLDocument(OPCPackage pkg, String coreDocumentRel) { + super(pkg, coreDocumentRel); + init(pkg); + } + + private void init(OPCPackage pkg) { this.pkg = pkg; // Workaround for XMLBEANS-512 - ensure that when we parse diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java b/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java index f051eb32a..857f0dad2 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java @@ -63,7 +63,7 @@ public class POIXMLDocumentPart { DEFAULT_XML_OPTIONS.setCharacterEncoding("UTF-8"); } - + private String coreDocumentRel = PackageRelationshipTypes.CORE_DOCUMENT; private PackagePart packagePart; private PackageRelationship packageRel; private POIXMLDocumentPart parent; @@ -93,7 +93,16 @@ public class POIXMLDocumentPart { * Construct POIXMLDocumentPart representing a "core document" package part. */ public POIXMLDocumentPart(OPCPackage pkg) { - PackageRelationship coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0); + this(pkg, PackageRelationshipTypes.CORE_DOCUMENT); + } + + /** + * Construct POIXMLDocumentPart representing a custom "core document" package part. + */ + public POIXMLDocumentPart(OPCPackage pkg, String coreDocumentRel) { + this.coreDocumentRel = coreDocumentRel; + PackageRelationship coreRel = pkg.getRelationshipsByType(this.coreDocumentRel).getRelationship(0); + if (coreRel == null) { coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0); if (coreRel != null) { @@ -151,10 +160,10 @@ public class POIXMLDocumentPart { */ protected final void rebase(OPCPackage pkg) throws InvalidFormatException { PackageRelationshipCollection cores = - packagePart.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); + packagePart.getRelationshipsByType(coreDocumentRel); if(cores.size() != 1) { throw new IllegalStateException( - "Tried to rebase using " + PackageRelationshipTypes.CORE_DOCUMENT + + "Tried to rebase using " + coreDocumentRel + " but found " + cores.size() + " parts of the right type" ); } diff --git a/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java b/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java index 55f4093fb..2c41b18f7 100644 --- a/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java +++ b/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java @@ -31,6 +31,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.util.PackageHelper; import org.apache.poi.util.TempFile; @@ -44,6 +45,10 @@ public final class TestPOIXMLDocument extends TestCase { public OPCParser(OPCPackage pkg) { super(pkg); } + + public OPCParser(OPCPackage pkg, String coreDocumentRel) { + super(pkg, coreDocumentRel); + } @Override public List getAllEmbedds() { @@ -181,4 +186,33 @@ public final class TestPOIXMLDocument extends TestCase { part.onDocumentCreate(); //part.getTargetPart(null); } + + public void testVSDX() throws Exception { + OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); + + POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); + + assertNotNull(part); + assertEquals(0, part.getRelationCounter()); + } + + public void testVSDXPart() throws Exception { + OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); + + POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); + + assertNotNull(part); + assertEquals(0, part.getRelationCounter()); + } + + public void testInvalidCoreRel() throws Exception { + OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); + + try { + new POIXMLDocumentPart(open, "somethingillegal"); + fail("Unknown core ref will throw exception"); + } catch (POIXMLException e) { + // expected here + } + } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index b8eeb2053..1a130b751 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -101,6 +101,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { /** * Test format conditions based on a boolean formula */ + @SuppressWarnings("deprecation") public void testBooleanFormulaConditions() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sh = wb.createSheet(); @@ -136,6 +137,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals("B1:B3", ranges2[0].formatAsString()); } + @SuppressWarnings("deprecation") public void testSingleFormulaConditions() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sh = wb.createSheet(); @@ -207,6 +209,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals(ComparisonOperator.NOT_BETWEEN, rule9.getComparisonOperation()); } + @SuppressWarnings("deprecation") public void testCopy() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet1 = wb.createSheet(); @@ -546,18 +549,17 @@ public abstract class BaseTestConditionalFormatting extends TestCase { Sheet s = wb.getSheet("CF"); ConditionalFormatting cf = null; ConditionalFormattingRule cr = null; - IconMultiStateFormatting icon = null; - ConditionalFormattingThreshold th = null; + // Sanity check data assertEquals("Values", s.getRow(0).getCell(0).toString()); assertEquals("10.0", s.getRow(2).getCell(0).toString()); - // Check we found all the conditional formattings rules we should have + // Check we found all the conditional formatting rules we should have SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting(); int numCF = 3; int numCF12 = 15; - int numCFEX = 0; // TODO This should be 1, but we don't support CFEX formattings yet + int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet, see #58149 assertEquals(numCF+numCF12+numCFEX, sheetCF.getNumConditionalFormattings()); int fCF = 0, fCF12 = 0, fCFEX = 0; @@ -646,26 +648,18 @@ public abstract class BaseTestConditionalFormatting extends TestCase { // TODO Support Data Bars, then check the rest of this rule - // Colours R->G - Column F + // Colours Red->Yellow->Green - Column F cf = sheetCF.getConditionalFormattingAt(3); assertEquals(1, cf.getFormattingRanges().length); assertEquals("F2:F17", cf.getFormattingRanges()[0].formatAsString()); - - assertEquals(1, cf.getNumberOfRules()); - cr = cf.getRule(0); - assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType()); - // TODO Support Color Scales, then check the rest of this rule + assertColorScale(cf, "F8696B", "FFEB84", "63BE7B"); - // Colours BWR - Column G + // Colours Blue->White->Red - Column G cf = sheetCF.getConditionalFormattingAt(4); assertEquals(1, cf.getFormattingRanges().length); assertEquals("G2:G17", cf.getFormattingRanges()[0].formatAsString()); - - assertEquals(1, cf.getNumberOfRules()); - cr = cf.getRule(0); - assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType()); - // TODO Support Color Scales, then check the rest of this rule + assertColorScale(cf, "5A8AC6", "FCFCFF", "F8696B"); // Icons : Default - Column H, percentage thresholds @@ -696,22 +690,95 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertIconSetPercentages(cf, IconSet.GYRB_4_TRAFFIC_LIGHTS, 0d, 25d, 50d, 75d); - // Icons : 3 symbols - Column L - // Icons : 3 flags - Column M - // Icons : 3 symbols 2 - Column N - // Icons : 3 arrows - Column O - // Icons : 5 arrows grey - Column P - // Icons : 3 stars (ext) - Column Q - // Icons : 4 ratings - Column R - // Icons : 5 ratings - Column S - // Custom Icon+Format - Column T - // Mixed icons - Column U + // Icons : 3 symbols with backgrounds - Column L + cf = sheetCF.getConditionalFormattingAt(9); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("L2:L17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS_CIRCLE, 0d, 33d, 67d); + + // Icons : 3 flags - Column M2 Only + cf = sheetCF.getConditionalFormattingAt(10); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("M2", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d); + + // Icons : 3 flags - Column M (all) + cf = sheetCF.getConditionalFormattingAt(11); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("M2:M17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d); + + + // Icons : 3 symbols 2 (no background) - Column N + cf = sheetCF.getConditionalFormattingAt(12); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("N2:N17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS, 0d, 33d, 67d); + + + // Icons : 3 arrows - Column O + cf = sheetCF.getConditionalFormattingAt(13); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("O2:O17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_ARROW, 0d, 33d, 67d); + + + // Icons : 5 arrows grey - Column P + cf = sheetCF.getConditionalFormattingAt(14); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("P2:P17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GREY_5_ARROWS, 0d, 20d, 40d, 60d, 80d); + + + // Icons : 3 stars (ext) - Column Q + // TODO Support EXT formattings + + + // Icons : 4 ratings - Column R + cf = sheetCF.getConditionalFormattingAt(15); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("R2:R17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.RATINGS_4, 0d, 25d, 50d, 75d); + + + // Icons : 5 ratings - Column S + cf = sheetCF.getConditionalFormattingAt(16); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("S2:S17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.RATINGS_5, 0d, 20d, 40d, 60d, 80d); + + + // Custom Icon+Format - Column T + cf = sheetCF.getConditionalFormattingAt(17); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("T2:T17", cf.getFormattingRanges()[0].formatAsString()); + + // TODO Support IconSet + Other CFs with 2 rules +// assertEquals(2, cf.getNumberOfRules()); +// cr = cf.getRule(0); +// assertIconSetPercentages(cr, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d); +// cr = cf.getRule(1); +// assertEquals(ConditionType.FORMULA, cr.getConditionTypeType()); +// assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); +// // TODO Why aren't these two the same between formats? +// if (cr instanceof HSSFConditionalFormattingRule) { +// assertEquals("MOD(ROW($T1),2)=1", cr.getFormula1()); +// } else { +// assertEquals("MOD(ROW($T2),2)=1", cr.getFormula1()); +// } +// assertEquals(null, cr.getFormula2()); + + + // Mixed icons - Column U + // TODO Support EXT formattings } private void assertIconSetPercentages(ConditionalFormatting cf, IconSet iconset, Double...vals) { assertEquals(1, cf.getNumberOfRules()); ConditionalFormattingRule cr = cf.getRule(0); - + assertIconSetPercentages(cr, iconset, vals); + } + private void assertIconSetPercentages(ConditionalFormattingRule cr, IconSet iconset, Double...vals) { assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType()); assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); assertEquals(null, cr.getFormula1()); @@ -733,6 +800,50 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals(null, th.getFormula()); } } + + private void assertColorScale(ConditionalFormatting cf, String... colors) { + assertEquals(1, cf.getNumberOfRules()); + ConditionalFormattingRule cr = cf.getRule(0); + assertColorScale(cr, colors); + } + private void assertColorScale(ConditionalFormattingRule cr, String... colors) { + assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType()); + assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); + assertEquals(null, cr.getFormula1()); + assertEquals(null, cr.getFormula2()); + + // TODO Implement +/* + ColorScaleFormatting color = cr.getColorScaleFormatting(); + assertNotNull(color); + assertNotNull(color.getColors()); + assertNotNull(color.getThresholds()); + assertEquals(colors.length, color.getNumControlPoints()); + assertEquals(colors.length, color.getColors().length); + assertEquals(colors.length, color.getThresholds().length); + + // Thresholds should be Min / (evenly spaced) / Max + int steps = 100 / (colors.length-1); + for (int i=0; i