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.

XSSFConditionalFormatting.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 org.apache.poi.ss.usermodel.ConditionalFormatting;
  21. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. public class XSSFConditionalFormatting implements ConditionalFormatting {
  27. private final CTConditionalFormatting _cf;
  28. private final XSSFSheet _sh;
  29. /*package*/ XSSFConditionalFormatting(XSSFSheet sh) {
  30. _cf = CTConditionalFormatting.Factory.newInstance();
  31. _sh = sh;
  32. }
  33. /*package*/ XSSFConditionalFormatting(
  34. XSSFSheet sh, CTConditionalFormatting cf) {
  35. _cf = cf;
  36. _sh = sh;
  37. }
  38. /*package*/ CTConditionalFormatting getCTConditionalFormatting() {
  39. return _cf;
  40. }
  41. /**
  42. * @return array of {@code CellRangeAddress}s. Never {@code null}
  43. */
  44. @Override
  45. public CellRangeAddress[] getFormattingRanges() {
  46. ArrayList<CellRangeAddress> lst = new ArrayList<>();
  47. for (Object stRef : _cf.getSqref()) {
  48. String[] regions = stRef.toString().split(" ");
  49. for (final String region : regions) {
  50. lst.add(CellRangeAddress.valueOf(region));
  51. }
  52. }
  53. return lst.toArray(new CellRangeAddress[0]);
  54. }
  55. @Override
  56. public void setFormattingRanges(CellRangeAddress[] ranges) {
  57. if (ranges == null) {
  58. throw new IllegalArgumentException("cellRanges must not be null");
  59. }
  60. final StringBuilder sb = new StringBuilder();
  61. boolean first = true;
  62. for (CellRangeAddress range : ranges) {
  63. if (!first) {
  64. sb.append(' ');
  65. } else {
  66. first = false;
  67. }
  68. sb.append(range.formatAsString());
  69. }
  70. _cf.setSqref(Collections.singletonList(sb.toString()));
  71. }
  72. /**
  73. * Replaces an existing Conditional Formatting rule at position idx.
  74. * Excel allows to create up to 3 Conditional Formatting rules.
  75. * This method can be useful to modify existing Conditional Formatting rules.
  76. *
  77. * @param idx position of the rule. Should be between 0 and 2.
  78. * @param cfRule - Conditional Formatting rule
  79. */
  80. @Override
  81. public void setRule(int idx, ConditionalFormattingRule cfRule) {
  82. XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
  83. _cf.getCfRuleArray(idx).set(xRule.getCTCfRule());
  84. }
  85. /**
  86. * Add a Conditional Formatting rule.
  87. * Excel allows to create up to 3 Conditional Formatting rules.
  88. *
  89. * @param cfRule - Conditional Formatting rule
  90. */
  91. @Override
  92. public void addRule(ConditionalFormattingRule cfRule) {
  93. XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule) cfRule;
  94. _cf.addNewCfRule().set(xRule.getCTCfRule());
  95. }
  96. /**
  97. * @return the Conditional Formatting rule at position idx.
  98. */
  99. @Override
  100. public XSSFConditionalFormattingRule getRule(int idx) {
  101. return new XSSFConditionalFormattingRule(_sh, _cf.getCfRuleArray(idx));
  102. }
  103. /**
  104. * @return number of Conditional Formatting rules.
  105. */
  106. @Override
  107. public int getNumberOfRules() {
  108. return _cf.sizeOfCfRuleArray();
  109. }
  110. @Override
  111. public String toString() {
  112. return _cf.toString();
  113. }
  114. }