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

HSSFConditionalFormattingRule.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.cf.BorderFormatting;
  21. import org.apache.poi.hssf.record.cf.ColorGradientFormatting;
  22. import org.apache.poi.hssf.record.cf.DataBarFormatting;
  23. import org.apache.poi.hssf.record.cf.FontFormatting;
  24. import org.apache.poi.hssf.record.cf.IconMultiStateFormatting;
  25. import org.apache.poi.hssf.record.cf.PatternFormatting;
  26. import org.apache.poi.ss.formula.ptg.Ptg;
  27. import org.apache.poi.ss.usermodel.ConditionFilterData;
  28. import org.apache.poi.ss.usermodel.ConditionFilterType;
  29. import org.apache.poi.ss.usermodel.ConditionType;
  30. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  31. import org.apache.poi.ss.usermodel.ExcelNumberFormat;
  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. @SuppressWarnings("unused")
  39. public final class HSSFConditionalFormattingRule implements ConditionalFormattingRule {
  40. private static final byte CELL_COMPARISON = CFRuleBase.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. */
  60. @Override
  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. */
  69. @Override
  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. return (CFRule12Record) cfRuleRecord;
  79. }
  80. if (create) {
  81. throw new IllegalArgumentException("Can't convert a CF into a CF12 record");
  82. }
  83. return null;
  84. }
  85. /**
  86. * Always null for HSSF records, until someone figures out where to find it
  87. */
  88. @Override
  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} otherwise
  103. */
  104. @Override
  105. public HSSFFontFormatting getFontFormatting() {
  106. return getFontFormatting(false);
  107. }
  108. /**
  109. * create a new font formatting structure if it does not exist,
  110. * otherwise just return existing object.
  111. * @return - font formatting object, never returns {@code null}.
  112. */
  113. @Override
  114. public HSSFFontFormatting createFontFormatting() {
  115. return getFontFormatting(true);
  116. }
  117. private HSSFBorderFormatting getBorderFormatting(boolean create) {
  118. BorderFormatting borderFormatting = cfRuleRecord.getBorderFormatting();
  119. if (borderFormatting == null) {
  120. if (!create) return null;
  121. borderFormatting = new BorderFormatting();
  122. cfRuleRecord.setBorderFormatting(borderFormatting);
  123. }
  124. return new HSSFBorderFormatting(cfRuleRecord, workbook);
  125. }
  126. /**
  127. * @return - border formatting object if defined, {@code null} otherwise
  128. */
  129. @Override
  130. public HSSFBorderFormatting getBorderFormatting() {
  131. return getBorderFormatting(false);
  132. }
  133. /**
  134. * create a new border formatting structure if it does not exist,
  135. * otherwise just return existing object.
  136. * @return - border formatting object, never returns {@code null}.
  137. */
  138. @Override
  139. public HSSFBorderFormatting createBorderFormatting() {
  140. return getBorderFormatting(true);
  141. }
  142. private HSSFPatternFormatting getPatternFormatting(boolean create) {
  143. PatternFormatting patternFormatting = cfRuleRecord.getPatternFormatting();
  144. if (patternFormatting == null) {
  145. if (!create) return null;
  146. patternFormatting = new PatternFormatting();
  147. cfRuleRecord.setPatternFormatting(patternFormatting);
  148. }
  149. return new HSSFPatternFormatting(cfRuleRecord, workbook);
  150. }
  151. /**
  152. * @return - pattern formatting object if defined, {@code null} otherwise
  153. */
  154. @Override
  155. public HSSFPatternFormatting getPatternFormatting()
  156. {
  157. return getPatternFormatting(false);
  158. }
  159. /**
  160. * create a new pattern formatting structure if it does not exist,
  161. * otherwise just return existing object.
  162. * @return - pattern formatting object, never returns {@code null}.
  163. */
  164. @Override
  165. public HSSFPatternFormatting createPatternFormatting()
  166. {
  167. return getPatternFormatting(true);
  168. }
  169. private HSSFDataBarFormatting getDataBarFormatting(boolean create) {
  170. CFRule12Record cfRule12Record = getCFRule12Record(create);
  171. if (cfRule12Record == null) return null;
  172. DataBarFormatting databarFormatting = cfRule12Record.getDataBarFormatting();
  173. if (databarFormatting == null) {
  174. if (!create) return null;
  175. cfRule12Record.createDataBarFormatting();
  176. }
  177. return new HSSFDataBarFormatting(cfRule12Record, sheet);
  178. }
  179. /**
  180. * @return databar / data-bar formatting object if defined, {@code null} otherwise
  181. */
  182. @Override
  183. public HSSFDataBarFormatting getDataBarFormatting() {
  184. return getDataBarFormatting(false);
  185. }
  186. /**
  187. * create a new databar / data-bar formatting object if it does not exist,
  188. * otherwise just return the existing object.
  189. */
  190. public HSSFDataBarFormatting createDataBarFormatting() {
  191. return getDataBarFormatting(true);
  192. }
  193. private HSSFIconMultiStateFormatting getMultiStateFormatting(boolean create) {
  194. CFRule12Record cfRule12Record = getCFRule12Record(create);
  195. if (cfRule12Record == null) return null;
  196. IconMultiStateFormatting iconFormatting = cfRule12Record.getMultiStateFormatting();
  197. if (iconFormatting == null) {
  198. if (!create) return null;
  199. cfRule12Record.createMultiStateFormatting();
  200. }
  201. return new HSSFIconMultiStateFormatting(cfRule12Record, sheet);
  202. }
  203. /**
  204. * @return icon / multi-state formatting object if defined, {@code null} otherwise
  205. */
  206. @Override
  207. public HSSFIconMultiStateFormatting getMultiStateFormatting() {
  208. return getMultiStateFormatting(false);
  209. }
  210. /**
  211. * create a new icon / multi-state formatting object if it does not exist,
  212. * otherwise just return the existing object.
  213. */
  214. public HSSFIconMultiStateFormatting createMultiStateFormatting() {
  215. return getMultiStateFormatting(true);
  216. }
  217. private HSSFColorScaleFormatting getColorScaleFormatting(boolean create) {
  218. CFRule12Record cfRule12Record = getCFRule12Record(create);
  219. if (cfRule12Record == null) return null;
  220. ColorGradientFormatting colorFormatting = cfRule12Record.getColorGradientFormatting();
  221. if (colorFormatting == null) {
  222. if (!create) return null;
  223. cfRule12Record.createColorGradientFormatting();
  224. }
  225. return new HSSFColorScaleFormatting(cfRule12Record, sheet);
  226. }
  227. /**
  228. * @return color scale / gradient formatting object if defined, {@code null} otherwise
  229. */
  230. @Override
  231. public HSSFColorScaleFormatting getColorScaleFormatting() {
  232. return getColorScaleFormatting(false);
  233. }
  234. /**
  235. * create a new color scale / gradient formatting object if it does not exist,
  236. * otherwise just return the existing object.
  237. */
  238. public HSSFColorScaleFormatting createColorScaleFormatting() {
  239. return getColorScaleFormatting(true);
  240. }
  241. /**
  242. * @return - the conditiontype for the cfrule
  243. */
  244. @Override
  245. public ConditionType getConditionType() {
  246. byte code = cfRuleRecord.getConditionType();
  247. return ConditionType.forId(code);
  248. }
  249. /**
  250. * always null (not a filter condition) or {@link ConditionFilterType#FILTER} if it is.
  251. */
  252. @Override
  253. public ConditionFilterType getConditionFilterType() {
  254. return getConditionType() == ConditionType.FILTER ? ConditionFilterType.FILTER : null;
  255. }
  256. @Override
  257. public ConditionFilterData getFilterConfiguration() {
  258. return null;
  259. }
  260. /**
  261. * @return - the comparisionoperatation for the cfrule
  262. */
  263. @Override
  264. public byte getComparisonOperation() {
  265. return cfRuleRecord.getComparisonOperation();
  266. }
  267. @Override
  268. public String getFormula1()
  269. {
  270. return toFormulaString(cfRuleRecord.getParsedExpression1());
  271. }
  272. @Override
  273. public String getFormula2() {
  274. byte conditionType = cfRuleRecord.getConditionType();
  275. if (conditionType == CELL_COMPARISON) {
  276. byte comparisonOperation = cfRuleRecord.getComparisonOperation();
  277. switch(comparisonOperation) {
  278. case ComparisonOperator.BETWEEN:
  279. case ComparisonOperator.NOT_BETWEEN:
  280. return toFormulaString(cfRuleRecord.getParsedExpression2());
  281. }
  282. }
  283. return null;
  284. }
  285. @Override
  286. public String getText() {
  287. return null; // not available here, unless it exists and is unimplemented in cfRuleRecord
  288. }
  289. String toFormulaString(Ptg[] parsedExpression) {
  290. return toFormulaString(parsedExpression, workbook);
  291. }
  292. static String toFormulaString(Ptg[] parsedExpression, HSSFWorkbook workbook) {
  293. if(parsedExpression == null || parsedExpression.length == 0) {
  294. return null;
  295. }
  296. return HSSFFormulaParser.toFormulaString(workbook, parsedExpression);
  297. }
  298. /**
  299. * Conditional format rules don't define stripes, so always 0
  300. * @see org.apache.poi.ss.usermodel.DifferentialStyleProvider#getStripeSize()
  301. */
  302. @Override
  303. public int getStripeSize() {
  304. return 0;
  305. }
  306. }