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.

EvaluationConditionalFormatRule.java 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import java.text.CollationKey;
  17. import java.text.Collator;
  18. import java.text.DecimalFormat;
  19. import java.text.DecimalFormatSymbols;
  20. import java.util.*;
  21. import org.apache.poi.ss.formula.eval.BlankEval;
  22. import org.apache.poi.ss.formula.eval.BoolEval;
  23. import org.apache.poi.ss.formula.eval.ErrorEval;
  24. import org.apache.poi.ss.formula.eval.NumberEval;
  25. import org.apache.poi.ss.formula.eval.RefEval;
  26. import org.apache.poi.ss.formula.eval.StringEval;
  27. import org.apache.poi.ss.formula.eval.ValueEval;
  28. import org.apache.poi.ss.formula.functions.AggregateFunction;
  29. import org.apache.poi.ss.usermodel.Cell;
  30. import org.apache.poi.ss.usermodel.CellType;
  31. import org.apache.poi.ss.usermodel.ConditionFilterData;
  32. import org.apache.poi.ss.usermodel.ConditionFilterType;
  33. import org.apache.poi.ss.usermodel.ConditionType;
  34. import org.apache.poi.ss.usermodel.ConditionalFormatting;
  35. import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
  36. import org.apache.poi.ss.usermodel.ExcelNumberFormat;
  37. import org.apache.poi.ss.usermodel.Row;
  38. import org.apache.poi.ss.usermodel.Sheet;
  39. import org.apache.poi.ss.util.CellRangeAddress;
  40. import org.apache.poi.ss.util.CellReference;
  41. import org.apache.poi.util.LocaleUtil;
  42. /**
  43. * Abstracted and cached version of a Conditional Format rule for use with a
  44. * {@link ConditionalFormattingEvaluator}. This references a rule, its owning
  45. * {@link ConditionalFormatting}, its priority order (lower index = higher priority in Excel),
  46. * and the information needed to evaluate the rule for a given cell.
  47. * <p>
  48. * Having this all combined and cached avoids repeated access calls to the
  49. * underlying structural objects, XSSF CT* objects and HSSF raw byte structures.
  50. * Those objects can be referenced from here. This object will be out of sync if
  51. * anything modifies the referenced structures' evaluation properties.
  52. * <p>
  53. * The assumption is that consuming applications will read the display properties once and
  54. * create whatever style objects they need, caching those at the application level.
  55. * Thus this class only caches values needed for evaluation, not display.
  56. */
  57. public class EvaluationConditionalFormatRule implements Comparable<EvaluationConditionalFormatRule> {
  58. private final WorkbookEvaluator workbookEvaluator;
  59. private final Sheet sheet;
  60. private final ConditionalFormatting formatting;
  61. private final ConditionalFormattingRule rule;
  62. /* cached values */
  63. private final CellRangeAddress[] regions;
  64. /**
  65. * Depending on the rule type, it may want to know about certain values in the region when evaluating {@link #matches(CellReference)},
  66. * such as top 10, unique, duplicate, average, etc. This collection stores those if needed so they are not repeatedly calculated
  67. */
  68. private final Map<CellRangeAddress, Set<ValueAndFormat>> meaningfulRegionValues = new HashMap<>();
  69. private final int priority;
  70. private final int formattingIndex;
  71. private final int ruleIndex;
  72. private final String formula1;
  73. private final String formula2;
  74. private final String text;
  75. // cached for performance, used with cell text comparisons, which are case insensitive and need to be Locale aware (contains, starts with, etc.)
  76. private final String lowerText;
  77. private final OperatorEnum operator;
  78. private final ConditionType type;
  79. // cached for performance, to avoid reading the XMLBean every time a conditionally formatted cell is rendered
  80. private final ExcelNumberFormat numberFormat;
  81. // cached for performance, used to format numeric cells for string comparisons. See Bug #61764 for explanation
  82. private final DecimalFormat decimalTextFormat;
  83. /**
  84. *
  85. * @param workbookEvaluator
  86. * @param sheet
  87. * @param formatting
  88. * @param formattingIndex for priority, zero based
  89. * @param rule
  90. * @param ruleIndex for priority, zero based, if this is an HSSF rule. Unused for XSSF rules
  91. * @param regions could be read from formatting, but every call creates new objects in a new array.
  92. * this allows calling it once per formatting instance, and re-using the array.
  93. */
  94. public EvaluationConditionalFormatRule(WorkbookEvaluator workbookEvaluator, Sheet sheet, ConditionalFormatting formatting, int formattingIndex, ConditionalFormattingRule rule, int ruleIndex, CellRangeAddress[] regions) {
  95. super();
  96. this.workbookEvaluator = workbookEvaluator;
  97. this.sheet = sheet;
  98. this.formatting = formatting;
  99. this.rule = rule;
  100. this.formattingIndex = formattingIndex;
  101. this.ruleIndex = ruleIndex;
  102. this.priority = rule.getPriority();
  103. this.regions = regions;
  104. formula1 = rule.getFormula1();
  105. formula2 = rule.getFormula2();
  106. text = rule.getText();
  107. lowerText = text == null ? null : text.toLowerCase(LocaleUtil.getUserLocale());
  108. numberFormat = rule.getNumberFormat();
  109. operator = OperatorEnum.values()[rule.getComparisonOperation()];
  110. type = rule.getConditionType();
  111. // Excel uses the stored text representation from the XML apparently, in tests done so far
  112. decimalTextFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
  113. decimalTextFormat.setMaximumFractionDigits(340); // DecimalFormat.DOUBLE_FRACTION_DIGITS, which is default scoped
  114. }
  115. /**
  116. * @return sheet
  117. */
  118. public Sheet getSheet() {
  119. return sheet;
  120. }
  121. /**
  122. * @return the formatting
  123. */
  124. public ConditionalFormatting getFormatting() {
  125. return formatting;
  126. }
  127. /**
  128. * @return conditional formatting index
  129. */
  130. public int getFormattingIndex() {
  131. return formattingIndex;
  132. }
  133. /**
  134. * @return Excel number format string to apply to matching cells, or null to keep the cell default
  135. */
  136. public ExcelNumberFormat getNumberFormat() {
  137. return numberFormat;
  138. }
  139. /**
  140. * @return the rule
  141. */
  142. public ConditionalFormattingRule getRule() {
  143. return rule;
  144. }
  145. /**
  146. * @return rule index
  147. */
  148. public int getRuleIndex() {
  149. return ruleIndex;
  150. }
  151. /**
  152. * @return the regions
  153. */
  154. public CellRangeAddress[] getRegions() {
  155. return regions;
  156. }
  157. /**
  158. * @return the priority
  159. */
  160. public int getPriority() {
  161. return priority;
  162. }
  163. /**
  164. * @return the formula1
  165. */
  166. public String getFormula1() {
  167. return formula1;
  168. }
  169. /**
  170. * @return the formula2
  171. */
  172. public String getFormula2() {
  173. return formula2;
  174. }
  175. /**
  176. * @return condition text if any, or null
  177. */
  178. public String getText() {
  179. return text;
  180. }
  181. /**
  182. * @return the operator
  183. */
  184. public OperatorEnum getOperator() {
  185. return operator;
  186. }
  187. /**
  188. * @return the type
  189. */
  190. public ConditionType getType() {
  191. return type;
  192. }
  193. /**
  194. * Defined as equal sheet name and formatting and rule indexes
  195. * @see java.lang.Object#equals(java.lang.Object)
  196. */
  197. @Override
  198. public boolean equals(Object obj) {
  199. if (obj == null) {
  200. return false;
  201. }
  202. if (! obj.getClass().equals(this.getClass())) {
  203. return false;
  204. }
  205. final EvaluationConditionalFormatRule r = (EvaluationConditionalFormatRule) obj;
  206. return getSheet().getSheetName().equalsIgnoreCase(r.getSheet().getSheetName())
  207. && getFormattingIndex() == r.getFormattingIndex()
  208. && getRuleIndex() == r.getRuleIndex();
  209. }
  210. /**
  211. * Per Excel Help, XSSF rule priority is sheet-wide, not just within the owning ConditionalFormatting object.
  212. * This can be seen by creating 4 rules applying to two different ranges and examining the XML.
  213. * <p>
  214. * HSSF priority is based on definition/persistence order.
  215. *
  216. * @param o
  217. * @return comparison based on sheet name, formatting index, and rule priority
  218. */
  219. @Override
  220. public int compareTo(EvaluationConditionalFormatRule o) {
  221. int cmp = getSheet().getSheetName().compareToIgnoreCase(o.getSheet().getSheetName());
  222. if (cmp != 0) {
  223. return cmp;
  224. }
  225. final int x = getPriority();
  226. final int y = o.getPriority();
  227. // logic from Integer.compare()
  228. cmp = Integer.compare(x, y);
  229. if (cmp != 0) {
  230. return cmp;
  231. }
  232. cmp = Integer.compare(getFormattingIndex(), o.getFormattingIndex());
  233. if (cmp != 0) {
  234. return cmp;
  235. }
  236. return Integer.compare(getRuleIndex(), o.getRuleIndex());
  237. }
  238. @Override
  239. public int hashCode() {
  240. int hash = sheet.getSheetName().hashCode();
  241. hash = 31 * hash + formattingIndex;
  242. hash = 31 * hash + ruleIndex;
  243. return hash;
  244. }
  245. /**
  246. * @param ref
  247. * @return true if this rule evaluates to true for the given cell
  248. */
  249. /* package */ boolean matches(CellReference ref) {
  250. // first check that it is in one of the regions defined for this format
  251. CellRangeAddress region = null;
  252. for (CellRangeAddress r : regions) {
  253. if (r.isInRange(ref)) {
  254. region = r;
  255. break;
  256. }
  257. }
  258. if (region == null) {
  259. // cell not in range of this rule
  260. return false;
  261. }
  262. final ConditionType ruleType = getRule().getConditionType();
  263. // these rules apply to all cells in a region. Specific condition criteria
  264. // may specify no special formatting for that value partition, but that's display logic
  265. if (ruleType.equals(ConditionType.COLOR_SCALE)
  266. || ruleType.equals(ConditionType.DATA_BAR)
  267. || ruleType.equals(ConditionType.ICON_SET)) {
  268. return true;
  269. }
  270. Cell cell = null;
  271. final Row row = sheet.getRow(ref.getRow());
  272. if (row != null) {
  273. cell = row.getCell(ref.getCol());
  274. }
  275. if (ruleType.equals(ConditionType.CELL_VALUE_IS)) {
  276. // undefined cells never match a VALUE_IS condition
  277. if (cell == null) return false;
  278. return checkValue(cell, region);
  279. }
  280. if (ruleType.equals(ConditionType.FORMULA)) {
  281. return checkFormula(ref, region);
  282. }
  283. if (ruleType.equals(ConditionType.FILTER)) {
  284. return checkFilter(cell, ref, region);
  285. }
  286. // TODO: anything else, we don't handle yet, such as top 10
  287. return false;
  288. }
  289. /**
  290. * @param cell the cell to check for
  291. * @param region for adjusting relative formulas
  292. * @return if the value of the cell is valid or not for the formatting rule
  293. */
  294. private boolean checkValue(Cell cell, CellRangeAddress region) {
  295. if (cell == null || DataValidationEvaluator.isType(cell, CellType.BLANK)
  296. || DataValidationEvaluator.isType(cell,CellType.ERROR)
  297. || (DataValidationEvaluator.isType(cell,CellType.STRING)
  298. && (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty())
  299. )
  300. ) {
  301. return false;
  302. }
  303. ValueEval eval = unwrapEval(workbookEvaluator.evaluate(rule.getFormula1(), ConditionalFormattingEvaluator.getRef(cell), region));
  304. String f2 = rule.getFormula2();
  305. ValueEval eval2 = BlankEval.instance;
  306. if (f2 != null && f2.length() > 0) {
  307. eval2 = unwrapEval(workbookEvaluator.evaluate(f2, ConditionalFormattingEvaluator.getRef(cell), region));
  308. }
  309. // we assume the cell has been evaluated, and the current formula value stored
  310. if (DataValidationEvaluator.isType(cell, CellType.BOOLEAN)
  311. && (eval == BlankEval.instance || eval instanceof BoolEval)
  312. && (eval2 == BlankEval.instance || eval2 instanceof BoolEval)
  313. ) {
  314. return operator.isValid(cell.getBooleanCellValue(), eval == BlankEval.instance ? null : ((BoolEval) eval).getBooleanValue(), eval2 == BlankEval.instance ? null : ((BoolEval) eval2).getBooleanValue());
  315. }
  316. if (DataValidationEvaluator.isType(cell, CellType.NUMERIC)
  317. && (eval == BlankEval.instance || eval instanceof NumberEval )
  318. && (eval2 == BlankEval.instance || eval2 instanceof NumberEval)
  319. ) {
  320. return operator.isValid(cell.getNumericCellValue(), eval == BlankEval.instance ? null : ((NumberEval) eval).getNumberValue(), eval2 == BlankEval.instance ? null : ((NumberEval) eval2).getNumberValue());
  321. }
  322. if (DataValidationEvaluator.isType(cell, CellType.STRING)
  323. && (eval == BlankEval.instance || eval instanceof StringEval )
  324. && (eval2 == BlankEval.instance || eval2 instanceof StringEval)
  325. ) {
  326. return operator.isValid(cell.getStringCellValue(), eval == BlankEval.instance ? null : ((StringEval) eval).getStringValue(), eval2 == BlankEval.instance ? null : ((StringEval) eval2).getStringValue());
  327. }
  328. return operator.isValidForIncompatibleTypes();
  329. }
  330. private ValueEval unwrapEval(ValueEval eval) {
  331. ValueEval comp = eval;
  332. while (comp instanceof RefEval) {
  333. RefEval ref = (RefEval) comp;
  334. comp = ref.getInnerValueEval(ref.getFirstSheetIndex());
  335. }
  336. return comp;
  337. }
  338. /**
  339. * @param ref needed for offsets from region anchor - may be null!
  340. * @param region for adjusting relative formulas
  341. * @return true/false using the same rules as Data Validation evaluations
  342. */
  343. private boolean checkFormula(CellReference ref, CellRangeAddress region) {
  344. ValueEval comp = unwrapEval(workbookEvaluator.evaluate(rule.getFormula1(), ref, region));
  345. // Copied for now from DataValidationEvaluator.ValidationEnum.FORMULA#isValidValue()
  346. if (comp instanceof BlankEval) {
  347. return true;
  348. }
  349. if (comp instanceof ErrorEval) {
  350. return false;
  351. }
  352. if (comp instanceof BoolEval) {
  353. return ((BoolEval) comp).getBooleanValue();
  354. }
  355. // empirically tested in Excel - 0=false, any other number = true/valid
  356. // see test file DataValidationEvaluations.xlsx
  357. if (comp instanceof NumberEval) {
  358. return ((NumberEval) comp).getNumberValue() != 0;
  359. }
  360. return false; // anything else is false, such as text
  361. }
  362. private boolean checkFilter(Cell cell, CellReference ref, CellRangeAddress region) {
  363. final ConditionFilterType filterType = rule.getConditionFilterType();
  364. if (filterType == null) {
  365. return false;
  366. }
  367. final ValueAndFormat cv = getCellValue(cell);
  368. // TODO: this could/should be delegated to the Enum type, but that's in the usermodel package,
  369. // we may not want evaluation code there. Of course, maybe the enum should go here in formula,
  370. // and not be returned by the SS model, but then we need the XSSF rule to expose the raw OOXML
  371. // type value, which isn't ideal either.
  372. switch (filterType) {
  373. case FILTER:
  374. return false; // we don't evaluate HSSF filters yet
  375. case TOP_10:
  376. // from testing, Excel only operates on numbers and dates (which are stored as numbers) in the range.
  377. // numbers stored as text are ignored, but numbers formatted as text are treated as numbers.
  378. if (! cv.isNumber()) {
  379. return false;
  380. }
  381. return getMeaningfulValues(region, false, new ValueFunction() {
  382. @Override
  383. public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
  384. final ConditionFilterData conf = rule.getFilterConfiguration();
  385. if (! conf.getBottom()) {
  386. allValues.sort(Collections.reverseOrder());
  387. } else {
  388. Collections.sort(allValues);
  389. }
  390. int limit = (int) conf.getRank();
  391. if (conf.getPercent()) {
  392. limit = allValues.size() * limit / 100;
  393. }
  394. if (allValues.size() <= limit) {
  395. return new HashSet<>(allValues);
  396. }
  397. return new HashSet<>(allValues.subList(0, limit));
  398. }
  399. }).contains(cv);
  400. case UNIQUE_VALUES:
  401. // Per Excel help, "duplicate" means matching value AND format
  402. // https://support.office.com/en-us/article/Filter-for-unique-values-or-remove-duplicate-values-ccf664b0-81d6-449b-bbe1-8daaec1e83c2
  403. return getMeaningfulValues(region, true, new ValueFunction() {
  404. @Override
  405. public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
  406. Collections.sort(allValues);
  407. final Set<ValueAndFormat> unique = new HashSet<>();
  408. for (int i = 0; i < allValues.size(); i++) {
  409. final ValueAndFormat v = allValues.get(i);
  410. // skip this if the current value matches the next one, or is the last one and matches the previous one
  411. if ( (i < allValues.size()-1 && v.equals(allValues.get(i+1)) ) || ( i > 0 && i == allValues.size()-1 && v.equals(allValues.get(i-1)) ) ) {
  412. // current value matches next value, skip both
  413. i++;
  414. continue;
  415. }
  416. unique.add(v);
  417. }
  418. return unique;
  419. }
  420. }).contains(cv);
  421. case DUPLICATE_VALUES:
  422. // Per Excel help, "duplicate" means matching value AND format
  423. // https://support.office.com/en-us/article/Filter-for-unique-values-or-remove-duplicate-values-ccf664b0-81d6-449b-bbe1-8daaec1e83c2
  424. return getMeaningfulValues(region, true, new ValueFunction() {
  425. @Override
  426. public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
  427. Collections.sort(allValues);
  428. final Set<ValueAndFormat> dup = new HashSet<>();
  429. for (int i = 0; i < allValues.size(); i++) {
  430. final ValueAndFormat v = allValues.get(i);
  431. // skip this if the current value matches the next one, or is the last one and matches the previous one
  432. if ( (i < allValues.size()-1 && v.equals(allValues.get(i+1)) ) || ( i > 0 && i == allValues.size()-1 && v.equals(allValues.get(i-1)) ) ) {
  433. // current value matches next value, add one
  434. dup.add(v);
  435. i++;
  436. }
  437. }
  438. return dup;
  439. }
  440. }).contains(cv);
  441. case ABOVE_AVERAGE:
  442. // from testing, Excel only operates on numbers and dates (which are stored as numbers) in the range.
  443. // numbers stored as text are ignored, but numbers formatted as text are treated as numbers.
  444. final ConditionFilterData conf = rule.getFilterConfiguration();
  445. // actually ordered, so iteration order is predictable
  446. List<ValueAndFormat> values = new ArrayList<>(getMeaningfulValues(region, false, new ValueFunction() {
  447. @Override
  448. public Set<ValueAndFormat> evaluate(List<ValueAndFormat> allValues) {
  449. double total = 0;
  450. ValueEval[] pop = new ValueEval[allValues.size()];
  451. for (int i = 0; i < allValues.size(); i++) {
  452. ValueAndFormat v = allValues.get(i);
  453. total += v.value.doubleValue();
  454. pop[i] = new NumberEval(v.value.doubleValue());
  455. }
  456. final Set<ValueAndFormat> avgSet = new LinkedHashSet<>(1);
  457. avgSet.add(new ValueAndFormat(Double.valueOf(allValues.size() == 0 ? 0 : total / allValues.size()), null, decimalTextFormat));
  458. final double stdDev = allValues.size() <= 1 ? 0 : ((NumberEval) AggregateFunction.STDEV.evaluate(pop, 0, 0)).getNumberValue();
  459. avgSet.add(new ValueAndFormat(Double.valueOf(stdDev), null, decimalTextFormat));
  460. return avgSet;
  461. }
  462. }));
  463. Double val = cv.isNumber() ? cv.getValue() : null;
  464. if (val == null) {
  465. return false;
  466. }
  467. double avg = values.get(0).value.doubleValue();
  468. double stdDev = values.get(1).value.doubleValue();
  469. /*
  470. * use StdDev, aboveAverage, equalAverage to find:
  471. * comparison value
  472. * operator type
  473. */
  474. Double comp = Double.valueOf(conf.getStdDev() > 0 ? (avg + (conf.getAboveAverage() ? 1 : -1) * stdDev * conf.getStdDev()) : avg) ;
  475. final OperatorEnum op;
  476. if (conf.getAboveAverage()) {
  477. if (conf.getEqualAverage()) {
  478. op = OperatorEnum.GREATER_OR_EQUAL;
  479. } else {
  480. op = OperatorEnum.GREATER_THAN;
  481. }
  482. } else {
  483. if (conf.getEqualAverage()) {
  484. op = OperatorEnum.LESS_OR_EQUAL;
  485. } else {
  486. op = OperatorEnum.LESS_THAN;
  487. }
  488. }
  489. return op.isValid(val, comp, null);
  490. case CONTAINS_TEXT:
  491. // implemented both by a cfRule "text" attribute and a formula. Use the text.
  492. return text == null ? false : cv.toString().toLowerCase(LocaleUtil.getUserLocale()).contains(lowerText);
  493. case NOT_CONTAINS_TEXT:
  494. // implemented both by a cfRule "text" attribute and a formula. Use the text.
  495. return text == null ? true : ! cv.toString().toLowerCase(LocaleUtil.getUserLocale()).contains(lowerText);
  496. case BEGINS_WITH:
  497. // implemented both by a cfRule "text" attribute and a formula. Use the text.
  498. return cv.toString().toLowerCase(LocaleUtil.getUserLocale()).startsWith(lowerText);
  499. case ENDS_WITH:
  500. // implemented both by a cfRule "text" attribute and a formula. Use the text.
  501. return cv.toString().toLowerCase(LocaleUtil.getUserLocale()).endsWith(lowerText);
  502. case CONTAINS_BLANKS:
  503. try {
  504. String v = cv.getString();
  505. // see TextFunction.TRIM for implementation
  506. return v == null || v.trim().length() == 0;
  507. } catch (Exception e) {
  508. // not a valid string value, and not a blank cell (that's checked earlier)
  509. return false;
  510. }
  511. case NOT_CONTAINS_BLANKS:
  512. try {
  513. String v = cv.getString();
  514. // see TextFunction.TRIM for implementation
  515. return v != null && v.trim().length() > 0;
  516. } catch (Exception e) {
  517. // not a valid string value, but not blank
  518. return true;
  519. }
  520. case CONTAINS_ERRORS:
  521. return cell != null && DataValidationEvaluator.isType(cell, CellType.ERROR);
  522. case NOT_CONTAINS_ERRORS:
  523. return cell == null || ! DataValidationEvaluator.isType(cell, CellType.ERROR);
  524. case TIME_PERIOD:
  525. // implemented both by a cfRule "text" attribute and a formula. Use the formula.
  526. return checkFormula(ref, region);
  527. default:
  528. return false;
  529. }
  530. }
  531. /**
  532. * from testing, Excel only operates on numbers and dates (which are stored as numbers) in the range.
  533. * numbers stored as text are ignored, but numbers formatted as text are treated as numbers.
  534. *
  535. * @param region
  536. * @return the meaningful values in the range of cells specified
  537. */
  538. private Set<ValueAndFormat> getMeaningfulValues(CellRangeAddress region, boolean withText, ValueFunction func) {
  539. Set<ValueAndFormat> values = meaningfulRegionValues.get(region);
  540. if (values != null) {
  541. return values;
  542. }
  543. List<ValueAndFormat> allValues = new ArrayList<>((region.getLastColumn() - region.getFirstColumn() + 1) * (region.getLastRow() - region.getFirstRow() + 1));
  544. for (int r=region.getFirstRow(); r <= region.getLastRow(); r++) {
  545. final Row row = sheet.getRow(r);
  546. if (row == null) {
  547. continue;
  548. }
  549. for (int c = region.getFirstColumn(); c <= region.getLastColumn(); c++) {
  550. Cell cell = row.getCell(c);
  551. final ValueAndFormat cv = getCellValue(cell);
  552. if (withText || cv.isNumber()) {
  553. allValues.add(cv);
  554. }
  555. }
  556. }
  557. values = func.evaluate(allValues);
  558. meaningfulRegionValues.put(region, values);
  559. return values;
  560. }
  561. private ValueAndFormat getCellValue(Cell cell) {
  562. if (cell != null) {
  563. final String format = cell.getCellStyle().getDataFormatString();
  564. CellType type = cell.getCellType();
  565. if (type == CellType.FORMULA) {
  566. type = cell.getCachedFormulaResultType();
  567. }
  568. switch (type) {
  569. case NUMERIC:
  570. return new ValueAndFormat(Double.valueOf(cell.getNumericCellValue()), format, decimalTextFormat);
  571. case STRING:
  572. case BOOLEAN:
  573. return new ValueAndFormat(cell.getStringCellValue(), format);
  574. default:
  575. break;
  576. }
  577. }
  578. return new ValueAndFormat("", "");
  579. }
  580. /**
  581. * instances evaluate the values for a region and return the positive matches for the function type.
  582. * TODO: when we get to use Java 8, this is obviously a Lambda Function.
  583. */
  584. protected interface ValueFunction {
  585. /**
  586. *
  587. * @param values
  588. * @return the desired values for the rules implemented by the current instance
  589. */
  590. Set<ValueAndFormat> evaluate(List<ValueAndFormat> values);
  591. }
  592. /**
  593. * Not calling it OperatorType to avoid confusion for now with other classes.
  594. * Definition order matches OOXML type ID indexes.
  595. * Note that this has NO_COMPARISON as the first item, unlike the similar
  596. * DataValidation operator enum. Thanks, Microsoft.
  597. */
  598. public static enum OperatorEnum {
  599. NO_COMPARISON {
  600. /** always false/invalid */
  601. @Override
  602. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  603. return false;
  604. }
  605. },
  606. BETWEEN {
  607. @Override
  608. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  609. if (v1 == null) {
  610. if (cellValue instanceof Number) {
  611. // use zero for null
  612. double n1 = 0;
  613. double n2 = v2 == null ? 0 : ((Number) v2).doubleValue();
  614. return Double.compare( ((Number) cellValue).doubleValue(), n1) >= 0 && Double.compare(((Number) cellValue).doubleValue(), n2) <= 0;
  615. } else if (cellValue instanceof String) {
  616. String n1 = "";
  617. String n2 = v2 == null ? "" : (String) v2;
  618. return ((String) cellValue).compareToIgnoreCase(n1) >= 0 && ((String) cellValue).compareToIgnoreCase(n2) <= 0;
  619. } else if (cellValue instanceof Boolean) return false;
  620. return false; // just in case - not a typical possibility
  621. }
  622. return cellValue.compareTo(v1) >= 0 && cellValue.compareTo(v2) <= 0;
  623. }
  624. },
  625. NOT_BETWEEN {
  626. @Override
  627. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  628. if (v1 == null) {
  629. if (cellValue instanceof Number) {
  630. // use zero for null
  631. double n1 = 0;
  632. double n2 = v2 == null ? 0 : ((Number) v2).doubleValue();
  633. return Double.compare( ((Number) cellValue).doubleValue(), n1) < 0 || Double.compare(((Number) cellValue).doubleValue(), n2) > 0;
  634. } else if (cellValue instanceof String) {
  635. String n1 = "";
  636. String n2 = v2 == null ? "" : (String) v2;
  637. return ((String) cellValue).compareToIgnoreCase(n1) < 0 || ((String) cellValue).compareToIgnoreCase(n2) > 0;
  638. } else if (cellValue instanceof Boolean) return true;
  639. return false; // just in case - not a typical possibility
  640. }
  641. return cellValue.compareTo(v1) < 0 || cellValue.compareTo(v2) > 0;
  642. }
  643. public boolean isValidForIncompatibleTypes() {
  644. return true;
  645. }
  646. },
  647. EQUAL {
  648. @Override
  649. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  650. if (v1 == null) {
  651. if (cellValue instanceof Number) {
  652. // use zero for null
  653. return Double.compare( ((Number) cellValue).doubleValue(), 0) == 0;
  654. } else if (cellValue instanceof String) {
  655. return false; // even an empty string is not equal the empty cell, only another empty cell is, handled higher up
  656. } else if (cellValue instanceof Boolean) return false;
  657. return false; // just in case - not a typical possibility
  658. }
  659. // need to avoid instanceof, to work around a 1.6 compiler bug
  660. if (cellValue.getClass() == String.class) {
  661. return cellValue.toString().compareToIgnoreCase(v1.toString()) == 0;
  662. }
  663. return cellValue.compareTo(v1) == 0;
  664. }
  665. },
  666. NOT_EQUAL {
  667. @Override
  668. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  669. if (v1 == null) {
  670. return true; // non-null not equal null, returns true
  671. }
  672. // need to avoid instanceof, to work around a 1.6 compiler bug
  673. if (cellValue.getClass() == String.class) {
  674. return cellValue.toString().compareToIgnoreCase(v1.toString()) == 0;
  675. }
  676. return cellValue.compareTo(v1) != 0;
  677. }
  678. public boolean isValidForIncompatibleTypes() {
  679. return true;
  680. }
  681. },
  682. GREATER_THAN {
  683. @Override
  684. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  685. if (v1 == null) {
  686. if (cellValue instanceof Number) {
  687. // use zero for null
  688. return Double.compare( ((Number) cellValue).doubleValue(), 0) > 0;
  689. } else if (cellValue instanceof String) {
  690. return true; // non-null string greater than empty cell
  691. } else if (cellValue instanceof Boolean) return true;
  692. return false; // just in case - not a typical possibility
  693. }
  694. return cellValue.compareTo(v1) > 0;
  695. }
  696. },
  697. LESS_THAN {
  698. @Override
  699. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  700. if (v1 == null) {
  701. if (cellValue instanceof Number) {
  702. // use zero for null
  703. return Double.compare( ((Number) cellValue).doubleValue(), 0) < 0;
  704. } else if (cellValue instanceof String) {
  705. return false; // non-null string greater than empty cell
  706. } else if (cellValue instanceof Boolean) return false;
  707. return false; // just in case - not a typical possibility
  708. }
  709. return cellValue.compareTo(v1) < 0;
  710. }
  711. },
  712. GREATER_OR_EQUAL {
  713. @Override
  714. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  715. if (v1 == null) {
  716. if (cellValue instanceof Number) {
  717. // use zero for null
  718. return Double.compare( ((Number) cellValue).doubleValue(), 0) >= 0;
  719. } else if (cellValue instanceof String) {
  720. return true; // non-null string greater than empty cell
  721. } else if (cellValue instanceof Boolean) return true;
  722. return false; // just in case - not a typical possibility
  723. }
  724. return cellValue.compareTo(v1) >= 0;
  725. }
  726. },
  727. LESS_OR_EQUAL {
  728. @Override
  729. public <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2) {
  730. if (v1 == null) {
  731. if (cellValue instanceof Number) {
  732. // use zero for null
  733. return Double.compare( ((Number) cellValue).doubleValue(), 0) <= 0;
  734. } else if (cellValue instanceof String) {
  735. return false; // non-null string not less than empty cell
  736. } else if (cellValue instanceof Boolean) return false; // for completeness
  737. return false; // just in case - not a typical possibility
  738. }
  739. return cellValue.compareTo(v1) <= 0;
  740. }
  741. },
  742. ;
  743. /**
  744. * Evaluates comparison using operator instance rules
  745. * @param cellValue won't be null, assumption is previous checks handled that
  746. * @param v1 if null, per Excel behavior various results depending on the type of cellValue and the specific enum instance
  747. * @param v2 null if not needed. If null when needed, various results, per Excel behavior
  748. * @return true if the comparison is valid
  749. */
  750. public abstract <C extends Comparable<C>> boolean isValid(C cellValue, C v1, C v2);
  751. /**
  752. * Called when the cell and comparison values are of different data types
  753. * Needed for negation operators, which should return true.
  754. * @return true if this comparison is true when the types to compare are different
  755. */
  756. public boolean isValidForIncompatibleTypes() {
  757. return false;
  758. }
  759. }
  760. /**
  761. * Note: this class has a natural ordering that is inconsistent with equals.
  762. */
  763. protected static class ValueAndFormat implements Comparable<ValueAndFormat> {
  764. private final Double value;
  765. private final String string;
  766. private final String format;
  767. private final DecimalFormat decimalTextFormat;
  768. public ValueAndFormat(Double value, String format, DecimalFormat df) {
  769. this.value = value;
  770. this.format = format;
  771. string = null;
  772. decimalTextFormat = df;
  773. }
  774. public ValueAndFormat(String value, String format) {
  775. this.value = null;
  776. this.format = format;
  777. string = value;
  778. decimalTextFormat = null;
  779. }
  780. public boolean isNumber() {
  781. return value != null;
  782. }
  783. public Double getValue() {
  784. return value;
  785. }
  786. public String getString() {
  787. return string;
  788. }
  789. public String toString() {
  790. if(isNumber()) {
  791. return decimalTextFormat.format(getValue().doubleValue());
  792. } else {
  793. return getString();
  794. }
  795. }
  796. @Override
  797. public boolean equals(Object obj) {
  798. if (!(obj instanceof ValueAndFormat)) {
  799. return false;
  800. }
  801. ValueAndFormat o = (ValueAndFormat) obj;
  802. return (Objects.equals(value, o.value)
  803. && Objects.equals(format, o.format)
  804. && Objects.equals(string, o.string));
  805. }
  806. /**
  807. * Note: this class has a natural ordering that is inconsistent with equals.
  808. * @param o
  809. * @return value comparison
  810. */
  811. @Override
  812. public int compareTo(ValueAndFormat o) {
  813. if (value == null && o.value != null) {
  814. return 1;
  815. }
  816. if (o.value == null && value != null) {
  817. return -1;
  818. }
  819. int cmp = value == null ? 0 : value.compareTo(o.value);
  820. if (cmp != 0) {
  821. return cmp;
  822. }
  823. if (string == null && o.string != null) {
  824. return 1;
  825. }
  826. if (o.string == null && string != null) {
  827. return -1;
  828. }
  829. return string == null ? 0 : string.compareTo(o.string);
  830. }
  831. @Override
  832. public int hashCode() {
  833. return (string == null ? 0 : string.hashCode()) * 37 * 37 + 37 * (value == null ? 0 : value.hashCode()) + (format == null ? 0 : format.hashCode());
  834. }
  835. }
  836. }