/// <summary>
        /// Add rule (internal)
        /// </summary>
        /// <param name="type">The <see cref="eExcelConditionalFormattingRuleType"/> of the rule to add.</param>
        /// <param name="address">The <see cref="ExcelAddress"/> of the rule to add.</param>
        /// <returns>Returns the rule added to the collection.</returns>
        internal IExcelConditionalFormattingRule AddRule(eExcelConditionalFormattingRuleType type, ExcelAddress address)
        {
            Require.Argument(address).IsNotNull("address");
            address = ValidateAddress(address);
            EnsureRootElementExists();
            IExcelConditionalFormattingRule cfRule = ExcelConditionalFormattingRuleFactory.Create(
                type,
                address,
                GetNextPriority(),
                this.ExcelWorksheet,
                null);

            this.ConditionalFormattingRules.Add(cfRule);
            return(cfRule);
        }
Пример #2
0
        /****************************************************************************************/

        #region Conditional Formatting Rules
        /// <summary>
        /// Add rule (internal)
        /// </summary>
        /// <param name="type"></param>
        /// <param name="address"></param>
        /// <returns></returns>F
        internal IExcelConditionalFormattingRule AddRule(
            eExcelConditionalFormattingRuleType type,
            ExcelAddress address)
        {
            Require.Argument(address).IsNotNull("address");

            address = ValidateAddress(address);
            EnsureRootElementExists();

            // Create the Rule according to the correct type, address and priority
            IExcelConditionalFormattingRule cfRule = ExcelConditionalFormattingRuleFactory.Create(
                type,
                address,
                GetNextPriority(),
                _worksheet,
                null);

            // Add the newly created rule to the list
            _rules.Add(cfRule);

            // Return the newly created rule
            return(cfRule);
        }
