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.

XSSFConditionalFormattingRule.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xssf.usermodel;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import org.apache.poi.ss.usermodel.*;
  23. import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
  24. import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
  25. import org.apache.poi.xssf.usermodel.XSSFFontFormatting;
  26. import org.apache.poi.xssf.model.StylesTable;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
  28. /**
  29. * XSSF support for Conditional Formatting rules
  30. */
  31. public class XSSFConditionalFormattingRule implements ConditionalFormattingRule {
  32. private final CTCfRule _cfRule;
  33. private XSSFSheet _sh;
  34. private static Map<STCfType.Enum, ConditionType> typeLookup = new HashMap<STCfType.Enum, ConditionType>();
  35. private static Map<STCfType.Enum, ConditionFilterType> filterTypeLookup = new HashMap<STCfType.Enum, ConditionFilterType>();
  36. static {
  37. typeLookup.put(STCfType.CELL_IS, ConditionType.CELL_VALUE_IS);
  38. typeLookup.put(STCfType.EXPRESSION, ConditionType.FORMULA);
  39. typeLookup.put(STCfType.COLOR_SCALE, ConditionType.COLOR_SCALE);
  40. typeLookup.put(STCfType.DATA_BAR, ConditionType.DATA_BAR);
  41. typeLookup.put(STCfType.ICON_SET, ConditionType.ICON_SET);
  42. // These are all subtypes of Filter, we think...
  43. typeLookup.put(STCfType.TOP_10, ConditionType.FILTER);
  44. typeLookup.put(STCfType.UNIQUE_VALUES, ConditionType.FILTER);
  45. typeLookup.put(STCfType.DUPLICATE_VALUES, ConditionType.FILTER);
  46. typeLookup.put(STCfType.CONTAINS_TEXT, ConditionType.FILTER);
  47. typeLookup.put(STCfType.NOT_CONTAINS_TEXT, ConditionType.FILTER);
  48. typeLookup.put(STCfType.BEGINS_WITH, ConditionType.FILTER);
  49. typeLookup.put(STCfType.ENDS_WITH, ConditionType.FILTER);
  50. typeLookup.put(STCfType.CONTAINS_BLANKS, ConditionType.FILTER);
  51. typeLookup.put(STCfType.NOT_CONTAINS_BLANKS, ConditionType.FILTER);
  52. typeLookup.put(STCfType.CONTAINS_ERRORS, ConditionType.FILTER);
  53. typeLookup.put(STCfType.NOT_CONTAINS_ERRORS, ConditionType.FILTER);
  54. typeLookup.put(STCfType.TIME_PERIOD, ConditionType.FILTER);
  55. typeLookup.put(STCfType.ABOVE_AVERAGE, ConditionType.FILTER);
  56. filterTypeLookup.put(STCfType.TOP_10, ConditionFilterType.TOP_10);
  57. filterTypeLookup.put(STCfType.UNIQUE_VALUES, ConditionFilterType.UNIQUE_VALUES);
  58. filterTypeLookup.put(STCfType.DUPLICATE_VALUES, ConditionFilterType.DUPLICATE_VALUES);
  59. filterTypeLookup.put(STCfType.CONTAINS_TEXT, ConditionFilterType.CONTAINS_TEXT);
  60. filterTypeLookup.put(STCfType.NOT_CONTAINS_TEXT, ConditionFilterType.NOT_CONTAINS_TEXT);
  61. filterTypeLookup.put(STCfType.BEGINS_WITH, ConditionFilterType.BEGINS_WITH);
  62. filterTypeLookup.put(STCfType.ENDS_WITH, ConditionFilterType.ENDS_WITH);
  63. filterTypeLookup.put(STCfType.CONTAINS_BLANKS, ConditionFilterType.CONTAINS_BLANKS);
  64. filterTypeLookup.put(STCfType.NOT_CONTAINS_BLANKS, ConditionFilterType.NOT_CONTAINS_BLANKS);
  65. filterTypeLookup.put(STCfType.CONTAINS_ERRORS, ConditionFilterType.CONTAINS_ERRORS);
  66. filterTypeLookup.put(STCfType.NOT_CONTAINS_ERRORS, ConditionFilterType.NOT_CONTAINS_ERRORS);
  67. filterTypeLookup.put(STCfType.TIME_PERIOD, ConditionFilterType.TIME_PERIOD);
  68. filterTypeLookup.put(STCfType.ABOVE_AVERAGE, ConditionFilterType.ABOVE_AVERAGE);
  69. }
  70. /**
  71. * NOTE: does not set priority, so this assumes the rule will not be added to the sheet yet
  72. * @param sh
  73. */
  74. /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh){
  75. _cfRule = CTCfRule.Factory.newInstance();
  76. _sh = sh;
  77. }
  78. /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh, CTCfRule cfRule){
  79. _cfRule = cfRule;
  80. _sh = sh;
  81. }
  82. /*package*/ CTCfRule getCTCfRule(){
  83. return _cfRule;
  84. }
  85. /*package*/ CTDxf getDxf(boolean create){
  86. StylesTable styles = _sh.getWorkbook().getStylesSource();
  87. CTDxf dxf = null;
  88. if(styles._getDXfsSize() > 0 && _cfRule.isSetDxfId()){
  89. int dxfId = (int)_cfRule.getDxfId();
  90. dxf = styles.getDxfAt(dxfId);
  91. }
  92. if(create && dxf == null) {
  93. dxf = CTDxf.Factory.newInstance();
  94. int dxfId = styles.putDxf(dxf);
  95. _cfRule.setDxfId(dxfId - 1);
  96. }
  97. return dxf;
  98. }
  99. public int getPriority() {
  100. final int priority = _cfRule.getPriority();
  101. // priorities start at 1, if it is less, it is undefined, use definition order in caller
  102. return priority >=1 ? priority : 0;
  103. }
  104. public boolean getStopIfTrue() {
  105. return _cfRule.getStopIfTrue();
  106. }
  107. /**
  108. * Create a new border formatting structure if it does not exist,
  109. * otherwise just return existing object.
  110. *
  111. * @return - border formatting object, never returns <code>null</code>.
  112. */
  113. public XSSFBorderFormatting createBorderFormatting(){
  114. CTDxf dxf = getDxf(true);
  115. CTBorder border;
  116. if(!dxf.isSetBorder()) {
  117. border = dxf.addNewBorder();
  118. } else {
  119. border = dxf.getBorder();
  120. }
  121. return new XSSFBorderFormatting(border);
  122. }
  123. /**
  124. * @return - border formatting object if defined, <code>null</code> otherwise
  125. */
  126. public XSSFBorderFormatting getBorderFormatting(){
  127. CTDxf dxf = getDxf(false);
  128. if(dxf == null || !dxf.isSetBorder()) return null;
  129. return new XSSFBorderFormatting(dxf.getBorder());
  130. }
  131. /**
  132. * Create a new font formatting structure if it does not exist,
  133. * otherwise just return existing object.
  134. *
  135. * @return - font formatting object, never returns <code>null</code>.
  136. */
  137. public XSSFFontFormatting createFontFormatting(){
  138. CTDxf dxf = getDxf(true);
  139. CTFont font;
  140. if(!dxf.isSetFont()) {
  141. font = dxf.addNewFont();
  142. } else {
  143. font = dxf.getFont();
  144. }
  145. return new XSSFFontFormatting(font);
  146. }
  147. /**
  148. * @return - font formatting object if defined, <code>null</code> otherwise
  149. */
  150. public XSSFFontFormatting getFontFormatting(){
  151. CTDxf dxf = getDxf(false);
  152. if(dxf == null || !dxf.isSetFont()) return null;
  153. return new XSSFFontFormatting(dxf.getFont());
  154. }
  155. /**
  156. * Create a new pattern formatting structure if it does not exist,
  157. * otherwise just return existing object.
  158. *
  159. * @return - pattern formatting object, never returns <code>null</code>.
  160. */
  161. public XSSFPatternFormatting createPatternFormatting(){
  162. CTDxf dxf = getDxf(true);
  163. CTFill fill;
  164. if(!dxf.isSetFill()) {
  165. fill = dxf.addNewFill();
  166. } else {
  167. fill = dxf.getFill();
  168. }
  169. return new XSSFPatternFormatting(fill);
  170. }
  171. /**
  172. * @return - pattern formatting object if defined, <code>null</code> otherwise
  173. */
  174. public XSSFPatternFormatting getPatternFormatting(){
  175. CTDxf dxf = getDxf(false);
  176. if(dxf == null || !dxf.isSetFill()) return null;
  177. return new XSSFPatternFormatting(dxf.getFill());
  178. }
  179. public XSSFDataBarFormatting createDataBarFormatting(XSSFColor color) {
  180. // Is it already there?
  181. if (_cfRule.isSetDataBar() && _cfRule.getType() == STCfType.DATA_BAR)
  182. return getDataBarFormatting();
  183. // Mark it as being a Data Bar
  184. _cfRule.setType(STCfType.DATA_BAR);
  185. // Ensure the right element
  186. CTDataBar bar = null;
  187. if (_cfRule.isSetDataBar()) {
  188. bar = _cfRule.getDataBar();
  189. } else {
  190. bar = _cfRule.addNewDataBar();
  191. }
  192. // Set the color
  193. bar.setColor(color.getCTColor());
  194. // Add the default thresholds
  195. CTCfvo min = bar.addNewCfvo();
  196. min.setType(STCfvoType.Enum.forString(RangeType.MIN.name));
  197. CTCfvo max = bar.addNewCfvo();
  198. max.setType(STCfvoType.Enum.forString(RangeType.MAX.name));
  199. // Wrap and return
  200. return new XSSFDataBarFormatting(bar);
  201. }
  202. public XSSFDataBarFormatting getDataBarFormatting() {
  203. if (_cfRule.isSetDataBar()) {
  204. CTDataBar bar = _cfRule.getDataBar();
  205. return new XSSFDataBarFormatting(bar);
  206. } else {
  207. return null;
  208. }
  209. }
  210. public XSSFIconMultiStateFormatting createMultiStateFormatting(IconSet iconSet) {
  211. // Is it already there?
  212. if (_cfRule.isSetIconSet() && _cfRule.getType() == STCfType.ICON_SET)
  213. return getMultiStateFormatting();
  214. // Mark it as being an Icon Set
  215. _cfRule.setType(STCfType.ICON_SET);
  216. // Ensure the right element
  217. CTIconSet icons = null;
  218. if (_cfRule.isSetIconSet()) {
  219. icons = _cfRule.getIconSet();
  220. } else {
  221. icons = _cfRule.addNewIconSet();
  222. }
  223. // Set the type of the icon set
  224. if (iconSet.name != null) {
  225. STIconSetType.Enum xIconSet = STIconSetType.Enum.forString(iconSet.name);
  226. icons.setIconSet(xIconSet);
  227. }
  228. // Add a default set of thresholds
  229. int jump = 100 / iconSet.num;
  230. STCfvoType.Enum type = STCfvoType.Enum.forString(RangeType.PERCENT.name);
  231. for (int i=0; i<iconSet.num; i++) {
  232. CTCfvo cfvo = icons.addNewCfvo();
  233. cfvo.setType(type);
  234. cfvo.setVal(Integer.toString(i*jump));
  235. }
  236. // Wrap and return
  237. return new XSSFIconMultiStateFormatting(icons);
  238. }
  239. public XSSFIconMultiStateFormatting getMultiStateFormatting() {
  240. if (_cfRule.isSetIconSet()) {
  241. CTIconSet icons = _cfRule.getIconSet();
  242. return new XSSFIconMultiStateFormatting(icons);
  243. } else {
  244. return null;
  245. }
  246. }
  247. public XSSFColorScaleFormatting createColorScaleFormatting() {
  248. // Is it already there?
  249. if (_cfRule.isSetColorScale() && _cfRule.getType() == STCfType.COLOR_SCALE)
  250. return getColorScaleFormatting();
  251. // Mark it as being a Color Scale
  252. _cfRule.setType(STCfType.COLOR_SCALE);
  253. // Ensure the right element
  254. CTColorScale scale = null;
  255. if (_cfRule.isSetColorScale()) {
  256. scale = _cfRule.getColorScale();
  257. } else {
  258. scale = _cfRule.addNewColorScale();
  259. }
  260. // Add a default set of thresholds and colors
  261. if (scale.sizeOfCfvoArray() == 0) {
  262. CTCfvo cfvo;
  263. cfvo = scale.addNewCfvo();
  264. cfvo.setType(STCfvoType.Enum.forString(RangeType.MIN.name));
  265. cfvo = scale.addNewCfvo();
  266. cfvo.setType(STCfvoType.Enum.forString(RangeType.PERCENTILE.name));
  267. cfvo.setVal("50");
  268. cfvo = scale.addNewCfvo();
  269. cfvo.setType(STCfvoType.Enum.forString(RangeType.MAX.name));
  270. for (int i=0; i<3; i++) {
  271. scale.addNewColor();
  272. }
  273. }
  274. // Wrap and return
  275. return new XSSFColorScaleFormatting(scale);
  276. }
  277. public XSSFColorScaleFormatting getColorScaleFormatting() {
  278. if (_cfRule.isSetColorScale()) {
  279. CTColorScale scale = _cfRule.getColorScale();
  280. return new XSSFColorScaleFormatting(scale);
  281. } else {
  282. return null;
  283. }
  284. }
  285. /**
  286. * Type of conditional formatting rule.
  287. */
  288. @Override
  289. public ConditionType getConditionType() {
  290. return typeLookup.get(_cfRule.getType());
  291. }
  292. /**
  293. * Will return null if {@link #getConditionType()} != {@link ConditionType#FILTER}
  294. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getConditionFilterType()
  295. */
  296. public ConditionFilterType getConditionFilterType() {
  297. return filterTypeLookup.get(_cfRule.getType());
  298. }
  299. public ConditionFilterData getFilterConfiguration() {
  300. return new XSSFConditionFilterData(_cfRule);
  301. }
  302. /**
  303. * The comparison function used when the type of conditional formatting is set to
  304. * {@link ConditionType#CELL_VALUE_IS}
  305. * <p>
  306. * MUST be a constant from {@link org.apache.poi.ss.usermodel.ComparisonOperator}
  307. * </p>
  308. *
  309. * @return the conditional format operator
  310. */
  311. @Override
  312. public byte getComparisonOperation(){
  313. STConditionalFormattingOperator.Enum op = _cfRule.getOperator();
  314. if(op == null) return ComparisonOperator.NO_COMPARISON;
  315. switch(op.intValue()){
  316. case STConditionalFormattingOperator.INT_LESS_THAN: return ComparisonOperator.LT;
  317. case STConditionalFormattingOperator.INT_LESS_THAN_OR_EQUAL: return ComparisonOperator.LE;
  318. case STConditionalFormattingOperator.INT_GREATER_THAN: return ComparisonOperator.GT;
  319. case STConditionalFormattingOperator.INT_GREATER_THAN_OR_EQUAL: return ComparisonOperator.GE;
  320. case STConditionalFormattingOperator.INT_EQUAL: return ComparisonOperator.EQUAL;
  321. case STConditionalFormattingOperator.INT_NOT_EQUAL: return ComparisonOperator.NOT_EQUAL;
  322. case STConditionalFormattingOperator.INT_BETWEEN: return ComparisonOperator.BETWEEN;
  323. case STConditionalFormattingOperator.INT_NOT_BETWEEN: return ComparisonOperator.NOT_BETWEEN;
  324. }
  325. return ComparisonOperator.NO_COMPARISON;
  326. }
  327. /**
  328. * The formula used to evaluate the first operand for the conditional formatting rule.
  329. * <p>
  330. * If the condition type is {@link ConditionType#CELL_VALUE_IS},
  331. * this field is the first operand of the comparison.
  332. * If type is {@link ConditionType#FORMULA}, this formula is used
  333. * to determine if the conditional formatting is applied.
  334. * </p>
  335. * <p>
  336. * If comparison type is {@link ConditionType#FORMULA} the formula MUST be a Boolean function
  337. * </p>
  338. *
  339. * @return the first formula
  340. */
  341. public String getFormula1(){
  342. return _cfRule.sizeOfFormulaArray() > 0 ? _cfRule.getFormulaArray(0) : null;
  343. }
  344. /**
  345. * The formula used to evaluate the second operand of the comparison when
  346. * comparison type is {@link ConditionType#CELL_VALUE_IS} and operator
  347. * is either {@link org.apache.poi.ss.usermodel.ComparisonOperator#BETWEEN} or {@link org.apache.poi.ss.usermodel.ComparisonOperator#NOT_BETWEEN}
  348. *
  349. * @return the second formula
  350. */
  351. public String getFormula2(){
  352. return _cfRule.sizeOfFormulaArray() == 2 ? _cfRule.getFormulaArray(1) : null;
  353. }
  354. }