diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/ExcelParsingTests.cs b/Excel.TemplateEngine.Tests/ObjectPrintingTests/ExcelParsingTests.cs index d033ce6..97cdec9 100644 --- a/Excel.TemplateEngine.Tests/ObjectPrintingTests/ExcelParsingTests.cs +++ b/Excel.TemplateEngine.Tests/ObjectPrintingTests/ExcelParsingTests.cs @@ -65,6 +65,25 @@ public void TestSimpleWithEnumerable() }); } + [Test] + public void TestInvalidUris() + { + var (model, mappingForErrors) = Parse("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@email.email>"); + model.Items.Should().BeEquivalentTo(new[] + { + new Item {Id = "2311129000009", Name = "СЫР ГОЛЛАНДСКИЙ МОЖГА 1КГ"}, + new Item {Id = "2311131000004", Name = "СЫР РОССИЙСКИЙ МОЖГА 1КГ"}, + }); + } + [Test] public void TestLazyParse() { diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/simpleWithEnumerable_targetWithInvalidUri.xlsx b/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/simpleWithEnumerable_targetWithInvalidUri.xlsx new file mode 100644 index 0000000..9115874 Binary files /dev/null and b/Excel.TemplateEngine.Tests/ObjectPrintingTests/Files/simpleWithEnumerable_targetWithInvalidUri.xlsx differ diff --git a/Excel.TemplateEngine/Excel.TemplateEngine.csproj b/Excel.TemplateEngine/Excel.TemplateEngine.csproj index f5265ff..912f9bb 100644 --- a/Excel.TemplateEngine/Excel.TemplateEngine.csproj +++ b/Excel.TemplateEngine/Excel.TemplateEngine.csproj @@ -10,9 +10,8 @@ - + - diff --git a/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs b/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs index ce8efed..b34c499 100644 --- a/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs +++ b/Excel.TemplateEngine/FileGenerating/Caches/Implementations/ExcelDocumentStyle.cs @@ -32,18 +32,18 @@ public ExcelDocumentStyle(Stylesheet stylesheet, Theme theme, ILog logger) // Not using theme.ThemeElements.ColorScheme.Elements() here because of wrong order. colorSchemeElements = new List { - 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(); inverseCache = new Dictionary(); @@ -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++; @@ -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), @@ -105,37 +105,29 @@ private ExcelCellAlignment GetCellAlignment(Alignment alignment) private ExcelVerticalAlignment ToExcelVerticalAlignment(EnumValue 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 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), @@ -156,21 +148,17 @@ private ExcelCellBorderStyle GetBorderStyle(BorderPropertiesType border) private static ExcelBorderType ToExcelBorderType(EnumValue 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) @@ -179,21 +167,21 @@ 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) }; @@ -201,7 +189,7 @@ private ExcelCellFontStyle GetCellFontStyle(uint fontId) 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) @@ -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) {