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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 XSSFSheet _sh;
  51. private static Map<STCfType.Enum, ConditionType> typeLookup = new HashMap<>();
  52. private static 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. * @param sh
  90. */
  91. /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh){
  92. _cfRule = CTCfRule.Factory.newInstance();
  93. _sh = sh;
  94. }
  95. /*package*/ XSSFConditionalFormattingRule(XSSFSheet sh, CTCfRule cfRule){
  96. _cfRule = cfRule;
  97. _sh = sh;
  98. }
  99. /*package*/ CTCfRule getCTCfRule(){
  100. return _cfRule;
  101. }
  102. /*package*/ CTDxf getDxf(boolean create){
  103. StylesTable styles = _sh.getWorkbook().getStylesSource();
  104. CTDxf dxf = null;
  105. if(styles._getDXfsSize() > 0 && _cfRule.isSetDxfId()){
  106. int dxfId = (int)_cfRule.getDxfId();
  107. dxf = styles.getDxfAt(dxfId);
  108. }
  109. if(create && dxf == null) {
  110. dxf = CTDxf.Factory.newInstance();
  111. int dxfId = styles.putDxf(dxf);
  112. _cfRule.setDxfId(dxfId - 1L);
  113. }
  114. return dxf;
  115. }
  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. public boolean getStopIfTrue() {
  122. return _cfRule.getStopIfTrue();
  123. }
  124. /**
  125. * Create a new border formatting structure if it does not exist,
  126. * otherwise just return existing object.
  127. *
  128. * @return - border formatting object, never returns <code>null</code>.
  129. */
  130. public XSSFBorderFormatting createBorderFormatting(){
  131. CTDxf dxf = getDxf(true);
  132. CTBorder border;
  133. if(!dxf.isSetBorder()) {
  134. border = dxf.addNewBorder();
  135. } else {
  136. border = dxf.getBorder();
  137. }
  138. return new XSSFBorderFormatting(border, _sh.getWorkbook().getStylesSource().getIndexedColors());
  139. }
  140. /**
  141. * @return - border formatting object if defined, <code>null</code> otherwise
  142. */
  143. public XSSFBorderFormatting getBorderFormatting(){
  144. CTDxf dxf = getDxf(false);
  145. if(dxf == null || !dxf.isSetBorder()) return null;
  146. return new XSSFBorderFormatting(dxf.getBorder(), _sh.getWorkbook().getStylesSource().getIndexedColors());
  147. }
  148. /**
  149. * Create a new font formatting structure if it does not exist,
  150. * otherwise just return existing object.
  151. *
  152. * @return - font formatting object, never returns <code>null</code>.
  153. */
  154. public XSSFFontFormatting createFontFormatting(){
  155. CTDxf dxf = getDxf(true);
  156. CTFont font;
  157. if(!dxf.isSetFont()) {
  158. font = dxf.addNewFont();
  159. } else {
  160. font = dxf.getFont();
  161. }
  162. return new XSSFFontFormatting(font, _sh.getWorkbook().getStylesSource().getIndexedColors());
  163. }
  164. /**
  165. * @return - font formatting object if defined, <code>null</code> otherwise
  166. */
  167. public XSSFFontFormatting getFontFormatting(){
  168. CTDxf dxf = getDxf(false);
  169. if(dxf == null || !dxf.isSetFont()) return null;
  170. return new XSSFFontFormatting(dxf.getFont(), _sh.getWorkbook().getStylesSource().getIndexedColors());
  171. }
  172. /**
  173. * Create a new pattern formatting structure if it does not exist,
  174. * otherwise just return existing object.
  175. *
  176. * @return - pattern formatting object, never returns <code>null</code>.
  177. */
  178. public XSSFPatternFormatting createPatternFormatting(){
  179. CTDxf dxf = getDxf(true);
  180. CTFill fill;
  181. if(!dxf.isSetFill()) {
  182. fill = dxf.addNewFill();
  183. } else {
  184. fill = dxf.getFill();
  185. }
  186. return new XSSFPatternFormatting(fill, _sh.getWorkbook().getStylesSource().getIndexedColors());
  187. }
  188. /**
  189. * @return - pattern formatting object if defined, <code>null</code> otherwise
  190. */
  191. public XSSFPatternFormatting getPatternFormatting(){
  192. CTDxf dxf = getDxf(false);
  193. if(dxf == null || !dxf.isSetFill()) return null;
  194. return new XSSFPatternFormatting(dxf.getFill(), _sh.getWorkbook().getStylesSource().getIndexedColors());
  195. }
  196. /**
  197. *
  198. * @param color
  199. * @return data bar formatting
  200. */
  201. public XSSFDataBarFormatting createDataBarFormatting(XSSFColor color) {
  202. // Is it already there?
  203. if (_cfRule.isSetDataBar() && _cfRule.getType() == STCfType.DATA_BAR)
  204. return getDataBarFormatting();
  205. // Mark it as being a Data Bar
  206. _cfRule.setType(STCfType.DATA_BAR);
  207. // Ensure the right element
  208. CTDataBar bar = null;
  209. if (_cfRule.isSetDataBar()) {
  210. bar = _cfRule.getDataBar();
  211. } else {
  212. bar = _cfRule.addNewDataBar();
  213. }
  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. public XSSFDataBarFormatting getDataBarFormatting() {
  225. if (_cfRule.isSetDataBar()) {
  226. CTDataBar bar = _cfRule.getDataBar();
  227. return new XSSFDataBarFormatting(bar, _sh.getWorkbook().getStylesSource().getIndexedColors());
  228. } else {
  229. return null;
  230. }
  231. }
  232. public XSSFIconMultiStateFormatting createMultiStateFormatting(IconSet iconSet) {
  233. // Is it already there?
  234. if (_cfRule.isSetIconSet() && _cfRule.getType() == STCfType.ICON_SET)
  235. return getMultiStateFormatting();
  236. // Mark it as being an Icon Set
  237. _cfRule.setType(STCfType.ICON_SET);
  238. // Ensure the right element
  239. CTIconSet icons = null;
  240. if (_cfRule.isSetIconSet()) {
  241. icons = _cfRule.getIconSet();
  242. } else {
  243. icons = _cfRule.addNewIconSet();
  244. }
  245. // Set the type of the icon set
  246. if (iconSet.name != null) {
  247. STIconSetType.Enum xIconSet = STIconSetType.Enum.forString(iconSet.name);
  248. icons.setIconSet(xIconSet);
  249. }
  250. // Add a default set of thresholds
  251. int jump = 100 / iconSet.num;
  252. STCfvoType.Enum type = STCfvoType.Enum.forString(RangeType.PERCENT.name);
  253. for (int i=0; i<iconSet.num; i++) {
  254. CTCfvo cfvo = icons.addNewCfvo();
  255. cfvo.setType(type);
  256. cfvo.setVal(Integer.toString(i*jump));
  257. }
  258. // Wrap and return
  259. return new XSSFIconMultiStateFormatting(icons);
  260. }
  261. public XSSFIconMultiStateFormatting getMultiStateFormatting() {
  262. if (_cfRule.isSetIconSet()) {
  263. CTIconSet icons = _cfRule.getIconSet();
  264. return new XSSFIconMultiStateFormatting(icons);
  265. } else {
  266. return null;
  267. }
  268. }
  269. public XSSFColorScaleFormatting createColorScaleFormatting() {
  270. // Is it already there?
  271. if (_cfRule.isSetColorScale() && _cfRule.getType() == STCfType.COLOR_SCALE)
  272. return getColorScaleFormatting();
  273. // Mark it as being a Color Scale
  274. _cfRule.setType(STCfType.COLOR_SCALE);
  275. // Ensure the right element
  276. CTColorScale scale = null;
  277. if (_cfRule.isSetColorScale()) {
  278. scale = _cfRule.getColorScale();
  279. } else {
  280. scale = _cfRule.addNewColorScale();
  281. }
  282. // Add a default set of thresholds and colors
  283. if (scale.sizeOfCfvoArray() == 0) {
  284. CTCfvo cfvo;
  285. cfvo = scale.addNewCfvo();
  286. cfvo.setType(STCfvoType.Enum.forString(RangeType.MIN.name));
  287. cfvo = scale.addNewCfvo();
  288. cfvo.setType(STCfvoType.Enum.forString(RangeType.PERCENTILE.name));
  289. cfvo.setVal("50");
  290. cfvo = scale.addNewCfvo();
  291. cfvo.setType(STCfvoType.Enum.forString(RangeType.MAX.name));
  292. for (int i=0; i<3; i++) {
  293. scale.addNewColor();
  294. }
  295. }
  296. // Wrap and return
  297. return new XSSFColorScaleFormatting(scale, _sh.getWorkbook().getStylesSource().getIndexedColors());
  298. }
  299. public XSSFColorScaleFormatting getColorScaleFormatting() {
  300. if (_cfRule.isSetColorScale()) {
  301. CTColorScale scale = _cfRule.getColorScale();
  302. return new XSSFColorScaleFormatting(scale, _sh.getWorkbook().getStylesSource().getIndexedColors());
  303. } else {
  304. return null;
  305. }
  306. }
  307. /**
  308. * Return the number format from the dxf style record if present, null if not
  309. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getNumberFormat()
  310. */
  311. public ExcelNumberFormat getNumberFormat() {
  312. CTDxf dxf = getDxf(false);
  313. if(dxf == null || !dxf.isSetNumFmt()) return null;
  314. CTNumFmt numFmt = dxf.getNumFmt();
  315. return new ExcelNumberFormat((int) numFmt.getNumFmtId(), numFmt.getFormatCode());
  316. }
  317. /**
  318. * Type of conditional formatting rule.
  319. */
  320. @Override
  321. public ConditionType getConditionType() {
  322. return typeLookup.get(_cfRule.getType());
  323. }
  324. /**
  325. * Will return null if {@link #getConditionType()} != {@link ConditionType#FILTER}
  326. * @see org.apache.poi.ss.usermodel.ConditionalFormattingRule#getConditionFilterType()
  327. */
  328. public ConditionFilterType getConditionFilterType() {
  329. return filterTypeLookup.get(_cfRule.getType());
  330. }
  331. public ConditionFilterData getFilterConfiguration() {
  332. return new XSSFConditionFilterData(_cfRule);
  333. }
  334. /**
  335. * The comparison function used when the type of conditional formatting is set to
  336. * {@link ConditionType#CELL_VALUE_IS}
  337. * <p>
  338. * MUST be a constant from {@link org.apache.poi.ss.usermodel.ComparisonOperator}
  339. * </p>
  340. *
  341. * @return the conditional format operator
  342. */
  343. @Override
  344. public byte getComparisonOperation(){
  345. STConditionalFormattingOperator.Enum op = _cfRule.getOperator();
  346. if(op == null) return ComparisonOperator.NO_COMPARISON;
  347. switch(op.intValue()){
  348. case STConditionalFormattingOperator.INT_LESS_THAN: return ComparisonOperator.LT;
  349. case STConditionalFormattingOperator.INT_LESS_THAN_OR_EQUAL: return ComparisonOperator.LE;
  350. case STConditionalFormattingOperator.INT_GREATER_THAN: return ComparisonOperator.GT;
  351. case STConditionalFormattingOperator.INT_GREATER_THAN_OR_EQUAL: return ComparisonOperator.GE;
  352. case STConditionalFormattingOperator.INT_EQUAL: return ComparisonOperator.EQUAL;
  353. case STConditionalFormattingOperator.INT_NOT_EQUAL: return ComparisonOperator.NOT_EQUAL;
  354. case STConditionalFormattingOperator.INT_BETWEEN: return ComparisonOperator.BETWEEN;
  355. case STConditionalFormattingOperator.INT_NOT_BETWEEN: return ComparisonOperator.NOT_BETWEEN;
  356. }
  357. return ComparisonOperator.NO_COMPARISON;
  358. }
  359. /**
  360. * The formula used to evaluate the first operand for the conditional formatting rule.
  361. * <p>
  362. * If the condition type is {@link ConditionType#CELL_VALUE_IS},
  363. * this field is the first operand of the comparison.
  364. * If type is {@link ConditionType#FORMULA}, this formula is used
  365. * to determine if the conditional formatting is applied.
  366. * </p>
  367. * <p>
  368. * If comparison type is {@link ConditionType#FORMULA} the formula MUST be a Boolean function
  369. * </p>
  370. *
  371. * @return the first formula
  372. */
  373. public String getFormula1(){
  374. return _cfRule.sizeOfFormulaArray() > 0 ? _cfRule.getFormulaArray(0) : null;
  375. }
  376. /**
  377. * The formula used to evaluate the second operand of the comparison when
  378. * comparison type is {@link ConditionType#CELL_VALUE_IS} and operator
  379. * is either {@link org.apache.poi.ss.usermodel.ComparisonOperator#BETWEEN} or {@link org.apache.poi.ss.usermodel.ComparisonOperator#NOT_BETWEEN}
  380. *
  381. * @return the second formula
  382. */
  383. public String getFormula2(){
  384. return _cfRule.sizeOfFormulaArray() == 2 ? _cfRule.getFormulaArray(1) : null;
  385. }
  386. public String getText() {
  387. return _cfRule.getText();
  388. }
  389. /**
  390. * Conditional format rules don't define stripes, so always 0
  391. * @see org.apache.poi.ss.usermodel.DifferentialStyleProvider#getStripeSize()
  392. */
  393. public int getStripeSize() {
  394. return 0;
  395. }
  396. }