Files

153 lines
4.3 KiB
C#
Raw Permalink Normal View History

2026-06-19 15:06:40 +07:00
namespace QWERTYkez.ExcelProcessor;
/// <summary>
/// Неизменяемый набор всех параметров оформления ячейки.
/// </summary>
public sealed record CellStyle
{
public CellAlign? Align { get; init; }
public CellFont? Font { get; init; }
public CellFill? Fill { get; init; }
public CellBorder? Border { get; init; }
public NumberFormatPattern? NumberFormat { get; init; }
public bool IsEmpty() =>
Align == null && Font == null && Fill == null && Border == null && NumberFormat == null;
internal bool TryMerge(NumberFormatPattern format, out CellStyle result)
{
result = this;
if (format is not null)
{
if (!Equals(NumberFormat, format))
{
result = result with { NumberFormat = format };
return true;
}
}
return false;
}
internal bool TryMerge(CellAlign align, out CellStyle result)
{
result = this;
var current = Align ?? new CellAlign();
if (current.TryMerge(align, out align))
{
result = result with { Align = align };
return true;
}
return false;
}
internal bool TryMerge(CellBorder border, out CellStyle result)
{
result = this;
var current = Border ?? new CellBorder();
if (current.TryMerge(border, out border))
{
result = result with { Border = border };
return true;
}
return false;
}
internal bool TryMerge(CellFill fill, out CellStyle result)
{
result = this;
var current = Fill ?? new CellFill();
if (!current.Equals(fill))
{
result = result with { Fill = fill };
return true;
}
return false;
}
internal bool TryMerge(CellFont font, out CellStyle result)
{
result = this;
var current = Font ?? new CellFont();
if (!current.Equals(font))
{
result = result with { Font = font };
return true;
}
return false;
}
internal CellStyle Merge(CellStyle other)
{
if (other == null) return this;
return new CellStyle
{
Align = other.Align ?? Align,
Font = other.Font ?? Font,
Fill = other.Fill ?? Fill,
Border = other.Border ?? Border,
NumberFormat = other.NumberFormat ?? NumberFormat
};
}
internal bool TryMerge(CellStyle other, out CellStyle result)
{
result = this;
bool changed = false;
// Объединяем Align, если есть
if (other.Align is not null)
{
var currentAlign = Align ?? new CellAlign();
if (currentAlign.TryMerge(other.Align.Value, out var newAlign))
{
result = result with { Align = newAlign };
changed = true;
}
}
// Аналогично для Font
if (other.Font is not null)
{
var currentFont = Font ?? new CellFont();
if (currentFont.TryMerge(other.Font.Value, out var newFont))
{
result = result with { Font = newFont };
changed = true;
}
}
// Для Fill просто заменяем, если он задан (нет nullable-полей)
if (other.Fill is not null)
{
if (!Fill.Equals(other.Fill))
{
result = result with { Fill = other.Fill };
changed = true;
}
}
// Border
if (other.Border is not null)
{
var currentBorder = Border ?? new CellBorder();
if (currentBorder.TryMerge(other.Border.Value, out var newBorder))
{
result = result with { Border = newBorder };
changed = true;
}
}
// NumberFormat
if (other.NumberFormat is not null)
{
if (!Equals(NumberFormat, other.NumberFormat))
{
result = result with { NumberFormat = other.NumberFormat };
changed = true;
}
}
return changed;
}
}