]> source.dussan.org Git - poi.git/commitdiff
Add setFormattingRanges() to interface ConditionalFormatting, closes #42
authorDominik Stadler <centic@apache.org>
Mon, 7 Nov 2016 21:32:04 +0000 (21:32 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 7 Nov 2016 21:32:04 +0000 (21:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1768588 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java
src/java/org/apache/poi/ss/usermodel/ConditionalFormatting.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFConditionalFormatting.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index fd31c2df92237bc9f77f75a8416e247486b0ca60..26bdd6bd95b630dc60fb64849057e8a12f4db9ab 100644 (file)
@@ -94,10 +94,17 @@ public final class HSSFConditionalFormatting  implements ConditionalFormatting {
     /**
      * @return array of <tt>CellRangeAddress</tt>s. never <code>null</code> 
      */
+    @Override
     public CellRangeAddress[] getFormattingRanges() {
         return cfAggregate.getHeader().getCellRanges();
     }
 
+    @Override
+    public void setFormattingRanges(
+            final CellRangeAddress[] ranges) {
+        cfAggregate.getHeader().setCellRanges(ranges);
+    }
+
     /**
      * Replaces an existing Conditional Formatting rule at position idx. 
      * Older versions of Excel only allow up to 3 Conditional Formatting rules,
@@ -111,6 +118,7 @@ public final class HSSFConditionalFormatting  implements ConditionalFormatting {
         cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
     }
 
+    @Override
     public void setRule(int idx, ConditionalFormattingRule cfRule){
         setRule(idx, (HSSFConditionalFormattingRule)cfRule);
     }
@@ -124,6 +132,7 @@ public final class HSSFConditionalFormatting  implements ConditionalFormatting {
         cfAggregate.addRule(cfRule.getCfRuleRecord());
     }
 
+    @Override
     public void addRule(ConditionalFormattingRule cfRule){
         addRule((HSSFConditionalFormattingRule)cfRule);
     }
@@ -131,6 +140,7 @@ public final class HSSFConditionalFormatting  implements ConditionalFormatting {
     /**
      * @return the Conditional Formatting rule at position idx.
      */
+    @Override
     public HSSFConditionalFormattingRule getRule(int idx) {
         CFRuleBase ruleRecord = cfAggregate.getRule(idx);
         return new HSSFConditionalFormattingRule(sheet, ruleRecord);
@@ -139,10 +149,12 @@ public final class HSSFConditionalFormatting  implements ConditionalFormatting {
     /**
      * @return number of Conditional Formatting rules.
      */
+    @Override
     public int getNumberOfRules() {
         return cfAggregate.getNumberOfRules();
     }
 
+    @Override
     public String toString() {
         return cfAggregate.toString();
     }
index f6e8ff441f2b53a8eb3574650a45df0fa73ab62c..09d63a407d19e1a202eeda6062edf3490d510deb 100644 (file)
@@ -80,6 +80,12 @@ public interface ConditionalFormatting {
      */\r
     CellRangeAddress[] getFormattingRanges();\r
 \r
+    /**\r
+     * Sets the cell ranges the rule conditional formatting must be applied to.\r
+     * @param ranges non-null array of <tt>CellRangeAddress</tt>s\r
+     */\r
+    void setFormattingRanges(CellRangeAddress[] ranges);\r
+\r
     /**\r
      * Replaces an existing Conditional Formatting rule at position idx.\r
      * Excel pre-2007 allows to create up to 3 Conditional Formatting rules,\r
index 53eb41b122d5bd15b63e7a4866ba3b9ada887ccc..915ba5d4bf1182fda5e8a708032882f45443f4a4 100644 (file)
@@ -25,6 +25,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Collections;\r
 \r
 /**\r
  * @author Yegor Kozlov\r
@@ -33,73 +34,98 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
     private final CTConditionalFormatting _cf;\r
     private final XSSFSheet _sh;\r
 \r
-    /*package*/ XSSFConditionalFormatting(XSSFSheet sh){\r
+    /*package*/ XSSFConditionalFormatting(XSSFSheet sh) {\r
         _cf = CTConditionalFormatting.Factory.newInstance();\r
         _sh = sh;\r
     }\r
 \r
-    /*package*/ XSSFConditionalFormatting(XSSFSheet sh, CTConditionalFormatting cf){\r
+    /*package*/ XSSFConditionalFormatting(\r
+            XSSFSheet sh, CTConditionalFormatting cf) {\r
         _cf = cf;\r
         _sh = sh;\r
     }\r
 \r
-    /*package*/  CTConditionalFormatting getCTConditionalFormatting(){\r
+    /*package*/  CTConditionalFormatting getCTConditionalFormatting() {\r
         return _cf;\r
     }\r
 \r
     /**\r
-      * @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code>\r
-      */\r
-     public CellRangeAddress[] getFormattingRanges(){\r
-         ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>();\r
-         for (Object stRef : _cf.getSqref()) {\r
-             String[] regions = stRef.toString().split(" ");\r
-             for (int i = 0; i < regions.length; i++) {\r
-                 lst.add(CellRangeAddress.valueOf(regions[i]));\r
-             }\r
-         }\r
-         return lst.toArray(new CellRangeAddress[lst.size()]);\r
-     }\r
+     * @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code>\r
+     */\r
+    @Override\r
+    public CellRangeAddress[] getFormattingRanges() {\r
+        ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>();\r
+        for (Object stRef : _cf.getSqref()) {\r
+            String[] regions = stRef.toString().split(" ");\r
+            for (final String region : regions) {\r
+                lst.add(CellRangeAddress.valueOf(region));\r
+            }\r
+        }\r
+        return lst.toArray(new CellRangeAddress[lst.size()]);\r
+    }\r
 \r
-     /**\r
-      * Replaces an existing Conditional Formatting rule at position idx.\r
-      * Excel allows to create up to 3 Conditional Formatting rules.\r
-      * This method can be useful to modify existing  Conditional Formatting rules.\r
-      *\r
-      * @param idx position of the rule. Should be between 0 and 2.\r
-      * @param cfRule - Conditional Formatting rule\r
-      */\r
-     public void setRule(int idx, ConditionalFormattingRule cfRule){\r
-         XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule;\r
-         _cf.getCfRuleArray(idx).set(xRule.getCTCfRule());\r
-     }\r
+    @Override\r
+    public void setFormattingRanges(CellRangeAddress[] ranges) {\r
+        if (ranges == null) {\r
+            throw new IllegalArgumentException("cellRanges must not be null");\r
+        }\r
+        final StringBuilder sb = new StringBuilder();\r
+        boolean first = true;\r
+        for (CellRangeAddress range : ranges) {\r
+            if (!first) {\r
+                sb.append(" ");\r
+            } else {\r
+                first = false;\r
+            }\r
+            sb.append(range.formatAsString());\r
+        }\r
+        _cf.setSqref(Collections.singletonList(sb.toString()));\r
+    }\r
 \r
-     /**\r
-      * Add a Conditional Formatting rule.\r
-      * Excel allows to create up to 3 Conditional Formatting rules.\r
-      *\r
-      * @param cfRule - Conditional Formatting rule\r
-      */\r
-     public void addRule(ConditionalFormattingRule cfRule){\r
-        XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule;\r
-         _cf.addNewCfRule().set(xRule.getCTCfRule());\r
-     }\r
+    /**\r
+     * Replaces an existing Conditional Formatting rule at position idx.\r
+     * Excel allows to create up to 3 Conditional Formatting rules.\r
+     * This method can be useful to modify existing  Conditional Formatting rules.\r
+     *\r
+     * @param idx    position of the rule. Should be between 0 and 2.\r
+     * @param cfRule - Conditional Formatting rule\r
+     */\r
+    @Override\r
+    public void setRule(int idx, ConditionalFormattingRule cfRule) {\r
+        XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;\r
+        _cf.getCfRuleArray(idx).set(xRule.getCTCfRule());\r
+    }\r
 \r
-     /**\r
-      * @return the Conditional Formatting rule at position idx.\r
-      */\r
-     public XSSFConditionalFormattingRule getRule(int idx){\r
-         return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));\r
-     }\r
+    /**\r
+     * Add a Conditional Formatting rule.\r
+     * Excel allows to create up to 3 Conditional Formatting rules.\r
+     *\r
+     * @param cfRule - Conditional Formatting rule\r
+     */\r
+    @Override\r
+    public void addRule(ConditionalFormattingRule cfRule) {\r
+        XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;\r
+        _cf.addNewCfRule().set(xRule.getCTCfRule());\r
+    }\r
 \r
-     /**\r
-      * @return number of Conditional Formatting rules.\r
-      */\r
-     public int getNumberOfRules(){\r
-         return _cf.sizeOfCfRuleArray();\r
-     }\r
-     \r
-     public String toString() {\r
-         return _cf.toString();\r
-     }\r
+    /**\r
+     * @return the Conditional Formatting rule at position idx.\r
+     */\r
+    @Override\r
+    public XSSFConditionalFormattingRule getRule(int idx) {\r
+        return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));\r
+    }\r
+\r
+    /**\r
+     * @return number of Conditional Formatting rules.\r
+     */\r
+    @Override\r
+    public int getNumberOfRules() {\r
+        return _cf.sizeOfCfRuleArray();\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return _cf.toString();\r
+    }\r
 }\r
index 28cfb2cf7b63c8194eef5e4b68139b854a70f88b..d5a8124df975493c9fabd363c0723215601be883 100644 (file)
 \r
 package org.apache.poi.ss.usermodel;\r
 \r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertFalse;\r
-import static org.junit.Assert.assertNotNull;\r
-import static org.junit.Assert.assertNull;\r
-import static org.junit.Assert.assertTrue;\r
-import static org.junit.Assert.fail;\r
-\r
-import java.io.IOException;\r
-\r
 import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting;\r
 import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;\r
 import org.apache.poi.ss.ITestDataProvider;\r
@@ -36,6 +27,10 @@ import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
 import org.apache.poi.ss.util.CellRangeAddress;\r
 import org.junit.Test;\r
 \r
+import java.io.IOException;\r
+\r
+import static org.junit.Assert.*;\r
+\r
 /**\r
  * Base tests for Conditional Formatting, for both HSSF and XSSF\r
  */\r
@@ -1271,4 +1266,81 @@ public abstract class BaseTestConditionalFormatting {
         sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);\r
         wb.close();\r
     }\r
+\r
+    @Test\r
+    public void testSetCellRangeAddresswithSingleRange() throws Exception {\r
+        Workbook wb = _testDataProvider.createWorkbook();\r
+        final Sheet sheet = wb.createSheet("S1");\r
+        final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();\r
+        assertEquals(0, cf.getNumConditionalFormattings());\r
+        ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");\r
+        cf.addConditionalFormatting(new CellRangeAddress[] {\r
+                CellRangeAddress.valueOf("A1:A5")\r
+        }, rule1);\r
+\r
+        assertEquals(1, cf.getNumConditionalFormattings());\r
+        ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);\r
+        CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();\r
+        assertEquals(1, formattingRanges.length);\r
+        CellRangeAddress formattingRange = formattingRanges[0];\r
+        assertEquals("A1:A5", formattingRange.formatAsString());\r
+\r
+        readCf.setFormattingRanges(new CellRangeAddress[] {\r
+                CellRangeAddress.valueOf("A1:A6")\r
+        });\r
+\r
+        readCf = cf.getConditionalFormattingAt(0);\r
+        formattingRanges = readCf.getFormattingRanges();\r
+        assertEquals(1, formattingRanges.length);\r
+        formattingRange = formattingRanges[0];\r
+        assertEquals("A1:A6", formattingRange.formatAsString());\r
+    }\r
+\r
+    @Test\r
+    public void testSetCellRangeAddressWithMultipleRanges() throws Exception {\r
+        Workbook wb = _testDataProvider.createWorkbook();\r
+        final Sheet sheet = wb.createSheet("S1");\r
+        final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();\r
+        assertEquals(0, cf.getNumConditionalFormattings());\r
+        ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");\r
+        cf.addConditionalFormatting(new CellRangeAddress[] {\r
+                CellRangeAddress.valueOf("A1:A5")\r
+        }, rule1);\r
+\r
+        assertEquals(1, cf.getNumConditionalFormattings());\r
+        ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);\r
+        CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();\r
+        assertEquals(1, formattingRanges.length);\r
+        CellRangeAddress formattingRange = formattingRanges[0];\r
+        assertEquals("A1:A5", formattingRange.formatAsString());\r
+\r
+        readCf.setFormattingRanges(new CellRangeAddress[] {\r
+                CellRangeAddress.valueOf("A1:A6"),\r
+                CellRangeAddress.valueOf("B1:B6")\r
+        });\r
+\r
+        readCf = cf.getConditionalFormattingAt(0);\r
+        formattingRanges = readCf.getFormattingRanges();\r
+        assertEquals(2, formattingRanges.length);\r
+        formattingRange = formattingRanges[0];\r
+        assertEquals("A1:A6", formattingRange.formatAsString());\r
+        formattingRange = formattingRanges[1];\r
+        assertEquals("B1:B6", formattingRange.formatAsString());\r
+    }\r
+\r
+    @Test(expected = IllegalArgumentException.class)\r
+    public void testSetCellRangeAddressWithNullRanges() throws Exception {\r
+        Workbook wb = _testDataProvider.createWorkbook();\r
+        final Sheet sheet = wb.createSheet("S1");\r
+        final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();\r
+        assertEquals(0, cf.getNumConditionalFormattings());\r
+        ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");\r
+        cf.addConditionalFormatting(new CellRangeAddress[] {\r
+                CellRangeAddress.valueOf("A1:A5")\r
+        }, rule1);\r
+\r
+        assertEquals(1, cf.getNumConditionalFormattings());\r
+        ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);\r
+        readCf.setFormattingRanges(null);\r
+    }\r
 }
\ No newline at end of file