public int AddConditionalFormatting(CellRangeAddress[] regions, IConditionalFormattingRule[] cfRules) { if (regions == null) { throw new ArgumentException("regions must not be null"); } foreach (CellRangeAddress range in regions) { range.Validate(SpreadsheetVersion.EXCEL2007); } if (cfRules == null) { throw new ArgumentException("cfRules must not be null"); } if (cfRules.Length == 0) { throw new ArgumentException("cfRules must not be empty"); } if (cfRules.Length > 3) { throw new ArgumentException("Number of rules must not exceed 3"); } CellRangeAddress[] mergeCellRanges = CellRangeUtil.MergeCellRanges(regions); CT_ConditionalFormatting cf = _sheet.GetCTWorksheet().AddNewConditionalFormatting(); string refs = string.Empty; foreach (CellRangeAddress a in mergeCellRanges) { if (refs.Length == 0) { refs = a.FormatAsString(); } else { refs += " " + a.FormatAsString(); } } cf.sqref = refs; int priority = 1; foreach (CT_ConditionalFormatting c in _sheet.GetCTWorksheet().conditionalFormatting) { priority += c.sizeOfCfRuleArray(); } foreach (IConditionalFormattingRule rule in cfRules) { XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)rule; xRule.GetCTCfRule().priority = (priority++); cf.AddNewCfRule().Set(xRule.GetCTCfRule()); } return(_sheet.GetCTWorksheet().SizeOfConditionalFormattingArray() - 1); }
/** * A factory method allowing to create a conditional formatting rule with a formula.<br> * * @param formula - formula for the valued, Compared with the cell */ public IConditionalFormattingRule CreateConditionalFormattingRule(string formula) { XSSFConditionalFormattingRule rule = new XSSFConditionalFormattingRule(_sheet); CT_CfRule cfRule = rule.GetCTCfRule(); cfRule.AddFormula(formula); cfRule.type = ST_CfType.expression; return(rule); }
/** * A factory method allowing to create a conditional formatting rule * with a cell comparison operator<p/> * TODO - formulas Containing cell references are currently not Parsed properly * * @param comparisonOperation - a constant value from * <tt>{@link Npoi.Core.hssf.record.CFRuleRecord.ComparisonOperator}</tt>: <p> * <ul> * <li>BETWEEN</li> * <li>NOT_BETWEEN</li> * <li>EQUAL</li> * <li>NOT_EQUAL</li> * <li>GT</li> * <li>LT</li> * <li>GE</li> * <li>LE</li> * </ul> * </p> * @param formula1 - formula for the valued, Compared with the cell * @param formula2 - second formula (only used with * {@link Npoi.Core.ss.usermodel.ComparisonOperator#BETWEEN}) and * {@link Npoi.Core.ss.usermodel.ComparisonOperator#NOT_BETWEEN} operations) */ public IConditionalFormattingRule CreateConditionalFormattingRule( ComparisonOperator comparisonOperation, String formula1, String formula2) { XSSFConditionalFormattingRule rule = new XSSFConditionalFormattingRule(_sheet); CT_CfRule cfRule = rule.GetCTCfRule(); cfRule.AddFormula(formula1); if (formula2 != null) { cfRule.AddFormula(formula2); } cfRule.type = (ST_CfType.cellIs); ST_ConditionalFormattingOperator operator1; switch (comparisonOperation) { case ComparisonOperator.Between: operator1 = ST_ConditionalFormattingOperator.between; break; case ComparisonOperator.NotBetween: operator1 = ST_ConditionalFormattingOperator.notBetween; break; case ComparisonOperator.LessThan: operator1 = ST_ConditionalFormattingOperator.lessThan; break; case ComparisonOperator.LessThanOrEqual: operator1 = ST_ConditionalFormattingOperator.lessThanOrEqual; break; case ComparisonOperator.GreaterThan: operator1 = ST_ConditionalFormattingOperator.greaterThan; break; case ComparisonOperator.GreaterThanOrEqual: operator1 = ST_ConditionalFormattingOperator.greaterThanOrEqual; break; case ComparisonOperator.Equal: operator1 = ST_ConditionalFormattingOperator.equal; break; case ComparisonOperator.NotEqual: operator1 = ST_ConditionalFormattingOperator.notEqual; break; default: throw new ArgumentException("Unknown comparison operator: " + comparisonOperation); } cfRule.@operator = (operator1); return(rule); }
/** * Add a Conditional Formatting rule. * Excel allows to create up to 3 Conditional Formatting rules. * * @param cfRule - Conditional Formatting rule */ public void AddRule(IConditionalFormattingRule cfRule) { XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule; _cf.AddNewCfRule().Set(xRule.GetCTCfRule()); }
/** * Replaces an existing Conditional Formatting rule at position idx. * Excel allows to create up to 3 Conditional Formatting rules. * This method can be useful to modify existing Conditional Formatting rules. * * @param idx position of the rule. Should be between 0 and 2. * @param cfRule - Conditional Formatting rule */ public void SetRule(int idx, IConditionalFormattingRule cfRule) { XSSFConditionalFormattingRule xRule = (XSSFConditionalFormattingRule)cfRule; _cf.GetCfRuleArray(idx).Set(xRule.GetCTCfRule()); }