CellAddress implement IXLAddress

This commit is contained in:
ngfrolov 2022-06-24 11:03:13 +05:00
parent 2712eb43ef
commit 0c488f3b6b

View File

@ -1,25 +1,43 @@
using System;
using ClosedXML.Excel;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace AsbCloudInfrastructure.Services.DailyReport
{
internal class CellAddress
internal class CellAddress: IXLAddress
{
const int excelLettersCount = 'Z' - 'A';
public int Row { get; set; }
public int Colunm { get; set; }
const int excelLettersCount = 'Z' - 'A' + 1;
public int RowNumber { get; set; }
public int ColumnNumber { get; set; }
public string ColumnLetter
{
get { return CalcColumnLetter(); }
}
public bool FixedColumn { get; set; }
public bool FixedRow { get; set; }
public string UniqueId => ToString(XLReferenceStyle.A1, true);
public IXLWorksheet Worksheet { get; set; }
public CellAddress(int row, int colunm)
{
Row = row;
Colunm = colunm;
RowNumber = row;
ColumnNumber = colunm;
}
public static CellAddress operator + (CellAddress a, CellAddress b)
=> new CellAddress(a.Row + b.Row, a.Colunm + b.Colunm);
=> new CellAddress(a.RowNumber + b.RowNumber, a.ColumnNumber + b.ColumnNumber);
public static CellAddress operator +(CellAddress a, (int row, int column) b)
=> new CellAddress(a.RowNumber + b.row, a.ColumnNumber + b.column);
public static CellAddress operator - (CellAddress a, CellAddress b)
=> new CellAddress(a.Row - b.Row, a.Colunm - b.Colunm);
=> new CellAddress(a.RowNumber - b.RowNumber, a.ColumnNumber - b.ColumnNumber);
public static bool operator == (CellAddress a, CellAddress b)
=> a.Row == b.Row && a.Colunm == b.Colunm;
=> a.RowNumber == b.RowNumber && a.ColumnNumber == b.ColumnNumber;
public static bool operator !=(CellAddress a, CellAddress b)
=> !(a == b);
@ -54,20 +72,108 @@ namespace AsbCloudInfrastructure.Services.DailyReport
return true;
}
public string ToStringA1()
private string CalcColumnLetter()
{
string letter = "";
while (Colunm > 0)
while (ColumnNumber > 0)
{
int modulo = (Colunm - 1) % excelLettersCount;
int modulo = (ColumnNumber - 1) % excelLettersCount;
letter = Convert.ToChar('A' + modulo) + letter;
Colunm = (Colunm - modulo) / excelLettersCount;
ColumnNumber = (ColumnNumber - modulo) / excelLettersCount;
}
return letter + Row;
return letter;
}
public CellAddress Copy()
=> new CellAddress(this.RowNumber, this.ColumnNumber);
public string ToString(XLReferenceStyle referenceStyle)
=> ToString(referenceStyle, false);
public string ToString(XLReferenceStyle referenceStyle, bool includeSheet)
{
if (referenceStyle == XLReferenceStyle.R1C1)
throw new NotImplementedException("R1C1 - style doesn't implemented");
var sb = new StringBuilder();
if (includeSheet && Worksheet is not null)
sb.Append('$')
.Append(Worksheet.Name)
.Append('.');
if (FixedColumn)
sb.Append('$');
sb.Append(ColumnLetter);
if (FixedRow)
sb.Append('$');
sb.Append(RowNumber);
return sb.ToString();
}
public string ToStringFixed()
=> ToStringFixed(XLReferenceStyle.A1);
public string ToStringFixed(XLReferenceStyle referenceStyle)
=> ToStringFixed(referenceStyle, false);
public string ToStringFixed(XLReferenceStyle referenceStyle, bool includeSheet)
{
if (referenceStyle == XLReferenceStyle.R1C1)
throw new NotImplementedException("R1C1 - style doesn't implemented");
var sb = new StringBuilder();
if (includeSheet && Worksheet is not null)
sb.Append('$')
.Append(Worksheet.Name)
.Append('.');
sb.Append('$');
sb.Append(ColumnLetter);
sb.Append('$');
sb.Append(RowNumber);
return sb.ToString();
}
public string ToStringRelative()
=> ToStringRelative(false);
public string ToStringRelative(bool includeSheet)
{
var sb = new StringBuilder();
if (includeSheet && Worksheet is not null)
sb.Append('$')
.Append(Worksheet.Name)
.Append('.');
sb.Append(ColumnLetter);
sb.Append(RowNumber);
return sb.ToString();
}
public bool Equals(IXLAddress x, IXLAddress y)
=> x.ColumnNumber == y.ColumnNumber &&
x.RowNumber == y.RowNumber &&
x.FixedColumn == y.FixedColumn &&
x.FixedRow == y.FixedRow &&
x.Worksheet == y.Worksheet;
public override int GetHashCode()
=> base.GetHashCode();
public int GetHashCode([DisallowNull] IXLAddress obj)
=> obj.GetHashCode();
public bool Equals(IXLAddress other)
=> Equals(this, other);
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
@ -85,8 +191,5 @@ namespace AsbCloudInfrastructure.Services.DailyReport
return false;
}
public override int GetHashCode()
=> base.GetHashCode();
}
}