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 ==================================================================== */
17 package org
.apache
.poi
.hssf
.usermodel
;
19 import org
.apache
.poi
.hssf
.record
.CFHeaderRecord
;
20 import org
.apache
.poi
.hssf
.record
.CFRuleRecord
;
21 import org
.apache
.poi
.hssf
.record
.aggregates
.CFRecordsAggregate
;
22 import org
.apache
.poi
.hssf
.record
.cf
.CellRange
;
23 import org
.apache
.poi
.hssf
.util
.Region
;
26 * HSSFConditionalFormatting class encapsulates all settings of Conditional Formatting.
28 * The class can be used
32 * to make a copy HSSFConditionalFormatting settings.
38 * HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(index);
39 * newSheet.addConditionalFormatting(cf);
43 * or to modify existing Conditional Formatting settings (formatting regions and/or rules).
47 * Use {@link HSSFSheet#getConditionalFormattingAt(int)} to get access to an instance of this class.
49 * To create a new Conditional Formatting set use the following approach:
53 * // Define a Conditional Formatting rule, which triggers formatting
54 * // when cell's value is greater or equal than 100.0 and
55 * // applies patternFormatting defined below.
56 * HSSFConditionalFormattingRule rule = sheet.createConditionalFormattingRule(
57 * ComparisonOperator.GE,
58 * "100.0", // 1st formula
59 * null // 2nd formula is not used for comparison operator GE
62 * // Create pattern with red background
63 * HSSFPatternFormatting patternFmt = rule.cretePatternFormatting();
64 * patternFormatting.setFillBackgroundColor(HSSFColor.RED.index);
66 * // Define a region containing first column
69 * new Region(1,(short)1,-1,(short)1)
72 * // Apply Conditional Formatting rule defined above to the regions
73 * sheet.addConditionalFormatting(regions, rule);
76 * @author Dmitriy Kumshayev
78 public final class HSSFConditionalFormatting
80 private final HSSFWorkbook _workbook
;
81 private final CFRecordsAggregate cfAggregate
;
83 HSSFConditionalFormatting(HSSFWorkbook workbook
, CFRecordsAggregate cfAggregate
)
85 if(workbook
== null) {
86 throw new IllegalArgumentException("workbook must not be null");
88 if(cfAggregate
== null) {
89 throw new IllegalArgumentException("cfAggregate must not be null");
92 this.cfAggregate
= cfAggregate
;
94 CFRecordsAggregate
getCFRecordsAggregate() {
99 * @return array of <tt>Region</tt>s. never <code>null</code>
101 public Region
[] getFormattingRegions()
103 CFHeaderRecord cfh
= cfAggregate
.getHeader();
104 CellRange
[] cellRanges
= cfh
.getCellRanges();
105 return CellRange
.convertCellRangesToRegions(cellRanges
);
109 * Replaces an existing Conditional Formatting rule at position idx.
110 * Excel allows to create up to 3 Conditional Formatting rules.
111 * This method can be useful to modify existing Conditional Formatting rules.
113 * @param idx position of the rule. Should be between 0 and 2.
114 * @param cfRule - Conditional Formatting rule
116 public void setRule(int idx
, HSSFConditionalFormattingRule cfRule
)
118 cfAggregate
.setRule(idx
, cfRule
.getCfRuleRecord());
122 * add a Conditional Formatting rule.
123 * Excel allows to create up to 3 Conditional Formatting rules.
124 * @param cfRule - Conditional Formatting rule
126 public void addRule(HSSFConditionalFormattingRule cfRule
)
128 cfAggregate
.addRule(cfRule
.getCfRuleRecord());
132 * @return the Conditional Formatting rule at position idx.
134 public HSSFConditionalFormattingRule
getRule(int idx
)
136 CFRuleRecord ruleRecord
= cfAggregate
.getRule(idx
);
137 return new HSSFConditionalFormattingRule(_workbook
, ruleRecord
);
141 * @return number of Conditional Formatting rules.
143 public int getNumberOfRules()
145 return cfAggregate
.getNumberOfRules();
148 public String
toString()
150 return cfAggregate
.toString();