Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Cell function #1335

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/SS/Formula/Eval/FunctionEval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private static Function[] ProduceFunctions()
retval[122] = new NotImplementedFunction("NAMES"); // NAMES
retval[123] = new NotImplementedFunction("DIRECTORY"); // DIRECTORY
retval[124] = TextFunction.FIND; // Find
retval[125] = new NotImplementedFunction("CELL"); // CELL
retval[125] = new Cell(); // CELL
retval[126] = LogicalFunction.ISERR; // IsERR
retval[127] = LogicalFunction.ISTEXT; // IsTEXT
retval[128] = LogicalFunction.ISNUMBER; // IsNUMBER
Expand Down
77 changes: 77 additions & 0 deletions main/SS/Formula/Functions/Cell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
2020-03-20 Buzz Weetman
Custom created because NPOI doesn't implement CELL.
I am specifically fixing a case where it is used as CELL("col",W2)
The COLUMN function will do that same thing as COLUMN(W2)
But to avoid having to change existing templates, this implementation has been made.
It only handles the "col" and "row" parameters.
*/
namespace NPOI.SS.Formula.Functions
{
using NPOI.SS.Formula.Eval;

public class Cell : Function2Arg
{
public ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1)
{
string s0 = TextFunction.EvaluateStringArg(arg0, srcRowIndex, srcColumnIndex);

// Only "col" and "row" are implemented

int rnum;

if(string.Equals(s0, "col", System.StringComparison.InvariantCultureIgnoreCase))
{
// "col"
if(arg1 is AreaEval)
{
rnum = ((AreaEval) arg1).FirstColumn;
}
else if(arg1 is RefEval)
{
rnum = ((RefEval) arg1).Column;
}
else
{
// anything else is not valid argument
return ErrorEval.VALUE_INVALID;
}
}
else if(string.Equals(s0, "row", System.StringComparison.InvariantCultureIgnoreCase))
{
// "row"
if(arg1 is AreaEval)
{
rnum = ((AreaEval) arg1).FirstRow;
}
else if(arg1 is RefEval)
{
rnum = ((RefEval) arg1).Row;
}
else
{
// anything else is not valid argument
return ErrorEval.VALUE_INVALID;
}
}
else
{
//https://support.microsoft.com/en-us/office/cell-function-51bd39a5-f338-4dbe-a33f-955d67c2b2cf
// anything else is not valid argument, until we implement more
return ErrorEval.VALUE_INVALID;
}

return new NumberEval(rnum + 1);
}

public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex)
{
if (args.Length == 2)
{
return Evaluate(srcRowIndex, srcColumnIndex, args[0], args[1]);
}

return ErrorEval.VALUE_INVALID;
}
}
}
15 changes: 15 additions & 0 deletions testcases/main/SS/Formula/Functions/TestRowCol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public void TestCol()
}
}
[Test]
public void TestCell()
{
Function target = new Cell();
{
ValueEval[] args = { new StringEval("col"), EvalFactory.CreateRefEval("B5"), };
double actual = NumericFunctionInvoker.Invoke(target, args);
Assert.AreEqual(2, actual);
}
{
ValueEval[] args = { new StringEval("row"), EvalFactory.CreateRefEval("B5"), };
double actual = NumericFunctionInvoker.Invoke(target, args);
Assert.AreEqual(5, actual);
}
}
[Test]
public void TestRow()
{
//throw new NotImplementedException();
Expand Down
Loading