Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add horizontal alignment "fill" to cell #18

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="Vostok.Logging.Console" Version="1.0.9" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,16 @@ public void CellBorderFormatExtractionTest()
style.BordersStyle.BottomBorder.Color.Blue.Should().Be(255);
}

[Test]
public void CellAlignmentExtractionTest()
[TestCase(ExcelHorizontalAlignment.Left, "A1")]
[TestCase(ExcelHorizontalAlignment.Fill, "B1")]
public void CellAlignmentExtractionTest(ExcelHorizontalAlignment horizontalAlignment, string cellIndex)
{
var templateDocument = ExcelDocumentFactory.CreateFromTemplate(File.ReadAllBytes(GetFilePath("template.xlsx")), logger);
var cell = templateDocument.GetWorksheet(0).GetCell(new ExcelCellIndex("A1"));
var cell = templateDocument.GetWorksheet(0).GetCell(new ExcelCellIndex(cellIndex));
var style = cell.GetStyle();

style.Alignment.WrapText.Should().BeTrue();
style.Alignment.HorizontalAlignment.Should().Be(ExcelHorizontalAlignment.Left);
style.Alignment.HorizontalAlignment.Should().Be(horizontalAlignment);
}

[Test]
Expand Down
Binary file modified Excel.TemplateEngine.Tests/FileGeneratingTests/Files/template.xlsx
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void PrintComplexObjectTest()
},
DeliveryParty = new Organization
{
Address = "DeliveryPartyAddress",
Address = "DeliveryPartyAddress,DeliveryPartyAddress,DeliveryPartyAddress",
Name = "DeliveryPartyName"
},
Vehicle = new VehicleInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,81 @@

using SkbKontur.Excel.TemplateEngine.FileGenerating.DataTypes;

namespace SkbKontur.Excel.TemplateEngine.FileGenerating.Caches.CacheItems
namespace SkbKontur.Excel.TemplateEngine.FileGenerating.Caches.CacheItems;

internal class AlignmentCacheItem : IEquatable<AlignmentCacheItem>
{
internal class AlignmentCacheItem : IEquatable<AlignmentCacheItem>
public AlignmentCacheItem(ExcelCellAlignment cellAlignment)
{
public AlignmentCacheItem(ExcelCellAlignment cellAlignment)
{
verticalAlignment = cellAlignment.VerticalAlignment;
horizontalAlignment = cellAlignment.HorizontalAlignment;
wrapText = cellAlignment.WrapText;
}
verticalAlignment = cellAlignment.VerticalAlignment;
horizontalAlignment = cellAlignment.HorizontalAlignment;
wrapText = cellAlignment.WrapText;
}

public bool Equals(AlignmentCacheItem other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return verticalAlignment == other.verticalAlignment && horizontalAlignment == other.horizontalAlignment && wrapText.Equals(other.wrapText);
}
public bool Equals(AlignmentCacheItem other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return verticalAlignment == other.verticalAlignment && horizontalAlignment == other.horizontalAlignment && wrapText.Equals(other.wrapText);
}

public Alignment ToAlignment()
{
return new Alignment
{
Horizontal = GetHorizontalAlignment(),
Vertical = GetVerticalAlignment(),
WrapText = wrapText ? new BooleanValue(true) : null
};
}
public Alignment ToAlignment()
{
return new Alignment
{
Horizontal = GetHorizontalAlignment(),
Vertical = GetVerticalAlignment(),
WrapText = wrapText ? new BooleanValue(true) : null
};
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((AlignmentCacheItem)obj);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((AlignmentCacheItem)obj);
}

public override int GetHashCode()
public override int GetHashCode()
{
unchecked
{
unchecked
{
var hashCode = (int)verticalAlignment;
hashCode = (hashCode * 397) ^ (int)horizontalAlignment;
hashCode = (hashCode * 397) ^ wrapText.GetHashCode();
return hashCode;
}
var hashCode = (int)verticalAlignment;
hashCode = (hashCode * 397) ^ (int)horizontalAlignment;
hashCode = (hashCode * 397) ^ wrapText.GetHashCode();
return hashCode;
}
}

private EnumValue<VerticalAlignmentValues> GetVerticalAlignment()
{
EnumValue<VerticalAlignmentValues> result;
switch (verticalAlignment)
private EnumValue<VerticalAlignmentValues> GetVerticalAlignment()
{
EnumValue<VerticalAlignmentValues> result = verticalAlignment switch
{
case ExcelVerticalAlignment.Top:
result = new EnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Top);
break;
case ExcelVerticalAlignment.Center:
result = new EnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Center);
break;
case ExcelVerticalAlignment.Bottom:
result = new EnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Bottom);
break;
case ExcelVerticalAlignment.Default:
result = null;
break;
default:
throw new InvalidOperationException($"Unknown vertical alignment: {verticalAlignment}");
}
return result;
}
ExcelVerticalAlignment.Top => new EnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Top),
ExcelVerticalAlignment.Center => new EnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Center),
ExcelVerticalAlignment.Bottom => new EnumValue<VerticalAlignmentValues>(VerticalAlignmentValues.Bottom),
ExcelVerticalAlignment.Default => null,
_ => throw new InvalidOperationException($"Unknown vertical alignment: {verticalAlignment}")
};
return result;
}

private EnumValue<HorizontalAlignmentValues> GetHorizontalAlignment()
{
EnumValue<HorizontalAlignmentValues> result;
switch (horizontalAlignment)
private EnumValue<HorizontalAlignmentValues> GetHorizontalAlignment()
{
EnumValue<HorizontalAlignmentValues> result = horizontalAlignment switch
{
case ExcelHorizontalAlignment.Left:
result = new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Left);
break;
case ExcelHorizontalAlignment.Center:
result = new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Center);
break;
case ExcelHorizontalAlignment.Right:
result = new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Right);
break;
case ExcelHorizontalAlignment.Default:
result = null;
break;
default:
throw new InvalidOperationException($"Unknown horizontal alignment: {horizontalAlignment}");
}
return result;
}

private readonly ExcelVerticalAlignment verticalAlignment;
private readonly ExcelHorizontalAlignment horizontalAlignment;
private readonly bool wrapText;
ExcelHorizontalAlignment.Left => new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Left),
ExcelHorizontalAlignment.Center => new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Center),
ExcelHorizontalAlignment.Right => new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Right),
ExcelHorizontalAlignment.Fill => new EnumValue<HorizontalAlignmentValues>(HorizontalAlignmentValues.Fill),
ExcelHorizontalAlignment.Default => null,
_ => throw new InvalidOperationException($"Unknown horizontal alignment: {horizontalAlignment}")
};
return result;
}

private readonly ExcelVerticalAlignment verticalAlignment;
private readonly ExcelHorizontalAlignment horizontalAlignment;
private readonly bool wrapText;
}
Loading
Loading