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

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