Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / hssf / usermodel / HSSFSheetConditionalFormatting.java
blob4df94e4b527f0c5c06db568d2fe13ca1717ab54a
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.usermodel;
20 import org.apache.poi.hssf.model.Sheet;
21 import org.apache.poi.hssf.record.CFRuleRecord;
22 import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
23 import org.apache.poi.hssf.util.Region;
25 /**
26 * The 'Conditional Formatting' facet of <tt>HSSFSheet</tt>
28 * @author Dmitriy Kumshayev
30 public final class HSSFSheetConditionalFormatting {
32 private final HSSFWorkbook _workbook;
33 private final Sheet _sheet;
35 /* package */ HSSFSheetConditionalFormatting(HSSFWorkbook workbook, Sheet sheet) {
36 _workbook = workbook;
37 _sheet = sheet;
40 /**
41 * A factory method allowing to create a conditional formatting rule
42 * with a cell comparison operator<p/>
43 * TODO - formulas containing cell references are currently not parsed properly
45 * @param comparisonOperation - a constant value from
46 * <tt>{@link HSSFConditionalFormattingRule.ComparisonOperator}</tt>: <p>
47 * <ul>
48 * <li>BETWEEN</li>
49 * <li>NOT_BETWEEN</li>
50 * <li>EQUAL</li>
51 * <li>NOT_EQUAL</li>
52 * <li>GT</li>
53 * <li>LT</li>
54 * <li>GE</li>
55 * <li>LE</li>
56 * </ul>
57 * </p>
58 * @param formula1 - formula for the valued, compared with the cell
59 * @param formula2 - second formula (only used with
60 * {@link HSSFConditionalFormattingRule#COMPARISON_OPERATOR_BETWEEN}) and
61 * {@link HSSFConditionalFormattingRule#COMPARISON_OPERATOR_NOT_BETWEEN} operations)
63 public HSSFConditionalFormattingRule createConditionalFormattingRule(
64 byte comparisonOperation,
65 String formula1,
66 String formula2) {
68 HSSFWorkbook wb = _workbook;
69 CFRuleRecord rr = CFRuleRecord.create(wb, comparisonOperation, formula1, formula2);
70 return new HSSFConditionalFormattingRule(wb, rr);
73 /**
74 * A factory method allowing to create a conditional formatting rule with a formula.<br>
76 * The formatting rules are applied by Excel when the value of the formula not equal to 0.<p/>
77 * TODO - formulas containing cell references are currently not parsed properly
78 * @param formula - formula for the valued, compared with the cell
80 public HSSFConditionalFormattingRule createConditionalFormattingRule(String formula) {
81 HSSFWorkbook wb = _workbook;
82 CFRuleRecord rr = CFRuleRecord.create(wb, formula);
83 return new HSSFConditionalFormattingRule(wb, rr);
86 /**
87 * Adds a copy of HSSFConditionalFormatting object to the sheet
88 * <p>This method could be used to copy HSSFConditionalFormatting object
89 * from one sheet to another. For example:
90 * <pre>
91 * HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(index);
92 * newSheet.addConditionalFormatting(cf);
93 * </pre>
95 * @param cf HSSFConditionalFormatting object
96 * @return index of the new Conditional Formatting object
98 public int addConditionalFormatting( HSSFConditionalFormatting cf ) {
99 CFRecordsAggregate cfraClone = cf.getCFRecordsAggregate().cloneCFAggregate();
101 return _sheet.addConditionalFormatting(cfraClone);
105 * Allows to add a new Conditional Formatting set to the sheet.
107 * @param regions - list of rectangular regions to apply conditional formatting rules
108 * @param cfRules - set of up to three conditional formatting rules
110 * @return index of the newly created Conditional Formatting object
113 public int addConditionalFormatting(Region[] regions, HSSFConditionalFormattingRule[] cfRules) {
114 if (regions == null) {
115 throw new IllegalArgumentException("regions must not be null");
117 if (cfRules == null) {
118 throw new IllegalArgumentException("cfRules must not be null");
120 if (cfRules.length == 0) {
121 throw new IllegalArgumentException("cfRules must not be empty");
123 if (cfRules.length > 3) {
124 throw new IllegalArgumentException("Number of rules must not exceed 3");
127 CFRuleRecord[] rules = new CFRuleRecord[cfRules.length];
128 for (int i = 0; i != cfRules.length; i++) {
129 rules[i] = cfRules[i].getCfRuleRecord();
131 CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
132 return _sheet.addConditionalFormatting(cfra);
135 public int addConditionalFormatting(Region[] regions,
136 HSSFConditionalFormattingRule rule1)
138 return addConditionalFormatting(regions,
139 new HSSFConditionalFormattingRule[]
141 rule1
145 public int addConditionalFormatting(Region[] regions,
146 HSSFConditionalFormattingRule rule1,
147 HSSFConditionalFormattingRule rule2)
149 return addConditionalFormatting(regions,
150 new HSSFConditionalFormattingRule[]
152 rule1, rule2
156 public int addConditionalFormatting(Region[] regions,
157 HSSFConditionalFormattingRule rule1,
158 HSSFConditionalFormattingRule rule2,
159 HSSFConditionalFormattingRule rule3)
161 return addConditionalFormatting(regions,
162 new HSSFConditionalFormattingRule[]
164 rule1, rule2, rule3
169 * gets Conditional Formatting object at a particular index
171 * @param index
172 * of the Conditional Formatting object to fetch
173 * @return Conditional Formatting object
175 public HSSFConditionalFormatting getConditionalFormattingAt(int index) {
176 CFRecordsAggregate cf = _sheet.getCFRecordsAggregateAt(index);
177 if (cf == null) {
178 return null;
180 return new HSSFConditionalFormatting(_workbook, cf);
184 * @return number of Conditional Formatting objects of the sheet
186 public int getNumConditionalFormattings() {
187 return _sheet.getNumConditionalFormattings();
191 * removes a Conditional Formatting object by index
192 * @param index of a Conditional Formatting object to remove
194 public void removeConditionalFormatting(int index) {
195 _sheet.removeConditionalFormatting(index);