NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
Range.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9
10namespace NanoXLSX
11{
15 public struct Range
16 {
17 private readonly Address startAddress;
18 private readonly Address endAddress;
19
23 public Address EndAddress { get => endAddress; }
27 public Address StartAddress { get => startAddress; }
28
35 public Range(Address start, Address end)
36 {
37 if (start.CompareTo(end) < 0)
38 {
39 this.startAddress = start;
40 this.endAddress = end;
41 }
42 else
43 {
44 this.startAddress = end;
45 this.endAddress = start;
46 }
47 }
48
57 public Range(int startColumn, int startRow, int endColumn, int endRow) : this(new Address(startColumn, startRow), new Address(endColumn, endRow))
58 {
59 }
60
61
67 public Range(string range)
68 {
69 Range r = Cell.ResolveCellRange(range);
71 {
72 this.startAddress = r.StartAddress;
73 this.endAddress = r.EndAddress;
74 }
75 else
76 {
77 this.startAddress = r.EndAddress;
78 this.endAddress = r.StartAddress;
79 }
80 }
81
87 public bool Contains(Range other)
88 {
89 return this.StartAddress.Column <= other.StartAddress.Column &&
90 this.EndAddress.Column >= other.EndAddress.Column &&
91 this.StartAddress.Row <= other.StartAddress.Row &&
92 this.EndAddress.Row >= other.EndAddress.Row;
93 }
94
100 public bool Contains(Address address)
101 {
102 return address.Column >= this.startAddress.Column &&
103 address.Column <= this.endAddress.Column &&
104 address.Row >= this.startAddress.Row &&
105 address.Row <= this.endAddress.Row;
106 }
107
113 public bool Overlaps(Range other)
114 {
115 return !(this.EndAddress.Row < other.StartAddress.Row || this.StartAddress.Row > other.EndAddress.Row ||
116 this.EndAddress.Column < other.StartAddress.Column || this.StartAddress.Column > other.EndAddress.Column);
117 }
118
119
125 public IReadOnlyList<Address> ResolveEnclosedAddresses()
126 {
127 IEnumerable<Address> range = Cell.GetCellRange(this.StartAddress, this.EndAddress);
128 return new List<Address>(range);
129 }
130
131
136 public override string ToString()
137 {
138 return StartAddress + ":" + EndAddress;
139 }
140
146 public override bool Equals(object obj)
147 {
148 if (!(obj is Range))
149 {
150 return false;
151 }
152 Range other = (Range)obj;
153 return this.StartAddress.Equals(other.StartAddress) && this.EndAddress.Equals(other.EndAddress);
154 }
155
160 public override int GetHashCode()
161 {
162 return this.ToString().GetHashCode();
163 }
164
165 // Operator overloads
166
167 // Operator overloads
174 public static bool operator ==(Range range1, Range range2)
175 {
176 return range1.Equals(range2);
177 }
178
185 public static bool operator !=(Range range1, Range range2)
186 {
187 return !range1.Equals(range2);
188 }
189
190
195 internal Range Copy()
196 {
197 return new Range(this.StartAddress.Copy(), this.EndAddress.Copy());
198 }
199
200 }
201
202}
Class representing a cell of a worksheet.
Definition Cell.cs:24
static Range ResolveCellRange(string range)
Resolves a cell range from the format like A1:B3 or AAD556:AAD1000.
Definition Cell.cs:816
static IEnumerable< Address > GetCellRange(string range)
Gets a list of cell addresses from a cell range (format A1:B3 or AAD556:AAD1000).
Definition Cell.cs:628
Struct representing the cell address as column and row (zero based).
Definition Address.cs:16
int CompareTo(Address other)
Compares two addresses using the column and row numbers.
Definition Address.cs:174
int Row
Row number (zero based).
Definition Address.cs:28
bool Equals(Address other)
Compares two addresses whether they are equal.
Definition Address.cs:112
int Column
Column number (zero based).
Definition Address.cs:24
Struct representing a cell range with a start and end address.
Definition Range.cs:16
Range(string range)
Constructor with a range string as argument. The addresses are automatically swapped if the start add...
Definition Range.cs:67
Range(int startColumn, int startRow, int endColumn, int endRow)
Constructor with start and end rows and columns as arguments. The addresses are automatically swapped...
Definition Range.cs:57
bool Contains(Range other)
Gets whether another range is completely enclosed by this range.
Definition Range.cs:87
override string ToString()
Overwritten ToString method.
Definition Range.cs:136
IReadOnlyList< Address > ResolveEnclosedAddresses()
Gets a list of all addresses between the start and end address.
Definition Range.cs:125
Range(Address start, Address end)
Constructor with addresses as arguments. The addresses are automatically swapped if the start address...
Definition Range.cs:35
static bool operator==(Range range1, Range range2)
Compares two objects whether they are ranges and equal. The cell types (possible $ prefix) are consid...
Definition Range.cs:174
Address StartAddress
Start address of the range.
Definition Range.cs:27
static bool operator!=(Range range1, Range range2)
Compares two objects whether they not equal. This method reflects the inverted method of Equals(objec...
Definition Range.cs:185
bool Contains(Address address)
Determines whether an address is within this range.
Definition Range.cs:100
override int GetHashCode()
Gets the hash code of the range object according to its string representation.
Definition Range.cs:160
Address EndAddress
End address of the range.
Definition Range.cs:23
override bool Equals(object obj)
Compares two objects whether they are ranges and equal. The cell types (possible $ prefix) are consid...
Definition Range.cs:146
bool Overlaps(Range other)
Determines whether the passed range overlaps with this range.
Definition Range.cs:113