Example #1
0
        public IXLNamedRange Add(String rangeName, IXLRanges ranges, String comment)
        {
            var namedRange = new XLNamedRange(this, rangeName, ranges, comment);

            _namedRanges.Add(rangeName, namedRange);
            return(namedRange);
        }
Example #2
0
        /// <summary>
        /// Adds the specified range name.
        /// </summary>
        /// <param name="rangeName">Name of the range.</param>
        /// <param name="rangeAddress">The range address.</param>
        /// <param name="comment">The comment.</param>
        /// <param name="acceptInvalidReferences">if set to <c>true</c> range address will not be checked for validity. Necessary when loading files as is.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">For named ranges in the workbook scope, specify the sheet name in the reference.</exception>
        internal IXLNamedRange Add(String rangeName, String rangeAddress, String comment, bool acceptInvalidReferences)
        {
            if (!acceptInvalidReferences)
            {
                var match = XLHelper.NamedRangeReferenceRegex.Match(rangeAddress);

                if (!match.Success)
                {
                    var range = Worksheet?.Range(rangeAddress) ?? Workbook.Range(rangeAddress);
                    if (range == null)
                    {
                        throw new ArgumentException(string.Format("The range address '{0}' for the named range '{1}' is not a valid range.", rangeAddress, rangeName));
                    }
                    else
                    {
                        if (Worksheet == null || !XLHelper.NamedRangeReferenceRegex.Match(range.ToString()).Success)
                        {
                            throw new ArgumentException("For named ranges in the workbook scope, specify the sheet name in the reference.");
                        }
                        else
                        {
                            rangeAddress = Worksheet.Range(rangeAddress).ToString();
                        }
                    }
                }
            }

            var namedRange = new XLNamedRange(this, rangeName, rangeAddress, comment);

            _namedRanges.Add(rangeName, namedRange);
            return(namedRange);
        }
Example #3
0
        /// <summary>
        /// Adds the specified range name.
        /// </summary>
        /// <param name="rangeName">Name of the range.</param>
        /// <param name="rangeAddress">The range address.</param>
        /// <param name="comment">The comment.</param>
        /// <param name="validateName">if set to <c>true</c> validates the name.</param>
        /// <param name="validateRangeAddress">if set to <c>true</c> range address will be checked for validity.</param>
        /// <returns></returns>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="ArgumentException">
        /// For named ranges in the workbook scope, specify the sheet name in the reference.
        /// </exception>
        internal IXLNamedRange Add(String rangeName, String rangeAddress, String comment, Boolean validateName, Boolean validateRangeAddress)
        {
            // When loading named ranges from an existing file, we do not validate the range address or name.
            if (validateRangeAddress)
            {
                var match = XLHelper.NamedRangeReferenceRegex.Match(rangeAddress);

                if (!match.Success)
                {
                    if (XLHelper.IsValidRangeAddress(rangeAddress))
                    {
                        IXLRange range = null;
                        if (Scope == XLNamedRangeScope.Worksheet)
                        {
                            range = Worksheet.Range(rangeAddress);
                        }
                        else if (Scope == XLNamedRangeScope.Workbook)
                        {
                            range = Workbook.Range(rangeAddress);
                        }
                        else
                        {
                            throw new NotSupportedException($"Scope {Scope} is not supported");
                        }

                        if (range == null)
                        {
                            throw new ArgumentException(string.Format(
                                                            "The range address '{0}' for the named range '{1}' is not a valid range.", rangeAddress,
                                                            rangeName));
                        }

                        if (Scope == XLNamedRangeScope.Workbook || !XLHelper.NamedRangeReferenceRegex.Match(range.ToString()).Success)
                        {
                            throw new ArgumentException(
                                      "For named ranges in the workbook scope, specify the sheet name in the reference.");
                        }

                        rangeAddress = Worksheet.Range(rangeAddress).ToString();
                    }
                }
            }

            var namedRange = new XLNamedRange(this, rangeName, validateName, rangeAddress, comment);

            _namedRanges.Add(rangeName, namedRange);
            return(namedRange);
        }
 public IXLNamedRange Add(String rangeName, IXLRanges ranges, String comment)
 {
     var namedRange = new XLNamedRange(this, rangeName, ranges, comment);
     _namedRanges.Add(rangeName, namedRange);
     return namedRange;
 }