/**
* @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,
cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
}
+ @Override
public void setRule(int idx, ConditionalFormattingRule cfRule){
setRule(idx, (HSSFConditionalFormattingRule)cfRule);
}
cfAggregate.addRule(cfRule.getCfRuleRecord());
}
+ @Override
public void addRule(ConditionalFormattingRule cfRule){
addRule((HSSFConditionalFormattingRule)cfRule);
}
/**
* @return the Conditional Formatting rule at position idx.
*/
+ @Override
public HSSFConditionalFormattingRule getRule(int idx) {
CFRuleBase ruleRecord = cfAggregate.getRule(idx);
return new HSSFConditionalFormattingRule(sheet, ruleRecord);
/**
* @return number of Conditional Formatting rules.
*/
+ @Override
public int getNumberOfRules() {
return cfAggregate.getNumberOfRules();
}
+ @Override
public String toString() {
return cfAggregate.toString();
}
*/\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
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
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
\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
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
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