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:
parent
8d083d8c64
commit
76a0c6378e
@ -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();
|
||||
}
|
||||
|
@ -80,6 +80,12 @@ public interface ConditionalFormatting {
|
||||
*/
|
||||
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.
|
||||
* Excel pre-2007 allows to create up to 3 Conditional Formatting rules,
|
||||
|
@ -25,6 +25,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author Yegor Kozlov
|
||||
@ -38,7 +39,8 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
|
||||
_sh = sh;
|
||||
}
|
||||
|
||||
/*package*/ XSSFConditionalFormatting(XSSFSheet sh, CTConditionalFormatting cf){
|
||||
/*package*/ XSSFConditionalFormatting(
|
||||
XSSFSheet sh, CTConditionalFormatting cf) {
|
||||
_cf = cf;
|
||||
_sh = sh;
|
||||
}
|
||||
@ -50,17 +52,36 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
|
||||
/**
|
||||
* @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code>
|
||||
*/
|
||||
@Override
|
||||
public CellRangeAddress[] getFormattingRanges() {
|
||||
ArrayList<CellRangeAddress> lst = new ArrayList<CellRangeAddress>();
|
||||
for (Object stRef : _cf.getSqref()) {
|
||||
String[] regions = stRef.toString().split(" ");
|
||||
for (int i = 0; i < regions.length; i++) {
|
||||
lst.add(CellRangeAddress.valueOf(regions[i]));
|
||||
for (final String region : regions) {
|
||||
lst.add(CellRangeAddress.valueOf(region));
|
||||
}
|
||||
}
|
||||
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.
|
||||
* 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 cfRule - Conditional Formatting rule
|
||||
*/
|
||||
@Override
|
||||
public void setRule(int idx, ConditionalFormattingRule cfRule) {
|
||||
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
|
||||
_cf.getCfRuleArray(idx).set(xRule.getCTCfRule());
|
||||
@ -80,6 +102,7 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
|
||||
*
|
||||
* @param cfRule - Conditional Formatting rule
|
||||
*/
|
||||
@Override
|
||||
public void addRule(ConditionalFormattingRule cfRule) {
|
||||
XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
|
||||
_cf.addNewCfRule().set(xRule.getCTCfRule());
|
||||
@ -88,6 +111,7 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
|
||||
/**
|
||||
* @return the Conditional Formatting rule at position idx.
|
||||
*/
|
||||
@Override
|
||||
public XSSFConditionalFormattingRule getRule(int idx) {
|
||||
return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));
|
||||
}
|
||||
@ -95,10 +119,12 @@ public class XSSFConditionalFormatting implements ConditionalFormatting {
|
||||
/**
|
||||
* @return number of Conditional Formatting rules.
|
||||
*/
|
||||
@Override
|
||||
public int getNumberOfRules() {
|
||||
return _cf.sizeOfCfRuleArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return _cf.toString();
|
||||
}
|
||||
|
@ -19,15 +19,6 @@
|
||||
|
||||
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.HSSFConditionalFormattingRule;
|
||||
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.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Base tests for Conditional Formatting, for both HSSF and XSSF
|
||||
*/
|
||||
@ -1271,4 +1266,81 @@ public abstract class BaseTestConditionalFormatting {
|
||||
sheet.getSheetConditionalFormatting().addConditionalFormatting(ranges, rule);
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user