-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug : remove ExcelNumberFormat nuget to resolve error NU3037
- Loading branch information
1 parent
9a05683
commit b1f9acb
Showing
27 changed files
with
2,284 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System; | ||
|
||
[assembly:CLSCompliant(true)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ExcelNumberFormat | ||
{ | ||
internal class Color | ||
{ | ||
public string Value { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} |
Oops, something went wrong.