Пример #3
0
        /****************************************************************************************/

        #region Constructors
        /// <summary>
        /// Initialize the <see cref="ExcelConditionalFormattingCollection"/>
        /// </summary>
        /// <param name="worksheet"></param>
        internal ExcelConditionalFormattingCollection(
            ExcelWorksheet worksheet)
            : base(
                worksheet.NameSpaceManager,
                worksheet.WorksheetXml.DocumentElement)
        {
            Require.Argument(worksheet).IsNotNull("worksheet");

            _worksheet      = worksheet;
            SchemaNodeOrder = _worksheet.SchemaNodeOrder;

            // Look for all the <conditionalFormatting>
            var conditionalFormattingNodes = TopNode.SelectNodes(
                "//" + ExcelConditionalFormattingConstants.Paths.ConditionalFormatting,
                _worksheet.NameSpaceManager);

            // Check if we found at least 1 node
            if ((conditionalFormattingNodes != null) &&
                (conditionalFormattingNodes.Count > 0))
            {
                // Foreach <conditionalFormatting>
                foreach (XmlNode conditionalFormattingNode in conditionalFormattingNodes)
                {
                    // Check if @sqref attribute exists
                    if (conditionalFormattingNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Sqref] == null)
                    {
                        throw new Exception(
                                  ExcelConditionalFormattingConstants.Errors.MissingSqrefAttribute);
                    }

                    // Get the @sqref attribute
                    ExcelAddress address = new ExcelAddress(
                        conditionalFormattingNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Sqref].Value);

                    // Check for all the <cfRules> nodes and load them
                    var cfRuleNodes = conditionalFormattingNode.SelectNodes(
                        ExcelConditionalFormattingConstants.Paths.CfRule,
                        _worksheet.NameSpaceManager);

                    // Checking the count of cfRuleNodes "materializes" the collection which prevents a rare infinite loop bug
                    if (cfRuleNodes.Count == 0)
                    {
                        continue;
                    }

                    // Foreach <cfRule> inside the current <conditionalFormatting>
                    foreach (XmlNode cfRuleNode in cfRuleNodes)
                    {
                        // Check if @type attribute exists
                        if (cfRuleNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Type] == null)
                        {
                            throw new Exception(
                                      ExcelConditionalFormattingConstants.Errors.MissingTypeAttribute);
                        }

                        // Check if @priority attribute exists
                        if (cfRuleNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Priority] == null)
                        {
                            throw new Exception(
                                      ExcelConditionalFormattingConstants.Errors.MissingPriorityAttribute);
                        }

                        // Get the <cfRule> main attributes
                        string typeAttribute = ExcelConditionalFormattingHelper.GetAttributeString(
                            cfRuleNode,
                            ExcelConditionalFormattingConstants.Attributes.Type);

                        int priority = ExcelConditionalFormattingHelper.GetAttributeInt(
                            cfRuleNode,
                            ExcelConditionalFormattingConstants.Attributes.Priority);

                        // Transform the @type attribute to EPPlus Rule Type (slighty diferente)
                        var type = ExcelConditionalFormattingRuleType.GetTypeByAttrbiute(
                            typeAttribute,
                            cfRuleNode,
                            _worksheet.NameSpaceManager);

                        // Create the Rule according to the correct type, address and priority
                        var cfRule = ExcelConditionalFormattingRuleFactory.Create(
                            type,
                            address,
                            priority,
                            _worksheet,
                            cfRuleNode);

                        // Add the new rule to the list
                        if (cfRule != null)
                        {
                            _rules.Add(cfRule);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Initialize the <see cref="ExcelConditionalFormattingCollection"/>
        /// </summary>
        /// <param name="worksheet">The worksheet from which to construct the ConditionalFormattings.</param>
        internal ExcelConditionalFormattingCollection(
            ExcelWorksheet worksheet)
            : base(
                worksheet.NameSpaceManager,
                worksheet.WorksheetXml.DocumentElement)
        {
            Require.Argument(worksheet).IsNotNull("worksheet");

            this.ExcelWorksheet             = worksheet;
            this.SchemaNodeOrder            = this.ExcelWorksheet.SchemaNodeOrder;
            this.ConditionalFormattingRules = new List <IExcelConditionalFormattingRule>();

            // Look for all the <conditionalFormatting> nodes.
            var conditionalFormattingNodes = this.TopNode.SelectNodes(
                "//" + ExcelConditionalFormattingConstants.Paths.ConditionalFormatting,
                this.ExcelWorksheet.NameSpaceManager);

            if ((conditionalFormattingNodes != null) && (conditionalFormattingNodes.Count > 0))
            {
                foreach (XmlNode conditionalFormattingNode in conditionalFormattingNodes)
                {
                    // Try to get the @sqref attribute. If it is missing, do not add a cf rule for this node.
                    string sqref = conditionalFormattingNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Sqref]?.Value;
                    if (string.IsNullOrEmpty(sqref))
                    {
                        continue;
                    }
                    ExcelAddress address = new ExcelAddress(sqref);

                    // Check for all the <cfRules> nodes and load them.
                    var cfRuleNodes = conditionalFormattingNode.SelectNodes(
                        ExcelConditionalFormattingConstants.Paths.CfRule,
                        this.ExcelWorksheet.NameSpaceManager);

                    // Foreach <cfRule> inside the current <conditionalFormatting>.
                    foreach (XmlNode cfRuleNode in cfRuleNodes)
                    {
                        // Check if @type attribute exists.
                        if (cfRuleNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Type] == null)
                        {
                            throw new Exception(ExcelConditionalFormattingConstants.Errors.MissingTypeAttribute);
                        }

                        // Check if @priority attribute exists.
                        if (cfRuleNode.Attributes[ExcelConditionalFormattingConstants.Attributes.Priority] == null)
                        {
                            throw new Exception(ExcelConditionalFormattingConstants.Errors.MissingPriorityAttribute);
                        }

                        // Get the <cfRule> main attributes.
                        string typeAttribute = ExcelConditionalFormattingHelper.GetAttributeString(
                            cfRuleNode,
                            ExcelConditionalFormattingConstants.Attributes.Type);

                        int priority = ExcelConditionalFormattingHelper.GetAttributeInt(
                            cfRuleNode,
                            ExcelConditionalFormattingConstants.Attributes.Priority);

                        // Transform the @type attribute to EPPlus Rule Type (slighty different).
                        var type = ExcelConditionalFormattingRuleType.GetTypeByAttrbiute(
                            typeAttribute,
                            cfRuleNode,
                            this.ExcelWorksheet.NameSpaceManager);

                        var cfRule = ExcelConditionalFormattingRuleFactory.Create(
                            type,
                            address,
                            priority,
                            this.ExcelWorksheet,
                            cfRuleNode);

                        if (cfRule != null)
                        {
                            this.ConditionalFormattingRules.Add(cfRule);
                        }
                    }
                }
            }
        }