Add setFormattingRanges() to interface ConditionalFormatting, closes #42

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1768588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-11-07 21:32:04 +00:00
parent 8d083d8c64
commit 76a0c6378e
4 changed files with 178 additions and 62 deletions

View File

@ -94,10 +94,17 @@ public final class HSSFConditionalFormatting implements ConditionalFormatting {
/** /**
* @return array of <tt>CellRangeAddress</tt>s. never <code>null</code> * @return array of <tt>CellRangeAddress</tt>s. never <code>null</code>
*/ */
@Override
public CellRangeAddress[] getFormattingRanges() { public CellRangeAddress[] getFormattingRanges() {
return cfAggregate.getHeader().getCellRanges(); return cfAggregate.getHeader().getCellRanges();
} }
@Override
public void setFormattingRanges(
final CellRangeAddress[] ranges) {
cfAggregate.getHeader().setCellRanges(ranges);
}
/** /**
* Replaces an existing Conditional Formatting rule at position idx. * Replaces an existing Conditional Formatting rule at position idx.
* Older versions of Excel only allow up to 3 Conditional Formatting rules, * 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()); cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
} }
@Override
public void setRule(int idx, ConditionalFormattingRule cfRule){ public void setRule(int idx, ConditionalFormattingRule cfRule){
setRule(idx, (HSSFConditionalFormattingRule)cfRule); setRule(idx, (HSSFConditionalFormattingRule)cfRule);
} }
@ -124,6 +132,7 @@ public final class HSSFConditionalFormatting implements ConditionalFormatting {
cfAggregate.addRule(cfRule.getCfRuleRecord()); cfAggregate.addRule(cfRule.getCfRuleRecord());
} }
@Override
public void addRule(ConditionalFormattingRule cfRule){ public void addRule(ConditionalFormattingRule cfRule){
addRule((HSSFConditionalFormattingRule)cfRule); addRule((HSSFConditionalFormattingRule)cfRule);
} }
@ -131,6 +140,7 @@ public final class HSSFConditionalFormatting implements ConditionalFormatting {
/** /**
* @return the Conditional Formatting rule at position idx. * @return the Conditional Formatting rule at position idx.
*/ */
@Override
public HSSFConditionalFormattingRule getRule(int idx) { public HSSFConditionalFormattingRule getRule(int idx) {
CFRuleBase ruleRecord = cfAggregate.getRule(idx); CFRuleBase ruleRecord = cfAggregate.getRule(idx);
return new HSSFConditionalFormattingRule(sheet, ruleRecord); return new HSSFConditionalFormattingRule(sheet, ruleRecord);
@ -139,10 +149,12 @@ public final class HSSFConditionalFormatting implements ConditionalFormatting {
/** /**
* @return number of Conditional Formatting rules. * @return number of Conditional Formatting rules.
*/ */
@Override
public int getNumberOfRules() { public int getNumberOfRules() {
return cfAggregate.getNumberOfRules(); return cfAggregate.getNumberOfRules();
} }
@Override
public String toString() { public String toString() {
return cfAggregate.toString(); return cfAggregate.toString();
} }

View File

@ -80,6 +80,12 @@ public interface ConditionalFormatting {
*/ */
CellRangeAddress[] getFormattingRanges(); CellRangeAddress[] getFormattingRanges();
/**
* Sets the cell ranges the rule conditional formatting must be applied to.
* @param ranges non-null array of <tt>CellRangeAddress</tt>s
*/
void setFormattingRanges(CellRangeAddress[] ranges);
/** /**
* Replaces an existing Conditional Formatting rule at position idx. * Replaces an existing Conditional Formatting rule at position idx.
* Excel pre-2007 allows to create up to 3 Conditional Formatting rules, * Excel pre-2007 allows to create up to 3 Conditional Formatting rules,

View File

@ -25,6 +25,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
/** /**
* @author Yegor Kozlov * @author Yegor Kozlov
@ -38,7 +39,8 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
_sh = sh; _sh = sh;
} }
/*package*/ XSSFConditionalFormatting(XSSFSheet sh, CTConditionalFormatting cf){ /*package*/ XSSFConditionalFormatting(
XSSFSheet sh, CTConditionalFormatting cf) {
_cf = cf; _cf = cf;
_sh = sh; _sh = sh;
} }
@ -50,17 +52,36 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
/** /**
* @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code> * @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code>
*/ */
@Override
public CellRangeAddress[] getFormattingRanges() { public CellRangeAddress[] getFormattingRanges() {
ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>(); ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>();
for (Object stRef : _cf.getSqref()) { for (Object stRef : _cf.getSqref()) {
String[] regions = stRef.toString().split(" "); String[] regions = stRef.toString().split(" ");
for (int i = 0; i < regions.length; i++) { for (final String region : regions) {
lst.add(CellRangeAddress.valueOf(regions[i])); lst.add(CellRangeAddress.valueOf(region));
} }
} }
return lst.toArray(new CellRangeAddress[lst.size()]); return lst.toArray(new CellRangeAddress[lst.size()]);
} }
@Override
public void setFormattingRanges(CellRangeAddress[] ranges) {
if (ranges == null) {
throw new IllegalArgumentException("cellRanges must not be null");
}
final StringBuilder sb = new StringBuilder();
boolean first = true;
for (CellRangeAddress range : ranges) {
if (!first) {
sb.append(" ");
} else {
first = false;
}
sb.append(range.formatAsString());
}
_cf.setSqref(Collections.singletonList(sb.toString()));
}
/** /**
* Replaces an existing Conditional Formatting rule at position idx. * Replaces an existing Conditional Formatting rule at position idx.
* Excel allows to create up to 3 Conditional Formatting rules. * Excel allows to create up to 3 Conditional Formatting rules.
@ -69,6 +90,7 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
* @param idx position of the rule. Should be between 0 and 2. * @param idx position of the rule. Should be between 0 and 2.
* @param cfRule - Conditional Formatting rule * @param cfRule - Conditional Formatting rule
*/ */
@Override
public void setRule(int idx, ConditionalFormattingRule cfRule) { public void setRule(int idx, ConditionalFormattingRule cfRule) {
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule; XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
_cf.getCfRuleArray(idx).set(xRule.getCTCfRule()); _cf.getCfRuleArray(idx).set(xRule.getCTCfRule());
@ -80,6 +102,7 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
* *
* @param cfRule - Conditional Formatting rule * @param cfRule - Conditional Formatting rule
*/ */
@Override
public void addRule(ConditionalFormattingRule cfRule) { public void addRule(ConditionalFormattingRule cfRule) {
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule; XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
_cf.addNewCfRule().set(xRule.getCTCfRule()); _cf.addNewCfRule().set(xRule.getCTCfRule());
@ -88,6 +111,7 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
/** /**
* @return the Conditional Formatting rule at position idx. * @return the Conditional Formatting rule at position idx.
*/ */
@Override
public XSSFConditionalFormattingRule getRule(int idx) { public XSSFConditionalFormattingRule getRule(int idx) {
return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx)); return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));
} }
@ -95,10 +119,12 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
/** /**
* @return number of Conditional Formatting rules. * @return number of Conditional Formatting rules.
*/ */
@Override
public int getNumberOfRules() { public int getNumberOfRules() {
return _cf.sizeOfCfRuleArray(); return _cf.sizeOfCfRuleArray();
} }
@Override
public String toString() { public String toString() {
return _cf.toString(); return _cf.toString();
} }

View File

@ -19,15 +19,6 @@
package org.apache.poi.ss.usermodel; package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting; import org.apache.poi.hssf.usermodel.HSSFConditionalFormatting;
import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule; import org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule;
import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.ITestDataProvider;
@ -36,6 +27,10 @@ import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.Test; import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.*;
/** /**
* Base tests for Conditional Formatting, for both HSSF and XSSF * Base tests for Conditional Formatting, for both HSSF and XSSF
*/ */
@ -1271,4 +1266,81 @@ public abstract class BaseTestConditionalFormatting {
sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule); sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
wb.close(); wb.close();
} }
@Test
public void testSetCellRangeAddresswithSingleRange() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
final Sheet sheet = wb.createSheet("S1");
final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
assertEquals(0, cf.getNumConditionalFormattings());
ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
cf.addConditionalFormatting(new CellRangeAddress[] {
CellRangeAddress.valueOf("A1:A5")
}, rule1);
assertEquals(1, cf.getNumConditionalFormattings());
ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();
assertEquals(1, formattingRanges.length);
CellRangeAddress formattingRange = formattingRanges[0];
assertEquals("A1:A5", formattingRange.formatAsString());
readCf.setFormattingRanges(new CellRangeAddress[] {
CellRangeAddress.valueOf("A1:A6")
});
readCf = cf.getConditionalFormattingAt(0);
formattingRanges = readCf.getFormattingRanges();
assertEquals(1, formattingRanges.length);
formattingRange = formattingRanges[0];
assertEquals("A1:A6", formattingRange.formatAsString());
}
@Test
public void testSetCellRangeAddressWithMultipleRanges() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
final Sheet sheet = wb.createSheet("S1");
final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
assertEquals(0, cf.getNumConditionalFormattings());
ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
cf.addConditionalFormatting(new CellRangeAddress[] {
CellRangeAddress.valueOf("A1:A5")
}, rule1);
assertEquals(1, cf.getNumConditionalFormattings());
ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
CellRangeAddress[] formattingRanges = readCf.getFormattingRanges();
assertEquals(1, formattingRanges.length);
CellRangeAddress formattingRange = formattingRanges[0];
assertEquals("A1:A5", formattingRange.formatAsString());
readCf.setFormattingRanges(new CellRangeAddress[] {
CellRangeAddress.valueOf("A1:A6"),
CellRangeAddress.valueOf("B1:B6")
});
readCf = cf.getConditionalFormattingAt(0);
formattingRanges = readCf.getFormattingRanges();
assertEquals(2, formattingRanges.length);
formattingRange = formattingRanges[0];
assertEquals("A1:A6", formattingRange.formatAsString());
formattingRange = formattingRanges[1];
assertEquals("B1:B6", formattingRange.formatAsString());
}
@Test(expected = IllegalArgumentException.class)
public void testSetCellRangeAddressWithNullRanges() throws Exception {
Workbook wb = _testDataProvider.createWorkbook();
final Sheet sheet = wb.createSheet("S1");
final SheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
assertEquals(0, cf.getNumConditionalFormattings());
ConditionalFormattingRule rule1 = cf.createConditionalFormattingRule("$A$1>0");
cf.addConditionalFormatting(new CellRangeAddress[] {
CellRangeAddress.valueOf("A1:A5")
}, rule1);
assertEquals(1, cf.getNumConditionalFormattings());
ConditionalFormatting readCf = cf.getConditionalFormattingAt(0);
readCf.setFormattingRanges(null);
}
} }