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
@ -33,73 +34,98 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
private final CTConditionalFormatting _cf; private final CTConditionalFormatting _cf;
private final XSSFSheet _sh; private final XSSFSheet _sh;
/*package*/ XSSFConditionalFormatting(XSSFSheet sh){ /*package*/ XSSFConditionalFormatting(XSSFSheet sh) {
_cf = CTConditionalFormatting.Factory.newInstance(); _cf = CTConditionalFormatting.Factory.newInstance();
_sh = sh; _sh = sh;
} }
/*package*/ XSSFConditionalFormatting(XSSFSheet sh, CTConditionalFormatting cf){ /*package*/ XSSFConditionalFormatting(
XSSFSheet sh, CTConditionalFormatting cf) {
_cf = cf; _cf = cf;
_sh = sh; _sh = sh;
} }
/*package*/ CTConditionalFormatting getCTConditionalFormatting(){ /*package*/ CTConditionalFormatting getCTConditionalFormatting() {
return _cf; return _cf;
} }
/** /**
* @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code> * @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code>
*/ */
public CellRangeAddress[] getFormattingRanges(){ @Override
ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>(); public CellRangeAddress[] getFormattingRanges() {
for (Object stRef : _cf.getSqref()) { ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>();
String[] regions = stRef.toString().split(" "); for (Object stRef : _cf.getSqref()) {
for (int i = 0; i < regions.length; i++) { String[] regions = stRef.toString().split(" ");
lst.add(CellRangeAddress.valueOf(regions[i])); for (final String region : regions) {
} lst.add(CellRangeAddress.valueOf(region));
} }
return lst.toArray(new CellRangeAddress[lst.size()]); }
} return lst.toArray(new CellRangeAddress[lst.size()]);
}
/** @Override
* Replaces an existing Conditional Formatting rule at position idx. public void setFormattingRanges(CellRangeAddress[] ranges) {
* Excel allows to create up to 3 Conditional Formatting rules. if (ranges == null) {
* This method can be useful to modify existing Conditional Formatting rules. throw new IllegalArgumentException("cellRanges must not be null");
* }
* @param idx position of the rule. Should be between 0 and 2. final StringBuilder sb = new StringBuilder();
* @param cfRule - Conditional Formatting rule boolean first = true;
*/ for (CellRangeAddress range : ranges) {
public void setRule(int idx, ConditionalFormattingRule cfRule){ if (!first) {
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule; sb.append(" ");
_cf.getCfRuleArray(idx).set(xRule.getCTCfRule()); } else {
} first = false;
}
sb.append(range.formatAsString());
}
_cf.setSqref(Collections.singletonList(sb.toString()));
}
/** /**
* Add a Conditional Formatting rule. * 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.
* * This method can be useful to modify existing Conditional Formatting rules.
* @param cfRule - Conditional Formatting rule *
*/ * @param idx position of the rule. Should be between 0 and 2.
public void addRule(ConditionalFormattingRule cfRule){ * @param cfRule - Conditional Formatting rule
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule; */
_cf.addNewCfRule().set(xRule.getCTCfRule()); @Override
} public void setRule(int idx, ConditionalFormattingRule cfRule) {
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
_cf.getCfRuleArray(idx).set(xRule.getCTCfRule());
}
/** /**
* @return the Conditional Formatting rule at position idx. * Add a Conditional Formatting rule.
*/ * Excel allows to create up to 3 Conditional Formatting rules.
public XSSFConditionalFormattingRule getRule(int idx){ *
return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx)); * @param cfRule - Conditional Formatting rule
} */
@Override
public void addRule(ConditionalFormattingRule cfRule) {
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
_cf.addNewCfRule().set(xRule.getCTCfRule());
}
/** /**
* @return number of Conditional Formatting rules. * @return the Conditional Formatting rule at position idx.
*/ */
public int getNumberOfRules(){ @Override
return _cf.sizeOfCfRuleArray(); public XSSFConditionalFormattingRule getRule(int idx) {
} return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));
}
public String toString() { /**
return _cf.toString(); * @return number of Conditional Formatting rules.
} */
@Override
public int getNumberOfRules() {
return _cf.sizeOfCfRuleArray();
}
@Override
public String 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);
}
} }