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.

HSSFConditionalFormattingRule.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.model.HSSFFormulaParser;
  17. import org.apache.poi.hssf.record.CFRule12Record;
  18. import org.apache.poi.hssf.record.CFRuleBase;
  19. import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
  20. import org.apache.poi.hssf.record.CFRuleRecord;
  21. import org.apache.poi.hssf.record.cf.BorderFormatting;
  22. import org.apache.poi.hssf.record.cf.FontFormatting;
  23. import org.apache.poi.hssf.record.cf.IconMultiStateFormatting;
  24. import org.apache.poi.hssf.record.cf.PatternFormatting;
  25. import org.apache.poi.ss.formula.ptg.Ptg;
  26. import org.apache.poi.ss.usermodel.ConditionType;
  27. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  28. /**
  29. *
  30. * High level representation of Conditional Formatting Rule.
  31. * It allows to specify formula based conditions for the Conditional Formatting
  32. * and the formatting settings such as font, border and pattern.
  33. */
  34. public final class HSSFConditionalFormattingRule implements ConditionalFormattingRule {
  35. private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS;
  36. private final CFRuleBase cfRuleRecord;
  37. private final HSSFWorkbook workbook;
  38. private final HSSFSheet sheet;
  39. HSSFConditionalFormattingRule(HSSFSheet pSheet, CFRuleBase pRuleRecord) {
  40. if (pSheet == null) {
  41. throw new IllegalArgumentException("pSheet must not be null");
  42. }
  43. if (pRuleRecord == null) {
  44. throw new IllegalArgumentException("pRuleRecord must not be null");
  45. }
  46. sheet = pSheet;
  47. workbook = pSheet.getWorkbook();
  48. cfRuleRecord = pRuleRecord;
  49. }
  50. CFRuleBase getCfRuleRecord()
  51. {
  52. return cfRuleRecord;
  53. }
  54. private HSSFFontFormatting getFontFormatting(boolean create)
  55. {
  56. FontFormatting fontFormatting = cfRuleRecord.getFontFormatting();
  57. if ( fontFormatting != null)
  58. {
  59. cfRuleRecord.setFontFormatting(fontFormatting);
  60. return new HSSFFontFormatting(cfRuleRecord, workbook);
  61. }
  62. else if( create )
  63. {
  64. fontFormatting = new FontFormatting();
  65. cfRuleRecord.setFontFormatting(fontFormatting);
  66. return new HSSFFontFormatting(cfRuleRecord, workbook);
  67. }
  68. else
  69. {
  70. return null;
  71. }
  72. }
  73. /**
  74. * @return - font formatting object if defined, <code>null</code> otherwise
  75. */
  76. public HSSFFontFormatting getFontFormatting()
  77. {
  78. return getFontFormatting(false);
  79. }
  80. /**
  81. * create a new font formatting structure if it does not exist,
  82. * otherwise just return existing object.
  83. * @return - font formatting object, never returns <code>null</code>.
  84. */
  85. public HSSFFontFormatting createFontFormatting()
  86. {
  87. return getFontFormatting(true);
  88. }
  89. private HSSFBorderFormatting getBorderFormatting(boolean create)
  90. {
  91. BorderFormatting borderFormatting = cfRuleRecord.getBorderFormatting();
  92. if ( borderFormatting != null)
  93. {
  94. cfRuleRecord.setBorderFormatting(borderFormatting);
  95. return new HSSFBorderFormatting(cfRuleRecord, workbook);
  96. }
  97. else if( create )
  98. {
  99. borderFormatting = new BorderFormatting();
  100. cfRuleRecord.setBorderFormatting(borderFormatting);
  101. return new HSSFBorderFormatting(cfRuleRecord, workbook);
  102. }
  103. else
  104. {
  105. return null;
  106. }
  107. }
  108. /**
  109. * @return - border formatting object if defined, <code>null</code> otherwise
  110. */
  111. public HSSFBorderFormatting getBorderFormatting()
  112. {
  113. return getBorderFormatting(false);
  114. }
  115. /**
  116. * create a new border formatting structure if it does not exist,
  117. * otherwise just return existing object.
  118. * @return - border formatting object, never returns <code>null</code>.
  119. */
  120. public HSSFBorderFormatting createBorderFormatting()
  121. {
  122. return getBorderFormatting(true);
  123. }
  124. private HSSFPatternFormatting getPatternFormatting(boolean create)
  125. {
  126. PatternFormatting patternFormatting = cfRuleRecord.getPatternFormatting();
  127. if ( patternFormatting != null)
  128. {
  129. cfRuleRecord.setPatternFormatting(patternFormatting);
  130. return new HSSFPatternFormatting(cfRuleRecord, workbook);
  131. }
  132. else if( create )
  133. {
  134. patternFormatting = new PatternFormatting();
  135. cfRuleRecord.setPatternFormatting(patternFormatting);
  136. return new HSSFPatternFormatting(cfRuleRecord, workbook);
  137. }
  138. else
  139. {
  140. return null;
  141. }
  142. }
  143. /**
  144. * @return - pattern formatting object if defined, <code>null</code> otherwise
  145. */
  146. public HSSFPatternFormatting getPatternFormatting()
  147. {
  148. return getPatternFormatting(false);
  149. }
  150. /**
  151. * create a new pattern formatting structure if it does not exist,
  152. * otherwise just return existing object.
  153. * @return - pattern formatting object, never returns <code>null</code>.
  154. */
  155. public HSSFPatternFormatting createPatternFormatting()
  156. {
  157. return getPatternFormatting(true);
  158. }
  159. private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) {
  160. if (cfRuleRecord instanceof CFRule12Record) {
  161. // Good
  162. } else {
  163. if (create) throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
  164. return null;
  165. }
  166. CFRule12Record cfRule12Record = (CFRule12Record)cfRuleRecord;
  167. IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting();
  168. if (iconFormatting != null)
  169. {
  170. return new HSSFIconMultiStateFormatting(cfRule12Record, sheet);
  171. }
  172. else if( create )
  173. {
  174. iconFormatting = cfRule12Record.createMultiStateFormatting();
  175. return new HSSFIconMultiStateFormatting(cfRule12Record, sheet);
  176. }
  177. else
  178. {
  179. return null;
  180. }
  181. }
  182. /**
  183. * @return icon / multi-state formatting object if defined, <code>null</code> otherwise
  184. */
  185. public HSSFIconMultiStateFormatting getMultiStateFormatting() {
  186. return getMultiStateFormatting(false);
  187. }
  188. /**
  189. * create a new icon / multi-state formatting object if it does not exist,
  190. * otherwise just return the existing object.
  191. */
  192. public HSSFIconMultiStateFormatting createMultiStateFormatting() {
  193. return getMultiStateFormatting(true);
  194. }
  195. /**
  196. * @return - the conditiontype for the cfrule
  197. */
  198. public byte getConditionType() {
  199. return cfRuleRecord.getConditionType();
  200. }
  201. /**
  202. * @return - the conditiontype for the cfrule
  203. */
  204. public ConditionType getConditionTypeType() {
  205. return ConditionType.forId(getConditionType());
  206. }
  207. /**
  208. * @return - the comparisionoperatation for the cfrule
  209. */
  210. public byte getComparisonOperation() {
  211. return cfRuleRecord.getComparisonOperation();
  212. }
  213. public String getFormula1()
  214. {
  215. return toFormulaString(cfRuleRecord.getParsedExpression1());
  216. }
  217. public String getFormula2() {
  218. byte conditionType = cfRuleRecord.getConditionType();
  219. if (conditionType == CELL_COMPARISON) {
  220. byte comparisonOperation = cfRuleRecord.getComparisonOperation();
  221. switch(comparisonOperation) {
  222. case ComparisonOperator.BETWEEN:
  223. case ComparisonOperator.NOT_BETWEEN:
  224. return toFormulaString(cfRuleRecord.getParsedExpression2());
  225. }
  226. }
  227. return null;
  228. }
  229. protected String toFormulaString(Ptg[] parsedExpression) {
  230. return toFormulaString(parsedExpression, workbook);
  231. }
  232. protected static String toFormulaString(Ptg[] parsedExpression, HSSFWorkbook workbook) {
  233. if(parsedExpression == null || parsedExpression.length == 0) {
  234. return null;
  235. }
  236. return HSSFFormulaParser.toFormulaString(workbook, parsedExpression);
  237. }
  238. }