Skip to content

Commit

Permalink
Merge pull request #17 from skbkontur/deleteInvalidUris
Browse files Browse the repository at this point in the history
delete invalid URIs
  • Loading branch information
wasadus authored Nov 21, 2023
2 parents 36cf4ea + 4cacb02 commit 17021e6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public void TestSimpleWithEnumerable()
});
}

[Test]
public void TestInvalidUris()
{
var (model, mappingForErrors) = Parse<PriceList>("simpleWithEnumerable_template.xlsx", "simpleWithEnumerable_targetWithInvalidUri.xlsx");

mappingForErrors["Type"].Should().Be("C3");
mappingForErrors["Items[0].Id"].Should().Be("B13");
mappingForErrors["Items[0].Name"].Should().Be("C13");
mappingForErrors["Items[1].Id"].Should().Be("B14");
mappingForErrors["Items[1].Name"].Should().Be("C14");

model.Type.Should().Be("[email protected]>");
model.Items.Should().BeEquivalentTo(new[]
{
new Item {Id = "2311129000009", Name = "СЫР ГОЛЛАНДСКИЙ МОЖГА 1КГ"},
new Item {Id = "2311131000004", Name = "СЫР РОССИЙСКИЙ МОЖГА 1КГ"},
});
}

[Test]
public void TestLazyParse()
{
Expand Down
Binary file not shown.
3 changes: 1 addition & 2 deletions Excel.TemplateEngine/Excel.TemplateEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<ItemGroup>
<PackageReference Include="morelinq" Version="3.3.2" />
<PackageReference Include="C5" Version="2.5.3" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.19.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="System.IO.Packaging" Version="6.0.0" />
<PackageReference Include="Vostok.Logging.Abstractions" Version="1.0.30" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public ExcelDocumentStyle(Stylesheet stylesheet, Theme theme, ILog logger)
// Not using theme.ThemeElements.ColorScheme.Elements<Color2Type>() here because of wrong order.
colorSchemeElements = new List<Color2Type>
{
theme.ThemeElements.ColorScheme.Light1Color,
theme.ThemeElements.ColorScheme.Dark1Color,
theme.ThemeElements.ColorScheme.Light2Color,
theme.ThemeElements.ColorScheme.Dark2Color,
theme.ThemeElements.ColorScheme.Accent1Color,
theme.ThemeElements.ColorScheme.Accent2Color,
theme.ThemeElements.ColorScheme.Accent3Color,
theme.ThemeElements.ColorScheme.Accent4Color,
theme.ThemeElements.ColorScheme.Accent5Color,
theme.ThemeElements.ColorScheme.Accent6Color,
theme.ThemeElements.ColorScheme.Hyperlink,
theme.ThemeElements.ColorScheme.FollowedHyperlinkColor,
theme.ThemeElements?.ColorScheme?.Light1Color,
theme.ThemeElements?.ColorScheme?.Dark1Color,
theme.ThemeElements?.ColorScheme?.Light2Color,
theme.ThemeElements?.ColorScheme?.Dark2Color,
theme.ThemeElements?.ColorScheme?.Accent1Color,
theme.ThemeElements?.ColorScheme?.Accent2Color,
theme.ThemeElements?.ColorScheme?.Accent3Color,
theme.ThemeElements?.ColorScheme?.Accent4Color,
theme.ThemeElements?.ColorScheme?.Accent5Color,
theme.ThemeElements?.ColorScheme?.Accent6Color,
theme.ThemeElements?.ColorScheme?.Hyperlink,
theme.ThemeElements?.ColorScheme?.FollowedHyperlinkColor,
};
cache = new Dictionary<CellStyleCacheItem, uint>();
inverseCache = new Dictionary<uint, ExcelCellStyle>();
Expand All @@ -64,7 +64,7 @@ public uint AddStyle(ExcelCellStyle style)
NumberFormatId = numberFormatId,
Alignment = alignment
};
if (!cache.TryGetValue(cacheItem, out var result))
if (!cache.TryGetValue(cacheItem, out var result) && stylesheet.CellFormats != null)
{
result = stylesheet.CellFormats.Count;
stylesheet.CellFormats.Count++;
Expand All @@ -79,7 +79,7 @@ public ExcelCellStyle GetStyle(int styleIndex)
if (inverseCache.TryGetValue((uint)styleIndex, out var result))
return result;

var cellFormat = stylesheet?.CellFormats?.ChildElements?.Count > styleIndex ? (CellFormat)stylesheet.CellFormats.ChildElements[styleIndex] : null;
var cellFormat = stylesheet?.CellFormats?.ChildElements.Count > styleIndex ? (CellFormat)stylesheet.CellFormats.ChildElements[styleIndex] : null;
result = new ExcelCellStyle
{
FillStyle = cellFormat?.FillId == null ? null : GetCellFillStyle(cellFormat.FillId.Value),
Expand All @@ -105,37 +105,29 @@ private ExcelCellAlignment GetCellAlignment(Alignment alignment)

private ExcelVerticalAlignment ToExcelVerticalAlignment(EnumValue<VerticalAlignmentValues> vertical)
{
switch (vertical.Value)
{
case VerticalAlignmentValues.Bottom:
if (vertical.Value == VerticalAlignmentValues.Bottom)
return ExcelVerticalAlignment.Bottom;
case VerticalAlignmentValues.Center:
if (vertical.Value == VerticalAlignmentValues.Center)
return ExcelVerticalAlignment.Center;
case VerticalAlignmentValues.Top:
if (vertical.Value == VerticalAlignmentValues.Top)
return ExcelVerticalAlignment.Top;
default:
return ExcelVerticalAlignment.Default;
}
return ExcelVerticalAlignment.Default;
}

private ExcelHorizontalAlignment ToExcelHorizontalAlignment(EnumValue<HorizontalAlignmentValues> horizontal)
{
switch (horizontal.Value)
{
case HorizontalAlignmentValues.Center:
if (horizontal.Value == HorizontalAlignmentValues.Center)
return ExcelHorizontalAlignment.Center;
case HorizontalAlignmentValues.Left:
if (horizontal.Value == HorizontalAlignmentValues.Left)
return ExcelHorizontalAlignment.Left;
case HorizontalAlignmentValues.Right:
if (horizontal.Value == HorizontalAlignmentValues.Right)
return ExcelHorizontalAlignment.Right;
default:
return ExcelHorizontalAlignment.Default;
}
return ExcelHorizontalAlignment.Default;
}

private ExcelCellBordersStyle GetCellBordersStyle(uint borderId)
{
var bordersStyle = stylesheet?.Borders?.ChildElements?.Count > borderId ? (Border)stylesheet.Borders.ChildElements[(int)borderId] : null;
var bordersStyle = stylesheet?.Borders?.ChildElements.Count > borderId ? (Border)stylesheet.Borders.ChildElements[(int)borderId] : null;
return new ExcelCellBordersStyle
{
LeftBorder = bordersStyle?.LeftBorder == null ? null : GetBorderStyle(bordersStyle.LeftBorder),
Expand All @@ -156,21 +148,17 @@ private ExcelCellBorderStyle GetBorderStyle(BorderPropertiesType border)

private static ExcelBorderType ToExcelBorderType(EnumValue<BorderStyleValues> borderStyle)
{
switch (borderStyle.Value)
{
case BorderStyleValues.None:
if (borderStyle.Value == BorderStyleValues.None)
return ExcelBorderType.None;
case BorderStyleValues.Thin:
if (borderStyle.Value == BorderStyleValues.Thin)
return ExcelBorderType.Thin;
case BorderStyleValues.Medium:
if (borderStyle.Value == BorderStyleValues.Medium)
return ExcelBorderType.Single;
case BorderStyleValues.Thick:
if (borderStyle.Value == BorderStyleValues.Thick)
return ExcelBorderType.Bold;
case BorderStyleValues.Double:
if (borderStyle.Value == BorderStyleValues.Double)
return ExcelBorderType.Double;
default:
throw new InvalidOperationException($"Unknown border type: {borderStyle}");
}
throw new InvalidOperationException($"Unknown border type: {borderStyle}");
}

private ExcelCellNumberingFormat GetCellNumberingFormat(uint numberFormatId)
Expand All @@ -179,29 +167,29 @@ private ExcelCellNumberingFormat GetCellNumberingFormat(uint numberFormatId)
return new ExcelCellNumberingFormat(numberFormatId);

var numberFormat = (NumberingFormat)stylesheet?.NumberingFormats?.ChildElements
?.FirstOrDefault(ce => ((NumberingFormat)ce)?.NumberFormatId != null &&
((NumberingFormat)ce).NumberFormatId.Value == numberFormatId);
.FirstOrDefault(ce => ((NumberingFormat)ce)?.NumberFormatId != null &&
((NumberingFormat)ce).NumberFormatId!.Value == numberFormatId);
if (numberFormat?.FormatCode?.Value == null)
return null;

return new ExcelCellNumberingFormat(numberFormat.NumberFormatId.Value, numberFormat.FormatCode.Value);
return new ExcelCellNumberingFormat(numberFormat.NumberFormatId!.Value, numberFormat.FormatCode.Value);
}

private ExcelCellFontStyle GetCellFontStyle(uint fontId)
{
var internalFont = stylesheet?.Fonts?.ChildElements?.Count > fontId ? (Font)stylesheet.Fonts.ChildElements[(int)fontId] : null;
var internalFont = stylesheet?.Fonts?.ChildElements.Count > fontId ? (Font)stylesheet.Fonts.ChildElements[(int)fontId] : null;
return new ExcelCellFontStyle
{
Bold = internalFont?.Bold != null,
Size = internalFont?.FontSize == null ? (int?)null : Convert.ToInt32((object)internalFont.FontSize?.Val.Value),
Size = internalFont?.FontSize == null ? (int?)null : Convert.ToInt32((object)internalFont.FontSize?.Val?.Value),
Underlined = internalFont?.Underline != null,
Color = ToExcelColor(internalFont?.Color)
};
}

private ExcelCellFillStyle GetCellFillStyle(uint fillId)
{
var fill = stylesheet?.Fills?.ChildElements?.Count > fillId ? (Fill)stylesheet.Fills.ChildElements[(int)fillId] : null;
var fill = stylesheet?.Fills?.ChildElements.Count > fillId ? (Fill)stylesheet.Fills.ChildElements[(int)fillId] : null;
var color = ToExcelColor(fill?.PatternFill?.ForegroundColor);

if (color == null)
Expand All @@ -217,7 +205,7 @@ private ExcelColor ToExcelColor([CanBeNull] ColorType color)
return null;
if (color.Rgb?.HasValue == true)
{
return RgbStringToExcelColor(color.Rgb.Value);
return RgbStringToExcelColor(color.Rgb.Value!);
}
if (color.Theme?.HasValue == true)
{
Expand Down

0 comments on commit 17021e6

Please sign in to comment.