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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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, _sh.getWorkbook().getStylesSource().getIndexedColors());
  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(), _sh.getWorkbook().getStylesSource().getIndexedColors());
  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, _sh.getWorkbook().getStylesSource().getIndexedColors());
  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(), _sh.getWorkbook().getStylesSource().getIndexedColors());
  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, _sh.getWorkbook().getStylesSource().getIndexedColors());
  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(), _sh.getWorkbook().getStylesSource().getIndexedColors());
  178. }
  179. /**
  180. *
  181. * @param color
  182. * @return data bar formatting
  183. */
  184. public XSSFDataBarFormatting createDataBarFormatting(XSSFColor color) {
  185. // Is it already there?
  186. if (_cfRule.isSetDataBar() && _cfRule.getType() == STCfType.DATA_BAR)
  187. return getDataBarFormatting();
  188. // Mark it as being a Data Bar
  189. _cfRule.setType(STCfType.DATA_BAR);
  190. // Ensure the right element
  191. CTDataBar bar = null;
  192. if (_cfRule.isSetDataBar()) {
  193. bar = _cfRule.getDataBar();
  194. } else {
  195. bar = _cfRule.addNewDataBar();
  196. }
  197. // Set the color
  198. bar.setColor(color.getCTColor());
  199. // Add the default thresholds
  200. CTCfvo min = bar.addNewCfvo();
  201. min.setType(STCfvoType.Enum.forString(RangeType.MIN.name));
  202. CTCfvo max = bar.addNewCfvo();
  203. max.setType(STCfvoType.Enum.forString(RangeType.MAX.name));
  204. // Wrap and return
  205. return new XSSFDataBarFormatting(bar, _sh.getWorkbook().getStylesSource().getIndexedColors());
  206. }
  207. public XSSFDataBarFormatting getDataBarFormatting() {
  208. if (_cfRule.isSetDataBar()) {
  209. CTDataBar bar = _cfRule.getDataBar();
  210. return new XSSFDataBarFormatting(bar, _sh.getWorkbook().getStylesSource().getIndexedColors());
  211. } else {
  212. return null;
  213. }
  214. }
  215. public XSSFIconMultiStateFormatting createMultiStateFormatting(IconSet iconSet) {
  216. // Is it already there?
  217. if (_cfRule.isSetIconSet() && _cfRule.getType() == STCfType.ICON_SET)
  218. return getMultiStateFormatting();
  219. // Mark it as being an Icon Set
  220. _cfRule.setType(STCfType.ICON_SET);
  221. // Ensure the right element
  222. CTIconSet icons = null;
  223. if (_cfRule.isSetIconSet()) {
  224. icons = _cfRule.getIconSet();
  225. } else {
  226. icons = _cfRule.addNewIconSet();
  227. }
  228. // Set the type of the icon set
  229. if (iconSet.name != null) {
  230. STIconSetType.Enum xIconSet = STIconSetType.Enum.forString(iconSet.name);
  231. icons.setIconSet(xIconSet);
  232. }
  233. // Add a default set of thresholds
  234. int jump = 100 / iconSet.num;
  235. STCfvoType.Enum type = STCfvoType.Enum.forString(RangeType.PERCENT.name);
  236. for (int i=0; i<iconSet.num; i++) {
  237. CTCfvo cfvo = icons.addNewCfvo();
  238. cfvo.setType(type);
  239. cfvo.setVal(Integer.toString(i*jump));
  240. }
  241. // Wrap and return
  242. return new XSSFIconMultiStateFormatting(icons);
  243. }
  244. public XSSFIconMultiStateFormatting getMultiStateFormatting() {
  245. if (_cfRule.isSetIconSet()) {
  246. CTIconSet icons = _cfRule.getIconSet();
  247. return new XSSFIconMultiStateFormatting(icons);
  248. } else {
  249. return null;
  250. }
  251. }
  252. public XSSFColorScaleFormatting createColorScaleFormatting() {
  253. // Is it already there?
  254. if (_cfRule.isSetColorScale() && _cfRule.getType() == STCfType.COLOR_SCALE)
  255. return getColorScaleFormatting();
  256. // Mark it as being a Color Scale
  257. _cfRule.setType(STCfType.COLOR_SCALE);
  258. // Ensure the right element
  259. CTColorScale scale = null;
  260. if (_cfRule.isSetColorScale()) {
  261. scale = _cfRule.getColorScale();
  262. } else {
  263. scale = _cfRule.addNewColorScale();
  264. }
  265. // Add a default set of thresholds and colors
  266. if (scale.sizeOfCfvoArray() == 0) {
  267. CTCfvo cfvo;
  268. cfvo = scale.addNewCfvo();
  269. cfvo.setType(STCfvoType.Enum.forString(RangeType.MIN.name));
  270. cfvo = scale.addNewCfvo();
  271. cfvo.setType(STCfvoType.Enum.forString(RangeType.PERCENTILE.name));
  272. cfvo.setVal("50");
  273. cfvo = scale.addNewCfvo();
  274. cfvo.setType(STCfvoType.Enum.forString(RangeType.MAX.name));
  275. for (int i=0; i<3; i++) {
  276. scale.addNewColor();
  277. }
  278. }
  279. // Wrap and return
  280. return new XSSFColorScaleFormatting(scale, _sh.getWorkbook().getStylesSource().getIndexedColors());
  281. }
  282. public XSSFColorScaleFormatting getColorScaleFormatting() {
  283. if (_cfRule.isSetColorScale()) {
  284. CTColorScale scale = _cfRule.getColorScale();
  285. return new XSSFColorScaleFormatting(scale, _sh.getWorkbook().getStylesSource().getIndexedColors());
  286. } else {
  287. return null;
  288. }
  289. }
  290. /**
  291. * Return the number format from the dxf style record if present, null if not
  292. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getNumberFormat()
  293. */
  294. public ExcelNumberFormat getNumberFormat() {
  295. CTDxf dxf = getDxf(false);
  296. if(dxf == null || !dxf.isSetNumFmt()) return null;
  297. CTNumFmt numFmt = dxf.getNumFmt();
  298. return new ExcelNumberFormat((int) numFmt.getNumFmtId(), numFmt.getFormatCode());
  299. }
  300. /**
  301. * Type of conditional formatting rule.
  302. */
  303. @Override
  304. public ConditionType getConditionType() {
  305. return typeLookup.get(_cfRule.getType());
  306. }
  307. /**
  308. * Will return null if {@link #getConditionType()} != {@link ConditionType#FILTER}
  309. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getConditionFilterType()
  310. */
  311. public ConditionFilterType getConditionFilterType() {
  312. return filterTypeLookup.get(_cfRule.getType());
  313. }
  314. public ConditionFilterData getFilterConfiguration() {
  315. return new XSSFConditionFilterData(_cfRule);
  316. }
  317. /**
  318. * The comparison function used when the type of conditional formatting is set to
  319. * {@link ConditionType#CELL_VALUE_IS}
  320. * <p>
  321. * MUST be a constant from {@link org.apache.poi.ss.usermodel.ComparisonOperator}
  322. * </p>
  323. *
  324. * @return the conditional format operator
  325. */
  326. @Override
  327. public byte getComparisonOperation(){
  328. STConditionalFormattingOperator.Enum op = _cfRule.getOperator();
  329. if(op == null) return ComparisonOperator.NO_COMPARISON;
  330. switch(op.intValue()){
  331. case STConditionalFormattingOperator.INT_LESS_THAN: return ComparisonOperator.LT;
  332. case STConditionalFormattingOperator.INT_LESS_THAN_OR_EQUAL: return ComparisonOperator.LE;
  333. case STConditionalFormattingOperator.INT_GREATER_THAN: return ComparisonOperator.GT;
  334. case STConditionalFormattingOperator.INT_GREATER_THAN_OR_EQUAL: return ComparisonOperator.GE;
  335. case STConditionalFormattingOperator.INT_EQUAL: return ComparisonOperator.EQUAL;
  336. case STConditionalFormattingOperator.INT_NOT_EQUAL: return ComparisonOperator.NOT_EQUAL;
  337. case STConditionalFormattingOperator.INT_BETWEEN: return ComparisonOperator.BETWEEN;
  338. case STConditionalFormattingOperator.INT_NOT_BETWEEN: return ComparisonOperator.NOT_BETWEEN;
  339. }
  340. return ComparisonOperator.NO_COMPARISON;
  341. }
  342. /**
  343. * The formula used to evaluate the first operand for the conditional formatting rule.
  344. * <p>
  345. * If the condition type is {@link ConditionType#CELL_VALUE_IS},
  346. * this field is the first operand of the comparison.
  347. * If type is {@link ConditionType#FORMULA}, this formula is used
  348. * to determine if the conditional formatting is applied.
  349. * </p>
  350. * <p>
  351. * If comparison type is {@link ConditionType#FORMULA} the formula MUST be a Boolean function
  352. * </p>
  353. *
  354. * @return the first formula
  355. */
  356. public String getFormula1(){
  357. return _cfRule.sizeOfFormulaArray() > 0 ? _cfRule.getFormulaArray(0) : null;
  358. }
  359. /**
  360. * The formula used to evaluate the second operand of the comparison when
  361. * comparison type is {@link ConditionType#CELL_VALUE_IS} and operator
  362. * is either {@link org.apache.poi.ss.usermodel.ComparisonOperator#BETWEEN} or {@link org.apache.poi.ss.usermodel.ComparisonOperator#NOT_BETWEEN}
  363. *
  364. * @return the second formula
  365. */
  366. public String getFormula2(){
  367. return _cfRule.sizeOfFormulaArray() == 2 ? _cfRule.getFormulaArray(1) : null;
  368. }
  369. /**
  370. * Conditional format rules don't define stripes, so always 0
  371. * @see org.apache.poi.ss.usermodel.DifferentialStyleProvider#getStripeSize()
  372. */
  373. public int getStripeSize() {
  374. return 0;
  375. }
  376. }