Class RangeCollection

RangeCollection class

Encapsulates a collection of Range objects.

public class RangeCollection : CollectionBase<Range>

Properties

NameDescription
Capacity { get; set; }
Count { get; }
Item { get; }Gets the Range element at the specified index.
Item { get; set; }

Methods

NameDescription
Add(Range)Adds a Range item to the collection.
BinarySearch(Range)
BinarySearch(Range, IComparer<Range>)
BinarySearch(int, int, Range, IComparer<Range>)
Clear()
Contains(Range)
CopyTo(Range[])
CopyTo(Range[], int)
CopyTo(int, Range[], int, int)
Exists(Predicate<Range>)
Find(Predicate<Range>)
FindAll(Predicate<Range>)
FindIndex(Predicate<Range>)
FindIndex(int, Predicate<Range>)
FindIndex(int, int, Predicate<Range>)
FindLast(Predicate<Range>)
FindLastIndex(Predicate<Range>)
FindLastIndex(int, Predicate<Range>)
FindLastIndex(int, int, Predicate<Range>)
GetEnumerator()
IndexOf(Range)
IndexOf(Range, int)
IndexOf(Range, int, int)
LastIndexOf(Range)
LastIndexOf(Range, int)
LastIndexOf(Range, int, int)
RemoveAt(int)

Examples

using System;
using Aspose.Cells;

namespace AsposeCellsExamples
{
    public class CellsClassRangeCollectionDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Create ranges and add them to the worksheet
            Aspose.Cells.Range range1 = worksheet.Cells.CreateRange("A1:B2");
            Aspose.Cells.Range range2 = worksheet.Cells.CreateRange("C3:D4");
            
            // Get the RangeCollection from the worksheet
            RangeCollection rangeCollection = worksheet.Cells.Ranges;
            
            // Add ranges to the collection
            rangeCollection.Add(range1);
            rangeCollection.Add(range2);

            // Create another RangeCollection for comparison
            RangeCollection compareCollection = worksheet.Cells.Ranges;
            compareCollection.Add(worksheet.Cells.CreateRange("A1:B2"));
            compareCollection.Add(worksheet.Cells.CreateRange("C3:D4"));

            // Compare the two RangeCollections
            bool areEqual = CompareRangeCollections(rangeCollection, compareCollection);
            Console.WriteLine("RangeCollections are equal: " + areEqual);
        }

        private static bool CompareRangeCollections(RangeCollection src, RangeCollection dest)
        {
            if (src.Count != dest.Count)
                return false;

            for (int i = 0; i < src.Count; i++)
            {
                bool foundMatch = false;
                Aspose.Cells.Range srcRange = src[i];
                
                foreach (Aspose.Cells.Range destRange in dest)
                {
                    if (srcRange.FirstRow == destRange.FirstRow &&
                        srcRange.FirstColumn == destRange.FirstColumn &&
                        srcRange.RowCount == destRange.RowCount &&
                        srcRange.ColumnCount == destRange.ColumnCount)
                    {
                        foundMatch = true;
                        break;
                    }
                }
                
                if (!foundMatch)
                    return false;
            }
            
            return true;
        }
    }
}

See Also