示例#1
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);
        }
示例#2
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);

                    // 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);
                        }
                    }
                }
            }
        }