Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HSSFConditionalFormattingRule.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.ColorGradientFormatting;
  23. import org.apache.poi.hssf.record.cf.DataBarFormatting;
  24. import org.apache.poi.hssf.record.cf.FontFormatting;
  25. import org.apache.poi.hssf.record.cf.IconMultiStateFormatting;
  26. import org.apache.poi.hssf.record.cf.PatternFormatting;
  27. import org.apache.poi.ss.formula.ptg.Ptg;
  28. import org.apache.poi.ss.usermodel.ConditionFilterData;
  29. import org.apache.poi.ss.usermodel.ConditionFilterType;
  30. import org.apache.poi.ss.usermodel.ConditionType;
  31. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  32. /**
  33. *
  34. * High level representation of Conditional Formatting Rule.
  35. * It allows to specify formula based conditions for the Conditional Formatting
  36. * and the formatting settings such as font, border and pattern.
  37. */
  38. public final class HSSFConditionalFormattingRule implements ConditionalFormattingRule {
  39. private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS;
  40. private final CFRuleBase cfRuleRecord;
  41. private final HSSFWorkbook workbook;
  42. private final HSSFSheet sheet;
  43. HSSFConditionalFormattingRule(HSSFSheet pSheet, CFRuleBase pRuleRecord) {
  44. if (pSheet == null) {
  45. throw new IllegalArgumentException("pSheet must not be null");
  46. }
  47. if (pRuleRecord == null) {
  48. throw new IllegalArgumentException("pRuleRecord must not be null");
  49. }
  50. sheet = pSheet;
  51. workbook = pSheet.getWorkbook();
  52. cfRuleRecord = pRuleRecord;
  53. }
  54. /**
  55. * we don't know priority for these, other than definition/model order, which appears to be what Excel uses.
  56. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getPriority()
  57. */
  58. public int getPriority() {
  59. return 0;
  60. }
  61. /**
  62. * Always true for HSSF files, per Microsoft Excel documentation
  63. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getStopIfTrue()
  64. */
  65. public boolean getStopIfTrue() {
  66. return true;
  67. }
  68. CFRuleBase getCfRuleRecord() {
  69. return cfRuleRecord;
  70. }
  71. private CFRule12Record getCFRule12Record(boolean create) {
  72. if (cfRuleRecord instanceof CFRule12Record) {
  73. // Good
  74. } else {
  75. if (create) throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
  76. return null;
  77. }
  78. return (CFRule12Record)cfRuleRecord;
  79. }
  80. private HSSFFontFormatting getFontFormatting(boolean create) {
  81. FontFormatting fontFormatting = cfRuleRecord.getFontFormatting();
  82. if (fontFormatting == null) {
  83. if (!create) return null;
  84. fontFormatting = new FontFormatting();
  85. cfRuleRecord.setFontFormatting(fontFormatting);
  86. }
  87. return new HSSFFontFormatting(cfRuleRecord, workbook);
  88. }
  89. /**
  90. * @return - font formatting object if defined, <code>null</code> otherwise
  91. */
  92. public HSSFFontFormatting getFontFormatting() {
  93. return getFontFormatting(false);
  94. }
  95. /**
  96. * create a new font formatting structure if it does not exist,
  97. * otherwise just return existing object.
  98. * @return - font formatting object, never returns <code>null</code>.
  99. */
  100. public HSSFFontFormatting createFontFormatting() {
  101. return getFontFormatting(true);
  102. }
  103. private HSSFBorderFormatting getBorderFormatting(boolean create) {
  104. BorderFormatting borderFormatting = cfRuleRecord.getBorderFormatting();
  105. if (borderFormatting == null) {
  106. if (!create) return null;
  107. borderFormatting = new BorderFormatting();
  108. cfRuleRecord.setBorderFormatting(borderFormatting);
  109. }
  110. return new HSSFBorderFormatting(cfRuleRecord, workbook);
  111. }
  112. /**
  113. * @return - border formatting object if defined, <code>null</code> otherwise
  114. */
  115. public HSSFBorderFormatting getBorderFormatting() {
  116. return getBorderFormatting(false);
  117. }
  118. /**
  119. * create a new border formatting structure if it does not exist,
  120. * otherwise just return existing object.
  121. * @return - border formatting object, never returns <code>null</code>.
  122. */
  123. public HSSFBorderFormatting createBorderFormatting() {
  124. return getBorderFormatting(true);
  125. }
  126. private HSSFPatternFormatting getPatternFormatting(boolean create) {
  127. PatternFormatting patternFormatting = cfRuleRecord.getPatternFormatting();
  128. if (patternFormatting == null) {
  129. if (!create) return null;
  130. patternFormatting = new PatternFormatting();
  131. cfRuleRecord.setPatternFormatting(patternFormatting);
  132. }
  133. return new HSSFPatternFormatting(cfRuleRecord, workbook);
  134. }
  135. /**
  136. * @return - pattern formatting object if defined, <code>null</code> otherwise
  137. */
  138. public HSSFPatternFormatting getPatternFormatting()
  139. {
  140. return getPatternFormatting(false);
  141. }
  142. /**
  143. * create a new pattern formatting structure if it does not exist,
  144. * otherwise just return existing object.
  145. * @return - pattern formatting object, never returns <code>null</code>.
  146. */
  147. public HSSFPatternFormatting createPatternFormatting()
  148. {
  149. return getPatternFormatting(true);
  150. }
  151. private HSSFDataBarFormatting getDataBarFormatting(boolean create) {
  152. CFRule12Record cfRule12Record = getCFRule12Record(create);
  153. if (cfRule12Record == null) return null;
  154. DataBarFormatting databarFormatting = cfRule12Record.getDataBarFormatting();
  155. if (databarFormatting == null) {
  156. if (!create) return null;
  157. cfRule12Record.createDataBarFormatting();
  158. }
  159. return new HSSFDataBarFormatting(cfRule12Record, sheet);
  160. }
  161. /**
  162. * @return databar / data-bar formatting object if defined, <code>null</code> otherwise
  163. */
  164. public HSSFDataBarFormatting getDataBarFormatting() {
  165. return getDataBarFormatting(false);
  166. }
  167. /**
  168. * create a new databar / data-bar formatting object if it does not exist,
  169. * otherwise just return the existing object.
  170. */
  171. public HSSFDataBarFormatting createDataBarFormatting() {
  172. return getDataBarFormatting(true);
  173. }
  174. private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) {
  175. CFRule12Record cfRule12Record = getCFRule12Record(create);
  176. if (cfRule12Record == null) return null;
  177. IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting();
  178. if (iconFormatting == null) {
  179. if (!create) return null;
  180. cfRule12Record.createMultiStateFormatting();
  181. }
  182. return new HSSFIconMultiStateFormatting(cfRule12Record, sheet);
  183. }
  184. /**
  185. * @return icon / multi-state formatting object if defined, <code>null</code> otherwise
  186. */
  187. public HSSFIconMultiStateFormatting getMultiStateFormatting() {
  188. return getMultiStateFormatting(false);
  189. }
  190. /**
  191. * create a new icon / multi-state formatting object if it does not exist,
  192. * otherwise just return the existing object.
  193. */
  194. public HSSFIconMultiStateFormatting createMultiStateFormatting() {
  195. return getMultiStateFormatting(true);
  196. }
  197. private HSSFColorScaleFormatting getColorScaleFormatting(boolean create) {
  198. CFRule12Record cfRule12Record = getCFRule12Record(create);
  199. if (cfRule12Record == null) return null;
  200. ColorGradientFormatting colorFormatting = cfRule12Record.getColorGradientFormatting();
  201. if (colorFormatting == null) {
  202. if (!create) return null;
  203. cfRule12Record.createColorGradientFormatting();
  204. }
  205. return new HSSFColorScaleFormatting(cfRule12Record, sheet);
  206. }
  207. /**
  208. * @return color scale / gradient formatting object if defined, <code>null</code> otherwise
  209. */
  210. public HSSFColorScaleFormatting getColorScaleFormatting() {
  211. return getColorScaleFormatting(false);
  212. }
  213. /**
  214. * create a new color scale / gradient formatting object if it does not exist,
  215. * otherwise just return the existing object.
  216. */
  217. public HSSFColorScaleFormatting createColorScaleFormatting() {
  218. return getColorScaleFormatting(true);
  219. }
  220. /**
  221. * @return - the conditiontype for the cfrule
  222. */
  223. @Override
  224. public ConditionType getConditionType() {
  225. byte code = cfRuleRecord.getConditionType();
  226. return ConditionType.forId(code);
  227. }
  228. /**
  229. * always null (not a filter condition) or {@link ConditionFilterType#FILTER} if it is.
  230. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getConditionFilterType()
  231. */
  232. public ConditionFilterType getConditionFilterType() {
  233. return getConditionType() == ConditionType.FILTER ? ConditionFilterType.FILTER : null;
  234. }
  235. public ConditionFilterData getFilterConfiguration() {
  236. return null;
  237. }
  238. /**
  239. * @return - the comparisionoperatation for the cfrule
  240. */
  241. @Override
  242. public byte getComparisonOperation() {
  243. return cfRuleRecord.getComparisonOperation();
  244. }
  245. public String getFormula1()
  246. {
  247. return toFormulaString(cfRuleRecord.getParsedExpression1());
  248. }
  249. public String getFormula2() {
  250. byte conditionType = cfRuleRecord.getConditionType();
  251. if (conditionType == CELL_COMPARISON) {
  252. byte comparisonOperation = cfRuleRecord.getComparisonOperation();
  253. switch(comparisonOperation) {
  254. case ComparisonOperator.BETWEEN:
  255. case ComparisonOperator.NOT_BETWEEN:
  256. return toFormulaString(cfRuleRecord.getParsedExpression2());
  257. }
  258. }
  259. return null;
  260. }
  261. protected String toFormulaString(Ptg[] parsedExpression) {
  262. return toFormulaString(parsedExpression, workbook);
  263. }
  264. protected static String toFormulaString(Ptg[] parsedExpression, HSSFWorkbook workbook) {
  265. if(parsedExpression == null || parsedExpression.length == 0) {
  266. return null;
  267. }
  268. return HSSFFormulaParser.toFormulaString(workbook, parsedExpression);
  269. }
  270. }