Quantcast
Channel: NPOI
Viewing all articles
Browse latest Browse all 1621

Created Unassigned: Custom formulas registered in static class [13641]

$
0
0
I tryed to register my custom function in workbook and it has some public properties. It seems that this registration is global (I think for the caching reason) and when I create the second workbook, register this custom function in it. it calls the first one custom function instead of second one. See the test below if my English is bad :)

```
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Formula.Udf;
using NPOI.SS.UserModel;
using NUnit.Framework;

namespace Poi.Tests
{
[TestFixture]
public class CustomFormulaTests
{
class TestFormula : FreeRefFunction
{
public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
{
return new NumberEval(TestValue);
}

public int TestValue { get; set; }
}
[Test]
public void TestCustomFormula()
{
var testValue = 1;
var cell = CreateCellWithCustomTestFormula(testValue);
Assert.AreEqual(testValue, cell.NumericCellValue); // test passed

testValue = 2;
cell = CreateCellWithCustomTestFormula(testValue);
// we expected that TestFormula returns 2
Assert.AreEqual(testValue, cell.NumericCellValue); // test failed: Expected: 2 But was: 1.0d
}

private ICell CreateCellWithCustomTestFormula(int testValue)
{
var workbook = new HSSFWorkbook();
var testFormula = new TestFormula {TestValue = testValue};
var udfs = new DefaultUDFFinder(new[] {"TestFormula"}, new FreeRefFunction[] {testFormula});
var udfToolpack = new AggregatingUDFFinder(udfs);
workbook.AddToolPack(udfToolpack);
var sheet = workbook.CreateSheet("Test");
var cell = sheet.CreateRow(0).CreateCell(0);
cell.SetCellFormula("TestFormula()");
var formulaEvaluator = workbook.GetCreationHelper().CreateFormulaEvaluator();
formulaEvaluator.EvaluateAll();
return cell;
}
}

}
```

Viewing all articles
Browse latest Browse all 1621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>