-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1341 from dcjxdd123/xdddevelop
Implement Concat Function
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace NPOI.SS.Formula.Functions | ||
{ | ||
using System.Text; | ||
using NPOI.SS.Formula.Eval; | ||
|
||
public class Concat : FreeRefFunction | ||
{ | ||
public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
foreach(ValueEval arg in args) | ||
{ | ||
try | ||
{ | ||
if(arg is AreaEval area) | ||
{ | ||
for(int rn = 0; rn<area.Height; rn++) | ||
{ | ||
for(int cn = 0; cn<area.Width; cn++) | ||
{ | ||
ValueEval ve = area.GetRelativeValue(rn, cn); | ||
sb.Append(TextFunction.EvaluateStringArg(ve, ec.RowIndex, ec.ColumnIndex)); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
sb.Append(TextFunction.EvaluateStringArg(arg, ec.RowIndex, ec.ColumnIndex)); | ||
} | ||
} | ||
catch (EvaluationException e) { | ||
return e.GetErrorEval(); | ||
} | ||
} | ||
return new StringEval(sb.ToString()); | ||
} | ||
|
||
} | ||
} |
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,104 @@ | ||
using NPOI.HSSF.UserModel; | ||
using NPOI.SS.Formula.Eval; | ||
using NPOI.SS.UserModel; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace TestCases.SS.Formula.Atp | ||
{ | ||
[TestFixture] | ||
public class TestConcat | ||
{ | ||
private IWorkbook wb; | ||
private ISheet sheet; | ||
private IFormulaEvaluator evaluator; | ||
private ICell textCell1; | ||
private ICell textCell2; | ||
private ICell numericCell1; | ||
private ICell numericCell2; | ||
private ICell textCell3; | ||
private ICell formulaCell; | ||
|
||
private IWorkbook InitWorkbook1() | ||
{ | ||
IWorkbook wb = new HSSFWorkbook(); | ||
ISheet sheet = wb.CreateSheet("Sheet1"); | ||
|
||
sheet.CreateRow(0).CreateCell(0).SetCellValue("Currency"); | ||
sheet.CreateRow(1).CreateCell(0).SetCellValue("US Dollar"); | ||
sheet.CreateRow(2).CreateCell(0).SetCellValue("Australian Dollar"); | ||
sheet.CreateRow(3).CreateCell(0).SetCellValue("Chinese Yuan"); | ||
sheet.CreateRow(4).CreateCell(0).SetCellValue("Hong Kong Dollar"); | ||
sheet.CreateRow(5).CreateCell(0).SetCellValue("Israeli Shekel"); | ||
sheet.CreateRow(6).CreateCell(0).SetCellValue("South Korean Won"); | ||
sheet.CreateRow(7).CreateCell(0).SetCellValue("Russian Ruble"); | ||
return wb; | ||
} | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
wb = new HSSFWorkbook(); | ||
evaluator = wb.GetCreationHelper().CreateFormulaEvaluator(); | ||
|
||
sheet = wb.CreateSheet("CONCAT"); | ||
IRow row = sheet.CreateRow(0); | ||
|
||
textCell1 = row.CreateCell(0); | ||
textCell1.SetCellValue("One"); | ||
|
||
textCell2 = row.CreateCell(1); | ||
textCell2.SetCellValue("Two"); | ||
|
||
textCell3 = row.CreateCell(2); | ||
textCell3.SetCellValue("Three"); | ||
|
||
numericCell1 = row.CreateCell(3); | ||
numericCell1.SetCellValue(1); | ||
|
||
numericCell2 = row.CreateCell(4); | ||
numericCell2.SetCellValue(2); | ||
|
||
formulaCell = row.CreateCell(100, CellType.Formula); | ||
} | ||
|
||
[Test] | ||
public void TestConcatWithStrings() | ||
{ | ||
evaluator.ClearAllCachedResultValues(); | ||
formulaCell.SetCellFormula("CONCAT(\"The\",\" \",\"sun\",\" \",\"will\",\" \",\"come\",\" \",\"up\",\" \",\"tomorrow.\")"); | ||
evaluator.EvaluateFormulaCell(formulaCell); | ||
Assert.AreEqual("The sun will come up tomorrow.", formulaCell.StringCellValue); | ||
} | ||
|
||
[Test] | ||
public void TestConcatWithColumns() | ||
{ | ||
evaluator.ClearAllCachedResultValues(); | ||
formulaCell.SetCellFormula("CONCAT(B:B, C:C)"); | ||
evaluator.EvaluateFormulaCell(formulaCell); | ||
Assert.AreEqual("TwoThree", formulaCell.StringCellValue); | ||
} | ||
|
||
[Test] | ||
public void TestConcatWithCellRanges() | ||
{ | ||
evaluator.ClearAllCachedResultValues(); | ||
formulaCell.SetCellFormula("CONCAT(A1:C1)"); | ||
evaluator.EvaluateFormulaCell(formulaCell); | ||
Assert.AreEqual("OneTwoThree", formulaCell.StringCellValue); | ||
} | ||
|
||
[Test] | ||
public void TestConcatWithCellRefs() | ||
{ | ||
evaluator.ClearAllCachedResultValues(); | ||
formulaCell.SetCellFormula("CONCAT(\"ONE\", A1, \"TWO\",B1, \"THREE\",C1, \".\")"); | ||
evaluator.EvaluateFormulaCell(formulaCell); | ||
Assert.AreEqual("ONEOneTWOTwoTHREEThree.", formulaCell.StringCellValue); | ||
} | ||
|
||
} | ||
} |