選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HSSFConditionalFormattingRule.java 12KB

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