use of org.apache.poi.ss.formula.EvaluationConditionalFormatRule in project poi by apache.
the class ConditionalFormats method evaluateRules.
/**
* Print out a summary of the conditional formatting rules applied to cells on the given sheet.
* Only cells with a matching rule are printed, and for those, all matching rules are sumarized.
* @param wb
* @param sheetName
*/
static void evaluateRules(Workbook wb, String sheetName) {
final WorkbookEvaluatorProvider wbEvalProv = (WorkbookEvaluatorProvider) wb.getCreationHelper().createFormulaEvaluator();
final ConditionalFormattingEvaluator cfEval = new ConditionalFormattingEvaluator(wb, wbEvalProv);
// if cell values have changed, clear cached format results
cfEval.clearAllCachedValues();
final Sheet sheet = wb.getSheet(sheetName);
for (Row r : sheet) {
for (Cell c : r) {
final List<EvaluationConditionalFormatRule> rules = cfEval.getConditionalFormattingForCell(c);
// check rules list for null, although current implementation will return an empty list, not null, then do what you want with results
if (rules == null || rules.isEmpty())
continue;
final CellReference ref = ConditionalFormattingEvaluator.getRef(c);
if (rules.isEmpty())
continue;
System.out.println("\n" + ref.formatAsString() + " has conditional formatting.");
for (EvaluationConditionalFormatRule rule : rules) {
ConditionalFormattingRule cf = rule.getRule();
StringBuilder b = new StringBuilder();
b.append("\tRule ").append(rule.getFormattingIndex()).append(": ");
// check for color scale
if (cf.getColorScaleFormatting() != null) {
b.append("\n\t\tcolor scale (caller must calculate bucket)");
}
// check for data bar
if (cf.getDataBarFormatting() != null) {
b.append("\n\t\tdata bar (caller must calculate bucket)");
}
// check for icon set
if (cf.getMultiStateFormatting() != null) {
b.append("\n\t\ticon set (caller must calculate icon bucket)");
}
// check for fill
if (cf.getPatternFormatting() != null) {
final PatternFormatting fill = cf.getPatternFormatting();
b.append("\n\t\tfill pattern ").append(fill.getFillPattern()).append(" color index ").append(fill.getFillBackgroundColor());
}
// font stuff
if (cf.getFontFormatting() != null) {
final FontFormatting ff = cf.getFontFormatting();
b.append("\n\t\tfont format ").append("color index ").append(ff.getFontColorIndex());
if (ff.isBold())
b.append(" bold");
if (ff.isItalic())
b.append(" italic");
if (ff.isStruckout())
b.append(" strikeout");
b.append(" underline index ").append(ff.getUnderlineType());
}
System.out.println(b);
}
}
}
}
use of org.apache.poi.ss.formula.EvaluationConditionalFormatRule in project poi by apache.
the class ExcelNumberFormat method from.
/**
* @param cell cell to extract format from
* @param cfEvaluator ConditionalFormattingEvaluator to use, or null if none in this context
* @return number format from highest-priority rule with a number format, or the cell style, or null if none of the above apply/are defined
*/
public static ExcelNumberFormat from(Cell cell, ConditionalFormattingEvaluator cfEvaluator) {
if (cell == null)
return null;
ExcelNumberFormat nf = null;
if (cfEvaluator != null) {
// first one wins (priority order, per Excel help)
List<EvaluationConditionalFormatRule> rules = cfEvaluator.getConditionalFormattingForCell(cell);
for (EvaluationConditionalFormatRule rule : rules) {
nf = rule.getNumberFormat();
if (nf != null)
break;
}
}
if (nf == null) {
CellStyle style = cell.getCellStyle();
nf = ExcelNumberFormat.from(style);
}
return nf;
}
Aggregations