UnionRange.GetEnumerator
UnionRange.GetEnumerator method
Gets the enumerator for cells in this Range.
public IEnumerator GetEnumerator()
Return Value
The cells enumerator
Remarks
When traversing elements by the returned Enumerator, the cells collection should not be modified(such as operations that will cause new Cell/Row be instantiated or existing Cell/Row be deleted). Otherwise the enumerator may not be able to traverse all cells correctly(some elements may be traversed repeatedly or skipped).
Examples
namespace AsposeCellsExamples
{
using Aspose.Cells;
using System;
using System.Collections;
public class UnionRangeMethodGetEnumeratorDemo
{
public static void Run()
{
// Create a new workbook and get the first worksheet
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Create the first range (A1:B2) and fill it with data
Aspose.Cells.Range range1 = worksheet.Cells.CreateRange("A1", "B2");
range1[0, 0].PutValue("R1C1");
range1[0, 1].PutValue("R1C2");
range1[1, 0].PutValue("R2C1");
range1[1, 1].PutValue("R2C2");
// Create the second range (C1:D2) and fill it with data
Aspose.Cells.Range range2 = worksheet.Cells.CreateRange("C1", "D2");
range2[0, 0].PutValue("R1C3");
range2[0, 1].PutValue("R1C4");
range2[1, 0].PutValue("R2C3");
range2[1, 1].PutValue("R2C4");
try
{
// Build a UnionRange from the first range
UnionRange unionRange = worksheet.Cells
.CreateRange("A1:B2")
.UnionRanges(new Aspose.Cells.Range[] { range1 });
// Add the second range to the union
unionRange = unionRange.Union("C1:D2");
// Obtain the enumerator and iterate through all cells in the union
IEnumerator enumerator = unionRange.GetEnumerator();
Console.WriteLine("Enumerating cells in the UnionRange:");
while (enumerator.MoveNext())
{
Cell cell = enumerator.Current as Cell;
if (cell != null)
{
Console.WriteLine($"{cell.Name}: {cell.Value}");
}
}
// Save the workbook (optional, just to show the data persists)
workbook.Save("UnionRangeGetEnumeratorDemo.xlsx");
Console.WriteLine("Workbook saved successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error calling GetEnumerator: {ex.Message}");
}
}
}
}
See Also
- class UnionRange
- namespace Aspose.Cells
- assembly Aspose.Cells