Skip to content

Commit

Permalink
Fix bug : remove ExcelNumberFormat nuget to resolve error NU3037
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Nov 15, 2021
1 parent 9a05683 commit b1f9acb
Show file tree
Hide file tree
Showing 27 changed files with 2,284 additions and 6 deletions.
11 changes: 9 additions & 2 deletions MiniExcel.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniExcelLibs", "src\MiniExcel\MiniExcelLibs.csproj", "{097903C9-1F81-4427-B4C8-530CB59687B8}"
EndProject
Expand Down Expand Up @@ -34,6 +34,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniExcel.Benchmarks", "ben
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{77A8A169-168B-457F-AB5F-48F30D6BB33C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExcelNumberFormat", "src\ExcelNumberFormat\ExcelNumberFormat.csproj", "{7ECB1CFF-955F-4285-8486-756F1CDE2177}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -52,6 +54,10 @@ Global
{1888285A-B130-48D5-A628-DC85B623FAB8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1888285A-B130-48D5-A628-DC85B623FAB8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1888285A-B130-48D5-A628-DC85B623FAB8}.Release|Any CPU.Build.0 = Release|Any CPU
{7ECB1CFF-955F-4285-8486-756F1CDE2177}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7ECB1CFF-955F-4285-8486-756F1CDE2177}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7ECB1CFF-955F-4285-8486-756F1CDE2177}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7ECB1CFF-955F-4285-8486-756F1CDE2177}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -60,6 +66,7 @@ Global
{097903C9-1F81-4427-B4C8-530CB59687B8} = {CC1E0601-AEC9-42D7-8F6A-3FB3939EED16}
{77F2C86B-0F17-4370-AB38-A089F9DF4ED5} = {359A7094-3353-48F2-B3E1-FE9E59698318}
{1888285A-B130-48D5-A628-DC85B623FAB8} = {77A8A169-168B-457F-AB5F-48F30D6BB33C}
{7ECB1CFF-955F-4285-8486-756F1CDE2177} = {CC1E0601-AEC9-42D7-8F6A-3FB3939EED16}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {51DF25DA-2DCB-4883-90FE-399DA950D4F2}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH

### Reference

[ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader) / [ClosedXML](https://github.com/ClosedXML/ClosedXML) / [Dapper](https://github.com/DapperLib/Dapper)
[ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader) / [ClosedXML](https://github.com/ClosedXML/ClosedXML) / [Dapper](https://github.com/DapperLib/Dapper) / [ExcelNumberFormat](https://github.com/andersnm/ExcelNumberFormat)



Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH

### 参考

[ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader) / [ClosedXML](https://github.com/ClosedXML/ClosedXML) / [Dapper](https://github.com/DapperLib/Dapper)
[ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader) / [ClosedXML](https://github.com/ClosedXML/ClosedXML) / [Dapper](https://github.com/DapperLib/Dapper) / [ExcelNumberFormat](https://github.com/andersnm/ExcelNumberFormat)



Expand Down
2 changes: 1 addition & 1 deletion README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ public static DataTable QueryAsDataTableWithoutEmptyRow(Stream stream, bool useH

### 參考

[ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader) / [ClosedXML](https://github.com/ClosedXML/ClosedXML) / [Dapper](https://github.com/DapperLib/Dapper)
[ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader) / [ClosedXML](https://github.com/ClosedXML/ClosedXML) / [Dapper](https://github.com/DapperLib/Dapper) / [ExcelNumberFormat](https://github.com/andersnm/ExcelNumberFormat)



Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

### 0.19.1
- [New] QueryAsDataTable remove empty column keys. #298
- [Bug] Error NU3037: ExcelNumberFormat 1.1.0 #302

### 0.19.0
- [New] SaveAs default style with autoFilter mode. #190
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

### 0.19.1
- [New] QueryAsDataTable 删除空白 Column keys. #298
- [Bug] Error NU3037: ExcelNumberFormat 1.1.0 #302

### 0.19.0
- [New] SaveAs 预设样式增加筛选功能. #190
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

### 0.19.1
- [New] QueryAsDataTable 刪除空白 Column keys. #298
- [Bug] Error NU3037: ExcelNumberFormat 1.1.0 #302

### 0.19.0
- [New] SaveAs 預設樣式增加篩選功能. #190
Expand Down
3 changes: 3 additions & 0 deletions src/ExcelNumberFormat/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly:CLSCompliant(true)]
7 changes: 7 additions & 0 deletions src/ExcelNumberFormat/Color.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ExcelNumberFormat
{
internal class Color
{
public string Value { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/ExcelNumberFormat/CompatibleConvert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace ExcelNumberFormat
{
internal static class CompatibleConvert
{
/// <summary>
/// A backward-compatible version of <see cref="Convert.ToString(object, IFormatProvider)"/>.
/// Starting from .net Core 3.0 the default precision used for formatting floating point number has changed.
/// To always format numbers the same way, no matter what version of runtime is used, we specify the precision explicitly.
/// </summary>
public static string ToString(object value, IFormatProvider provider)
{
switch (value)
{
case double d:
return d.ToString("G15", provider);
case float f:
return f.ToString("G7", provider);
default:
return Convert.ToString(value, provider);
}
}
}
}
29 changes: 29 additions & 0 deletions src/ExcelNumberFormat/Condition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace ExcelNumberFormat
{
internal class Condition
{
public string Operator { get; set; }
public double Value { get; set; }

public bool Evaluate(double lhs)
{
switch (Operator)
{
case "<":
return lhs < Value;
case "<=":
return lhs <= Value;
case ">":
return lhs > Value;
case ">=":
return lhs >= Value;
case "<>":
return lhs != Value;
case "=":
return lhs == Value;
}

return false;
}
}
}
102 changes: 102 additions & 0 deletions src/ExcelNumberFormat/DecimalSection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;

namespace ExcelNumberFormat
{
internal class DecimalSection
{
public bool ThousandSeparator { get; set; }

public double ThousandDivisor { get; set; }

public double PercentMultiplier { get; set; }

public List<string> BeforeDecimal { get; set; }

public bool DecimalSeparator { get; set; }

public List<string> AfterDecimal { get; set; }

public static bool TryParse(List<string> tokens, out DecimalSection format)
{
if (Parser.ParseNumberTokens(tokens, 0, out var beforeDecimal, out var decimalSeparator, out var afterDecimal) == tokens.Count)
{
bool thousandSeparator;
var divisor = GetTrailingCommasDivisor(tokens, out thousandSeparator);
var multiplier = GetPercentMultiplier(tokens);

format = new DecimalSection()
{
BeforeDecimal = beforeDecimal,
DecimalSeparator = decimalSeparator,
AfterDecimal = afterDecimal,
PercentMultiplier = multiplier,
ThousandDivisor = divisor,
ThousandSeparator = thousandSeparator
};

return true;
}

format = null;
return false;
}

static double GetPercentMultiplier(List<string> tokens)
{
// If there is a percentage literal in the part list, multiply the result by 100
foreach (var token in tokens)
{
if (token == "%")
return 100;
}

return 1;
}

static double GetTrailingCommasDivisor(List<string> tokens, out bool thousandSeparator)
{
// This parses all comma literals in the part list:
// Each comma after the last digit placeholder divides the result by 1000.
// If there are any other commas, display the result with thousand separators.
bool hasLastPlaceholder = false;
var divisor = 1.0;

for (var j = 0; j < tokens.Count; j++)
{
var tokenIndex = tokens.Count - 1 - j;
var token = tokens[tokenIndex];

if (!hasLastPlaceholder)
{
if (Token.IsPlaceholder(token))
{
// Each trailing comma multiplies the divisor by 1000
for (var k = tokenIndex + 1; k < tokens.Count; k++)
{
token = tokens[k];
if (token == ",")
divisor *= 1000.0;
else
break;
}

// Continue scanning backwards from the last digit placeholder,
// but now look for a thousand separator comma
hasLastPlaceholder = true;
}
}
else
{
if (token == ",")
{
thousandSeparator = true;
return divisor;
}
}
}

thousandSeparator = false;
return divisor;
}
}
}
110 changes: 110 additions & 0 deletions src/ExcelNumberFormat/Evaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ExcelNumberFormat
{
internal static class Evaluator
{
public static Section GetSection(List<Section> sections, object value)
{
// Standard format has up to 4 sections:
// Positive;Negative;Zero;Text
switch (value)
{
case string s:
if (sections.Count >= 4)
return sections[3];

return null;

case DateTime dt:
// TODO: Check date conditions need date helpers and Date1904 knowledge
return GetFirstSection(sections, SectionType.Date);

case TimeSpan ts:
return GetNumericSection(sections, ts.TotalDays);

case double d:
return GetNumericSection(sections, d);

case int i:
return GetNumericSection(sections, i);

case short s:
return GetNumericSection(sections, s);

default:
return null;
}
}

public static Section GetFirstSection(List<Section> sections, SectionType type)
{
foreach (var section in sections)
if (section.Type == type)
return section;
return null;
}

private static Section GetNumericSection(List<Section> sections, double value)
{
// First section applies if
// - Has a condition:
// - There is 1 section, or
// - There are 2 sections, and the value is 0 or positive, or
// - There are >2 sections, and the value is positive
if (sections.Count < 1)
{
return null;
}

var section0 = sections[0];

if (section0.Condition != null)
{
if (section0.Condition.Evaluate(value))
{
return section0;
}
}
else if (sections.Count == 1 || (sections.Count == 2 && value >= 0) || (sections.Count >= 2 && value > 0))
{
return section0;
}

if (sections.Count < 2)
{
return null;
}

var section1 = sections[1];

// First condition didnt match, or was a negative number. Second condition applies if:
// - Has a condition, or
// - Value is negative, or
// - There are two sections, and the first section had a non-matching condition
if (section1.Condition != null)
{
if (section1.Condition.Evaluate(value))
{
return section1;
}
}
else if (value < 0 || (sections.Count == 2 && section0.Condition != null))
{
return section1;
}

// Second condition didnt match, or was positive. The following
// sections cannot have conditions, always fall back to the third
// section (for zero formatting) if specified.
if (sections.Count < 3)
{
return null;
}

return sections[2];
}
}
}
Loading

0 comments on commit b1f9acb

Please sign in to comment.