You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HSSFConditionalFormatting.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import org.apache.poi.hssf.record.CFRuleBase;
  17. import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
  18. import org.apache.poi.ss.usermodel.ConditionalFormatting;
  19. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  20. import org.apache.poi.ss.util.CellRangeAddress;
  21. /**
  22. * HSSFConditionalFormatting class encapsulates all settings of Conditional Formatting.
  23. *
  24. * The class can be used
  25. *
  26. * <UL>
  27. * <LI>
  28. * to make a copy HSSFConditionalFormatting settings.
  29. * </LI>
  30. *
  31. *
  32. * For example:
  33. * <PRE>
  34. * HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(index);
  35. * newSheet.addConditionalFormatting(cf);
  36. * </PRE>
  37. *
  38. * <LI>
  39. * or to modify existing Conditional Formatting settings (formatting regions and/or rules).
  40. * </LI>
  41. * </UL>
  42. *
  43. * Use {@link org.apache.poi.hssf.usermodel.HSSFSheet#getSheetConditionalFormatting()} to get access to an instance of this class.
  44. * <P>
  45. * To create a new Conditional Formatting set use the following approach:
  46. *
  47. * <PRE>
  48. *
  49. * // Define a Conditional Formatting rule, which triggers formatting
  50. * // when cell's value is greater or equal than 100.0 and
  51. * // applies patternFormatting defined below.
  52. * HSSFConditionalFormattingRule rule = sheet.createConditionalFormattingRule(
  53. * ComparisonOperator.GE,
  54. * "100.0", // 1st formula
  55. * null // 2nd formula is not used for comparison operator GE
  56. * );
  57. *
  58. * // Create pattern with red background
  59. * HSSFPatternFormatting patternFmt = rule.cretePatternFormatting();
  60. * patternFormatting.setFillBackgroundColor(HSSFColor.RED.index);
  61. *
  62. * // Define a region containing first column
  63. * Region [] regions =
  64. * {
  65. * new Region(1,(short)1,-1,(short)1)
  66. * };
  67. *
  68. * // Apply Conditional Formatting rule defined above to the regions
  69. * sheet.addConditionalFormatting(regions, rule);
  70. * </PRE>
  71. */
  72. public final class HSSFConditionalFormatting implements ConditionalFormatting {
  73. private final HSSFSheet sheet;
  74. private final CFRecordsAggregate cfAggregate;
  75. HSSFConditionalFormatting(HSSFSheet sheet, CFRecordsAggregate cfAggregate) {
  76. if(sheet == null) {
  77. throw new IllegalArgumentException("sheet must not be null");
  78. }
  79. if(cfAggregate == null) {
  80. throw new IllegalArgumentException("cfAggregate must not be null");
  81. }
  82. this.sheet = sheet;
  83. this.cfAggregate = cfAggregate;
  84. }
  85. CFRecordsAggregate getCFRecordsAggregate() {
  86. return cfAggregate;
  87. }
  88. /**
  89. * @deprecated (Aug-2008) use {@link HSSFConditionalFormatting#getFormattingRanges()}
  90. */
  91. public org.apache.poi.ss.util.Region[] getFormattingRegions() {
  92. CellRangeAddress[] cellRanges = getFormattingRanges();
  93. return org.apache.poi.ss.util.Region.convertCellRangesToRegions(cellRanges);
  94. }
  95. /**
  96. * @return array of <tt>CellRangeAddress</tt>s. never <code>null</code>
  97. */
  98. public CellRangeAddress[] getFormattingRanges() {
  99. return cfAggregate.getHeader().getCellRanges();
  100. }
  101. /**
  102. * Replaces an existing Conditional Formatting rule at position idx.
  103. * Older versions of Excel only allow up to 3 Conditional Formatting rules,
  104. * and will ignore rules beyond that, while newer versions are fine.
  105. * This method can be useful to modify existing Conditional Formatting rules.
  106. *
  107. * @param idx position of the rule. Should be between 0 and 2 for older Excel versions
  108. * @param cfRule - Conditional Formatting rule
  109. */
  110. public void setRule(int idx, HSSFConditionalFormattingRule cfRule) {
  111. cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
  112. }
  113. public void setRule(int idx, ConditionalFormattingRule cfRule){
  114. setRule(idx, (HSSFConditionalFormattingRule)cfRule);
  115. }
  116. /**
  117. * add a Conditional Formatting rule.
  118. * Excel allows to create up to 3 Conditional Formatting rules.
  119. * @param cfRule - Conditional Formatting rule
  120. */
  121. public void addRule(HSSFConditionalFormattingRule cfRule) {
  122. cfAggregate.addRule(cfRule.getCfRuleRecord());
  123. }
  124. public void addRule(ConditionalFormattingRule cfRule){
  125. addRule((HSSFConditionalFormattingRule)cfRule);
  126. }
  127. /**
  128. * @return the Conditional Formatting rule at position idx.
  129. */
  130. public HSSFConditionalFormattingRule getRule(int idx) {
  131. CFRuleBase ruleRecord = cfAggregate.getRule(idx);
  132. return new HSSFConditionalFormattingRule(sheet, ruleRecord);
  133. }
  134. /**
  135. * @return number of Conditional Formatting rules.
  136. */
  137. public int getNumberOfRules() {
  138. return cfAggregate.getNumberOfRules();
  139. }
  140. public String toString() {
  141. return cfAggregate.toString();
  142. }
  143. }