Adding a new method to the XSSFConditionalFormatting class #42

Closed
wants to merge 3 commits into
from
View
@@ -0,0 +1,47 @@
+# Created by .ignore support plugin (hsz.mobi)
+### JetBrains template
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+# User-specific stuff:
+.idea/workspace.xml
+.idea/tasks.xml
+.idea/dictionaries
+.idea/vcs.xml
+.idea/jsLibraryMappings.xml
+
+# Sensitive or high-churn files:
+.idea/dataSources.ids
+.idea/dataSources.xml
+.idea/dataSources.local.xml
+.idea/sqlDataSources.xml
+.idea/dynamic.xml
+.idea/uiDesigner.xml
+
+# Gradle:
+.idea/gradle.xml
+.idea/libraries
+
+# Mongo Explorer plugin:
+.idea/mongoSettings.xml
+
+## File-based project format:
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+/out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
+
@@ -94,10 +94,17 @@ CFRecordsAggregate getCFRecordsAggregate() {
/**
* @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 void setRule(int idx, HSSFConditionalFormattingRule cfRule) {
cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
}
+ @Override
public void setRule(int idx, ConditionalFormattingRule cfRule){
setRule(idx, (HSSFConditionalFormattingRule)cfRule);
}
@@ -124,13 +132,15 @@ public void addRule(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);
@@ -139,10 +149,12 @@ public HSSFConditionalFormattingRule getRule(int idx) {
/**
* @return number of Conditional Formatting rules.
*/
+ @Override
public int getNumberOfRules() {
return cfAggregate.getNumberOfRules();
}
+ @Override
public String toString() {
return cfAggregate.toString();
}
@@ -81,6 +81,12 @@
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,
* 2007 and later allow unlimited numbers.
@@ -25,6 +25,7 @@
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting;
import java.util.ArrayList;
+import java.util.Collections;
/**
* @author Yegor Kozlov
@@ -33,73 +34,98 @@
private final CTConditionalFormatting _cf;
private final XSSFSheet _sh;
- /*package*/ XSSFConditionalFormatting(XSSFSheet sh){
+ /*package*/ XSSFConditionalFormatting(XSSFSheet sh) {
_cf = CTConditionalFormatting.Factory.newInstance();
_sh = sh;
}
- /*package*/ XSSFConditionalFormatting(XSSFSheet sh, CTConditionalFormatting cf){
+ /*package*/ XSSFConditionalFormatting(
+ XSSFSheet sh, CTConditionalFormatting cf) {
_cf = cf;
_sh = sh;
}
- /*package*/ CTConditionalFormatting getCTConditionalFormatting(){
+ /*package*/ CTConditionalFormatting getCTConditionalFormatting() {
return _cf;
}
/**
- * @return array of <tt>CellRangeAddress</tt>s. Never <code>null</code>
- */
- 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]));
- }
- }
- return lst.toArray(new CellRangeAddress[lst.size()]);
- }
+ * @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 (final String region : regions) {
+ lst.add(CellRangeAddress.valueOf(region));
+ }
+ }
+ return lst.toArray(new CellRangeAddress[lst.size()]);
+ }
- /**
- * Replaces an existing Conditional Formatting rule at position idx.
- * Excel allows to create up to 3 Conditional Formatting rules.
- * This method can be useful to modify existing Conditional Formatting rules.
- *
- * @param idx position of the rule. Should be between 0 and 2.
- * @param cfRule - Conditional Formatting rule
- */
- public void setRule(int idx, ConditionalFormattingRule cfRule){
- XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule;
- _cf.getCfRuleArray(idx).set(xRule.getCTCfRule());
- }
+ @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()));
+ }
- /**
- * Add a Conditional Formatting rule.
- * Excel allows to create up to 3 Conditional Formatting rules.
- *
- * @param cfRule - Conditional Formatting rule
- */
- public void addRule(ConditionalFormattingRule cfRule){
- XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule;
- _cf.addNewCfRule().set(xRule.getCTCfRule());
- }
+ /**
+ * Replaces an existing Conditional Formatting rule at position idx.
+ * Excel allows to create up to 3 Conditional Formatting rules.
+ * This method can be useful to modify existing Conditional Formatting rules.
+ *
+ * @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());
+ }
- /**
- * @return the Conditional Formatting rule at position idx.
- */
- public XSSFConditionalFormattingRule getRule(int idx){
- return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));
- }
+ /**
+ * Add a Conditional Formatting rule.
+ * Excel allows to create up to 3 Conditional Formatting rules.
+ *
+ * @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.
- */
- public int getNumberOfRules(){
- return _cf.sizeOfCfRuleArray();
- }
-
- public String toString() {
- return _cf.toString();
- }
+ /**
+ * @return the Conditional Formatting rule at position idx.
+ */
+ @Override
+ public XSSFConditionalFormattingRule getRule(int idx) {
+ return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));
+ }
+
+ /**
+ * @return number of Conditional Formatting rules.
+ */
+ @Override
+ public int getNumberOfRules() {
+ return _cf.sizeOfCfRuleArray();
+ }
+
+ @Override
+ public String toString() {
+ return _cf.toString();
+ }
}
@@ -18,16 +18,16 @@
*/
package org.apache.poi.xssf.usermodel;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-import java.io.IOException;
-
import org.apache.poi.ss.usermodel.BaseTestConditionalFormatting;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.junit.Test;
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
/**
* XSSF-specific Conditional Formatting tests
*/
Oops, something went wrong.