Skip to content

Commit fecba85

Browse files
authored
Removed check for double negative sign in custom format. Replaced by … (#2270)
* Removed check for double negative sign in custom format. Replaced by Math.Abs when formatting the string. * Added more tests * added comment * Reverted to using regexp * Added some additional checks for custom format.
1 parent d533f64 commit fecba85

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/EPPlus/Utils/String/ValueToTextHandler.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ internal static string FormatValue(object v, bool forWidthCalc, ExcelFormatTrans
7676
if (v == null) v = 0;
7777
var f = nf.GetFormatPart(v);
7878
isValidFormat = f.IsValid;
79-
if(isValidFormat == false)
79+
if (isValidFormat == false)
8080
{
8181
return null;
8282
}
@@ -117,7 +117,7 @@ internal static string FormatValue(object v, bool forWidthCalc, ExcelFormatTrans
117117
}
118118
else
119119
{
120-
return FormatNumber(d, format, overrideCultureInfo ?? nf.Culture);
120+
return FormatNumber(d, format, overrideCultureInfo ?? nf.Culture, nf.Formats.Count > 1);
121121
}
122122
}
123123
else
@@ -215,22 +215,23 @@ internal static string FormatValue(object v, bool forWidthCalc, ExcelFormatTrans
215215
return v.ToString();
216216
}
217217

218-
private static string FormatNumber(double d, string format, CultureInfo cultureInfo)
218+
private static string FormatNumber(double d, string format, CultureInfo cultureInfo, bool isNegativeFormat)
219219
{
220220
var s = FormatNumberExcel(d, format, cultureInfo);
221221
var ns = cultureInfo?.NumberFormat?.NegativeSign ?? "-";
222222
if (string.IsNullOrEmpty(s) == false && d < 0)
223223
{
224-
return CheckAndRemoveNegativeSign(format, s, ns);
224+
return CheckAndRemoveNegativeSign(format, s, ns, isNegativeFormat);
225225
}
226226
else
227227
{
228228
return s;
229229
}
230230
}
231231

232-
private static string CheckAndRemoveNegativeSign(string format, string s, string ns)
232+
private static string CheckAndRemoveNegativeSign(string format, string s, string ns, bool isNegativeFormat)
233233
{
234+
//We should not use regexp, but it will do for now.
234235
//This regex pattern ^(\[[^\]]+\]|[\\'\""\*_])* removes:
235236
// \[[^\]]+\] : This part removes Anything inside square brackets
236237
// [\\'\""\*_] : This part removes Backslashes, quotes, asterisks and underlines
@@ -244,6 +245,10 @@ private static string CheckAndRemoveNegativeSign(string format, string s, string
244245
{
245246
return s.Substring(1);
246247
}
248+
else if ((s.StartsWith($"{ns}") || s.StartsWith($"-")) && !formatStartsWithNegative && isNegativeFormat)
249+
{
250+
return s.Remove(0, 1);
251+
}
247252
else
248253
{
249254
return s;

src/EPPlusTest/Issues/WorksheetIssues.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using OfficeOpenXml.FormulaParsing;
77
using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
88
using OfficeOpenXml.FormulaParsing.Excel.Functions.MathFunctions;
9+
using OfficeOpenXml.RichData;
910
using OfficeOpenXml.SystemDrawing.Image;
1011
using OfficeOpenXml.SystemDrawing.Text;
1112
using System;
@@ -1085,10 +1086,30 @@ public void i2258()
10851086
{
10861087
var p = OpenTemplatePackage("repro2.xlsx");
10871088
var ws = p.Workbook.Worksheets.First();
1089+
10881090
var a1 = ws.Cells["A1"].Text;
1089-
var b1 = ws.Cells["B1"].Text;
10901091
Assert.AreEqual("-/- 1 000", a1);
1092+
1093+
var b1 = ws.Cells["B1"].Text;
10911094
Assert.AreEqual("-/- 2 000", b1);
1095+
1096+
var c1 = ws.Cells["C1"].Text;
1097+
Assert.AreEqual("3 000,00", c1);
1098+
1099+
var d1 = ws.Cells["D1"].Text;
1100+
Assert.AreEqual("(4000,000)", d1);
1101+
1102+
var a3 = ws.Cells["A3"].Text;
1103+
Assert.AreEqual("1000,000", a3);
1104+
1105+
var b3 = ws.Cells["B3"].Text;
1106+
Assert.AreEqual("(2000,000)", b3);
1107+
1108+
var c3 = ws.Cells["C3"].Text;
1109+
Assert.AreEqual("-3000,000", c3);
1110+
1111+
var d3 = ws.Cells["D3"].Text;
1112+
Assert.AreEqual("Negative 4000,000", d3);
10921113
}
10931114

10941115
}

0 commit comments

Comments
 (0)