Skip to content

Commit 8c3fe47

Browse files
authored
fixed checking for negative sign when using custom number format. (#2259)
1 parent 2d1089a commit 8c3fe47

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/EPPlus/ExcelRangeBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ public string Text
816816
{
817817
get
818818
{
819-
820819
object value;
821820
if (IsSingleCell || IsName)
822821
{

src/EPPlus/Utils/String/ValueToTextHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,12 @@ private static string FormatNumber(double d, string format, CultureInfo cultureI
231231

232232
private static string CheckAndRemoveNegativeSign(string format, string s, string ns)
233233
{
234-
if ((s.StartsWith($"{ns}{ns}") || s.StartsWith($"{ns}-")) && (format.StartsWith(ns) || format.StartsWith("-")))
234+
//This regex pattern ^(\[[^\]]+\]|[\\'\""\*_])* removes:
235+
// \[[^\]]+\] : This part removes Anything inside square brackets
236+
// [\\'\""\*_] : This part removes Backslashes, quotes, asterisks and underlines
237+
string trimmedFormat = System.Text.RegularExpressions.Regex.Replace(format, @"^(\[[^\]]+\]|[\\'\""\*_])*", "");
238+
bool formatStartsWithNegative = trimmedFormat.StartsWith(ns) || trimmedFormat.StartsWith("-");
239+
if ((s.StartsWith($"{ns}{ns}") || s.StartsWith($"{ns}-")) && formatStartsWithNegative)
235240
{
236241
return s.Remove(1, 1);
237242
}

src/EPPlusTest/Issues/WorksheetIssues.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,5 +1079,17 @@ public void testRepro()
10791079
}
10801080
SwitchBackToCurrentCulture();
10811081
}
1082+
1083+
[TestMethod]
1084+
public void i2258()
1085+
{
1086+
var p = OpenTemplatePackage("repro2.xlsx");
1087+
var ws = p.Workbook.Worksheets.First();
1088+
var a1 = ws.Cells["A1"].Text;
1089+
var b1 = ws.Cells["B1"].Text;
1090+
Assert.AreEqual("-/- 1 000", a1);
1091+
Assert.AreEqual("-/- 2 000", b1);
1092+
}
1093+
10821094
}
10831095
}

0 commit comments

Comments
 (0)