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");
            }
            XSSFConditionalFormattingRule[] hfRules;
            if (cfRules is XSSFConditionalFormattingRule[])
            {
                hfRules = (XSSFConditionalFormattingRule[])cfRules;
            }
            else
            {
                hfRules = new XSSFConditionalFormattingRule[cfRules.Length];
                Array.Copy(cfRules, 0, hfRules, 0, hfRules.Length);
            }

            CellRangeAddress[]       mergeCellRanges = CellRangeUtil.MergeCellRanges(regions);
            CT_ConditionalFormatting cf   = _sheet.GetCTWorksheet().AddNewConditionalFormatting();
            List <String>            refs = new List <String>();

            foreach (CellRangeAddress a in mergeCellRanges)
            {
                refs.Add(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);
        }
Esempio n. 2
0
        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");
            }

            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);
        }
        public int AddConditionalFormatting(CellRangeAddress[] regions, IConditionalFormattingRule[] cfRules)
        {
            if (regions == null)
            {
                throw new ArgumentException("regions must not be null");
            }
            foreach (CellRangeAddressBase region in regions)
            {
                region.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");
            }
            if (!(cfRules is XSSFConditionalFormattingRule[]))
            {
                XSSFConditionalFormattingRule[] conditionalFormattingRuleArray = new XSSFConditionalFormattingRule[cfRules.Length];
                Array.Copy((Array)cfRules, 0, (Array)conditionalFormattingRuleArray, 0, conditionalFormattingRuleArray.Length);
            }
            CellRangeAddress[]       cellRangeAddressArray  = CellRangeUtil.MergeCellRanges(regions);
            CT_ConditionalFormatting conditionalFormatting1 = this._sheet.GetCTWorksheet().AddNewConditionalFormatting();
            List <string>            stringList             = new List <string>();

            foreach (CellRangeAddress cellRangeAddress in cellRangeAddressArray)
            {
                stringList.Add(cellRangeAddress.FormatAsString());
            }
            conditionalFormatting1.sqref = stringList;
            int num = 1;

            foreach (CT_ConditionalFormatting conditionalFormatting2 in this._sheet.GetCTWorksheet().conditionalFormatting)
            {
                num += conditionalFormatting2.sizeOfCfRuleArray();
            }
            foreach (XSSFConditionalFormattingRule cfRule in cfRules)
            {
                cfRule.GetCTCfRule().priority = num++;
                conditionalFormatting1.AddNewCfRule().Set(cfRule.GetCTCfRule());
            }
            return(this._sheet.GetCTWorksheet().SizeOfConditionalFormattingArray() - 1);
        }
 /**
  * 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());
 }