Skip to content

Commit

Permalink
Bug/i1102 (#1104)
Browse files Browse the repository at this point in the history
* Fix substitute returning strings when actually an error

* Changed from throw to return. Moved issue test to appropriate location
  • Loading branch information
OssianEPPlus authored Oct 17, 2023
1 parent ff89383 commit 8bb7743
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/EPPlus/FormulaParsing/Excel/Functions/Text/Substitute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Date Author Change
using System.Linq;
using System.Text;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Metadata;
using OfficeOpenXml.FormulaParsing.Exceptions;
using OfficeOpenXml.FormulaParsing.ExpressionGraph;

namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Text
Expand All @@ -28,6 +29,15 @@ internal class Substitute : ExcelFunction
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 3);

foreach (var arg in arguments)
{
if (arg.ValueIsExcelError)
{
return new CompileResult(arg.ValueAsExcelErrorValue, DataType.ExcelError);
}
}

var text = ArgToString(arguments, 0);
var find = ArgToString(arguments, 1);
var replaceWith = ArgToString(arguments, 2);
Expand Down
20 changes: 20 additions & 0 deletions src/EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5398,5 +5398,25 @@ void DebugGetFontInfo()
SaveAndCleanup(package);
}
}
[TestMethod]
public void i1102()
{
using (var excel = new ExcelPackage())
{
var worksheet = excel.Workbook.Worksheets.Add("Test Worksheet");
var targetCell = worksheet.Cells[1, 1];
var validationCell = worksheet.Cells[1, 2];

validationCell.Formula = "IFERROR(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND(\"-\",A1)),\"-\",\"|\"),\"0\")";

targetCell.Value = "invalid";

validationCell.Calculate();

var result = validationCell.GetValue<string>();

Assert.AreEqual("0", result);
}
}
}
}

0 comments on commit 8bb7743

Please sign in to comment.