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

Added search columns with similar names #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions src/LinqToExcel.Tests/ColumnMappings_IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ public void annotated_properties_map_to_columns()
Assert.AreEqual("N", rival.IsActive, "IsActive");
}

[Test]
public void annotated_properties_map_to_similar_columns()
{
var companies = from c in _repo.Worksheet<CompanyWithSimilarColumnsAnnotations>(_worksheetName)
where c.Name == "Taylor University"
select c;

var rival = companies.ToList().First();
Assert.AreEqual(1, companies.ToList().Count, "Result Count");
Assert.AreEqual("Taylor University", rival.Name, "Name");
Assert.AreEqual("Your Mom", rival.CEO, "CEO");
Assert.AreEqual(400, rival.EmployeeCount, "EmployeeCount");
Assert.AreEqual(new DateTime(1988, 7, 26), rival.StartDate, "StartDate");
Assert.AreEqual("N", rival.IsActive, "IsActive");
}

[Test]
public void two_transformations_with_the_same_property_but_in_different_types()
{
Expand Down
23 changes: 23 additions & 0 deletions src/LinqToExcel.Tests/CompanyWithSimilarColumnsAnnotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using LinqToExcel.Attributes;

namespace LinqToExcel.Tests
{
public class CompanyWithSimilarColumnsAnnotations
{
[ExcelColumn(new[] { "Company Title", "Company", "Business Title" })]
public string Name { get; set; }

[ExcelColumn(new[] { "Boss", "Head", "Executive" })]
public string CEO { get; set; }

[ExcelColumn(new[] { "Number People", "People Count" })]
public int EmployeeCount { get; set; }

[ExcelColumn(new[] { "Initiation Date", "Init Date" })]
public DateTime StartDate { get; set; }

[ExcelColumn(new[] { "Active", "Is Active" })]
public string IsActive { get; set; }
}
}
33 changes: 32 additions & 1 deletion src/LinqToExcel/Attributes/ExcelColumnAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace LinqToExcel.Attributes
{
Expand All @@ -7,15 +11,42 @@ public sealed class ExcelColumnAttribute : Attribute
{
private readonly string _columnName;

public ExcelColumnAttribute(string columnName)
private readonly string[] _similarColumnNames;
private readonly bool _forceToSimilar;

private readonly Regex pattern = new Regex(@"(?:( +[d|n](o|e|a|os|as))|( +(?:a(?:nd?)?|the|to|[io]n|from|with|of|for))) +| +|_+|-+|'+|`+");

public ExcelColumnAttribute(string columnName, string[] similarColumnNames = null, bool forceToSimilar = false)
{
_columnName = columnName;
_similarColumnNames = similarColumnNames;
_forceToSimilar = forceToSimilar;
}

public ExcelColumnAttribute(string[] similarColumnNames, bool forceToSimilar = true): this(null, similarColumnNames, forceToSimilar) { }

public string ColumnName
{
get { return _columnName; }
}

public bool IsForced
{
get { return _forceToSimilar; }
}

public bool HasSimilarColumn(string columnName)
{
return !string.IsNullOrEmpty(_similarColumnNames?.ToList().Find(x => Clear(x) == Clear(columnName)));
}

private string Clear(string s)
{
s = new string(s.Normalize(NormalizationForm.FormD)
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) != UnicodeCategory.NonSpacingMark)
.ToArray());

return pattern.Replace(s.Trim().ToLower(), "");
}
}
}
2 changes: 1 addition & 1 deletion src/LinqToExcel/LinqToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<ItemGroup>
<PackageReference Include="Remotion.Linq" Version="2.2.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/LinqToExcel/Query/ExcelQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ internal ExcelQueryable(ExcelQueryArgs args, ILogManagerFactory logManagerFactor
ExcelColumnAttribute att = (ExcelColumnAttribute)Attribute.GetCustomAttribute(property, typeof(ExcelColumnAttribute));
if (att != null && !args.ColumnMappings.ContainsKey(property.Name))
{
args.ColumnMappings.Add(property.Name, att.ColumnName);
var columnNames = ExcelUtilities.GetColumnNames(args);

args.ColumnMappings.Add(property.Name, !att.IsForced ?
att.ColumnName :
columnNames.ToList().Find(x => att.HasSimilarColumn(x))??att.ColumnName);
}
}
}
Expand Down