Files
QWERTYkez.OpenXmlProcessors/QWERTYkez.WordProcessor/Builders/CellProps.cs
2026-06-05 15:58:03 +07:00

89 lines
3.5 KiB
C#

namespace QWERTYkez.WordProcessor.Builders;
public readonly struct CellProps(double? width = null!)
{
public double? Width { get; init; } = width;
public TableVerticalAlignmentValues? VerticalAlignment { get; init; } = default;
public MergedCell? Merge { get; init; } = default;
#pragma warning disable IDE1006 // Стили именования
public static CellProps V_H_ { get; } = new() { Merge = MergedCell.V_H_ };
public static CellProps H_ { get; } = new() { Merge = MergedCell.H_ };
public static CellProps V_ { get; } = new() { Merge = MergedCell.V_ };
public static CellProps _V_H { get; } = new() { Merge = MergedCell._V_H };
public static CellProps _H { get; } = new() { Merge = MergedCell._H };
public static CellProps _V { get; } = new() { Merge = MergedCell._V };
public static CellProps _VH_ { get; } = new() { Merge = MergedCell._VH_ };
public static CellProps V__H { get; } = new() { Merge = MergedCell.V__H };
#pragma warning restore IDE1006 // Стили именования
internal bool TryExtract(out List<OpenXmlElement> list)
{
list =
[
new TableCellVerticalAlignment() { Val = VerticalAlignment ?? TableVerticalAlignmentValues.Center },
new TableCellMargin()
{
TopMargin = new TopMargin() { Width = "0" },
BottomMargin = new BottomMargin() { Width = "0" },
LeftMargin = new LeftMargin() { Width = "0" },
RightMargin = new RightMargin() { Width = "0" }
}
];
if (Width.HasValue)
list.Add(new TableCellWidth()
{
Type = TableWidthUnitValues.Dxa,
Width = ((uint)(Width.Value * 567d)).ToString() // 1 см = 567 DXA
});
if (Merge.HasValue)
{
switch (Merge.Value)
{
case MergedCell.V_H_:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Restart });
list.Add(new VerticalMerge() { Val = MergedCellValues.Restart });
break;
case MergedCell.H_:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Restart }); break;
case MergedCell.V_:
list.Add(new VerticalMerge() { Val = MergedCellValues.Restart }); break;
case MergedCell._V_H:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Continue });
list.Add(new VerticalMerge() { Val = MergedCellValues.Continue });
break;
case MergedCell._H:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Continue }); break;
case MergedCell._V:
list.Add(new VerticalMerge() { Val = MergedCellValues.Continue }); break;
case MergedCell._VH_:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Restart });
list.Add(new VerticalMerge() { Val = MergedCellValues.Continue });
break;
case MergedCell.V__H:
list.Add(new HorizontalMerge() { Val = MergedCellValues.Continue });
list.Add(new VerticalMerge() { Val = MergedCellValues.Restart });
break;
}
}
return list.Count > 0;
}
}
public enum MergedCell
{
V_H_,
H_,
V_,
_V_H,
_H,
_V,
_VH_,
V__H,
}