Skip to content

Commit

Permalink
parse multiple worksheets into one model object
Browse files Browse the repository at this point in the history
  • Loading branch information
Ярин Захар Владимирович committed Nov 20, 2024
1 parent 9c102a0 commit e8a0934
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 208 deletions.
72 changes: 62 additions & 10 deletions Excel.TemplateEngine.Tests/ObjectPrintingTests/ExcelParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,38 @@ public void TestInvalidUris()
});
}

[Test]
public void TestParseMultipleWorksheets()
{
var (model, mappingForErrors) = Parse<PriceList>(
"withMultipleWorksheets_template.xlsx",
"withMultipleWorksheets_target.xlsx",
workSheetNumber : 3);

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");
mappingForErrors["FromSecondWorksheet[0].Id"].Should().Be("Лист2:A2");
mappingForErrors["FromSecondWorksheet[0].Name"].Should().Be("Лист2:B2");
mappingForErrors["FromThirdWorksheet"].Should().Be("Лист3:B1");

model.Type.Should().Be("Основной");
model.Items.Should().BeEquivalentTo(new[]
{
new Item {Id = "2311129000009", Name = "СЫР ГОЛЛАНДСКИЙ МОЖГА 1КГ"},
new Item {Id = "2311131000004", Name = "СЫР РОССИЙСКИЙ МОЖГА 1КГ"},
});
model.FromSecondWorksheet.Should().BeEquivalentTo(new[]
{
new Item {Id = "2311129000009", Name = "FromSecondWorksheet1"},
new Item {Id = "2311131000004", Name = "FromSecondWorksheet2"},
new Item {Id = "2311131000004", Name = "FromSecondWorksheet3"},
});
model.FromThirdWorksheet.Should().Be("FromThirdWorksheet_Value");
}

