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

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