[Test]
public void TestLazyParse()
{
Expand Down Expand Up @@ -356,28 +388,44 @@ private void CheckCustomProperty(IExcelDocument documentModel, string key, strin
printedVersion.Should().Be(value);
}

private (TModel model, Dictionary<string, string> mappingForErrors) Parse<TModel>(string templateFileName, string targetFileName)
private (TModel model, Dictionary<string, string> mappingForErrors) Parse<TModel>(string templateFileName, string targetFileName, int workSheetNumber = 0)
where TModel : new()
{
return Parse<TModel>(File.ReadAllBytes(GetFilePath(templateFileName)), File.ReadAllBytes(GetFilePath(targetFileName)));
return Parse<TModel>(File.ReadAllBytes(GetFilePath(templateFileName)), File.ReadAllBytes(GetFilePath(targetFileName)), workSheetNumber);
}

private (TModel model, Dictionary<string, string> mappingForErrors) Parse<TModel>(byte[] templateBytes, byte[] targetBytes)
private (TModel model, Dictionary<string, string> mappingForErrors) Parse<TModel>(byte[] templateBytes, byte[] targetBytes, int workSheetNumber = 0)
where TModel : new()
{
using (var templateDocument = ExcelDocumentFactory.CreateFromTemplate(templateBytes, logger))
using (var targetDocument = ExcelDocumentFactory.CreateFromTemplate(targetBytes, logger))
{
var template = new ExcelTable(templateDocument.GetWorksheet(0));
var templateEngine = new TemplateEngine(template, logger);

var target = new ExcelTable(targetDocument.GetWorksheet(0));
var tableNavigator = new TableNavigator(new CellPosition("A1"), logger);
var tableParser = new TableParser(target, tableNavigator);
return templateEngine.Parse<TModel>(tableParser);
var (baseTemplateEngine, baseTableParser) = GetEngineAndParserForWorksheet(templateDocument, targetDocument, 0);
var (model, mappingForErrors) = baseTemplateEngine.Parse<TModel>(baseTableParser);
for (var i = 1; i < workSheetNumber; i++)
{
var (templateEngine, tableParser) = GetEngineAndParserForWorksheet(templateDocument, targetDocument, i);
var worksheetName = targetDocument.GetWorksheetName(i);
templateEngine.Parse(tableParser, (name, value) => mappingForErrors.Add(name, $"{worksheetName}:{value}"), ref model);
}
return (model, mappingForErrors);
}
}

private (TemplateEngine templateEngine, TableParser tableParser) GetEngineAndParserForWorksheet(
IExcelDocument templateDocument,
IExcelDocument targetDocument,
int workSheetNumber)
{
var template = new ExcelTable(templateDocument.GetWorksheet(workSheetNumber));
var templateEngine = new TemplateEngine(template, logger);

var target = new ExcelTable(targetDocument.GetWorksheet(workSheetNumber));
var tableNavigator = new TableNavigator(new CellPosition("A1"), logger);
var tableParser = new TableParser(target, tableNavigator);
return (templateEngine, tableParser);
}

private TModel LazyParse<TModel>(string templateFileName, string targetFileName, ObjectSize offset = null)
where TModel : new()
{
Expand Down Expand Up @@ -451,6 +499,10 @@ class PriceList
public Dictionary<int, bool> IntBoolDict { get; set; }
public PriceList InnerPriceList { get; set; }
public Dictionary<string, PriceList> PriceListsDict { get; set; }

public Item[] FromSecondWorksheet { get; set; }

public string FromThirdWorksheet { get; set; }
}

#endregion
Expand Down
Binary file not shown.
Binary file not shown.
24 changes: 13 additions & 11 deletions Excel.TemplateEngine/ITemplateEngine.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
using System;
using System.Collections.Generic;

using JetBrains.Annotations;

using SkbKontur.Excel.TemplateEngine.ObjectPrinting.LazyParse;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.NavigationPrimitives.Implementations;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.TableBuilder;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.TableParser;

namespace SkbKontur.Excel.TemplateEngine
#nullable enable
namespace SkbKontur.Excel.TemplateEngine;

public interface ITemplateEngine
{
public interface ITemplateEngine
{
void Render<TModel>([NotNull] ITableBuilder tableBuilder, [NotNull] TModel model);
void Render<TModel>(ITableBuilder tableBuilder, TModel model) where TModel : notnull;

(TModel model, Dictionary<string, string> mappingForErrors) Parse<TModel>(ITableParser tableParser)
where TModel : new();

(TModel model, Dictionary<string, string> mappingForErrors) Parse<TModel>([NotNull] ITableParser tableParser)
where TModel : new();
void Parse<TModel>(ITableParser tableParser, Action<string, string> mappingForErrors, ref TModel model)
where TModel : new();

public TModel LazyParse<TModel>([NotNull] LazyTableReader lazyTableReader, ObjectSize readerOffset = null, IFormulaEvaluator formulaEvaluator = null)
where TModel : new();
}
public TModel LazyParse<TModel>(LazyTableReader lazyTableReader, ObjectSize? readerOffset = null, IFormulaEvaluator? formulaEvaluator = null)
where TModel : new();
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;

using JetBrains.Annotations;

using SkbKontur.Excel.TemplateEngine.ObjectPrinting.RenderingTemplates;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.TableParser;

namespace SkbKontur.Excel.TemplateEngine.ObjectPrinting.ParseCollection.Parsers
#nullable enable

namespace SkbKontur.Excel.TemplateEngine.ObjectPrinting.ParseCollection.Parsers;

internal interface IClassParser
{
internal interface IClassParser
{
[NotNull]
TModel Parse<TModel>([NotNull] ITableParser tableParser, [NotNull] RenderingTemplate template, [NotNull] Action<string, string> addFieldMapping)
where TModel : new();
}
TModel Parse<TModel>(ITableParser tableParser, RenderingTemplate template, Action<string, string> addFieldMapping)
where TModel : new();

void Parse<TModel>(ITableParser tableParser, RenderingTemplate template, Action<string, string> addFieldMapping, ref TModel model)
where TModel : new();
}
Loading

0 comments on commit e8a0934

Please sign in to comment.