Skip to content

Commit

Permalink
Merge pull request #67 from Talm28/master
Browse files Browse the repository at this point in the history
Add IFERROR function + tests
  • Loading branch information
strichter authored Dec 18, 2023
2 parents 574736f + f75bb8d commit a9ffb8e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ Logical
+-----------------+--------------+-------+----------+-------+
| IF | * | * | * | |
+-----------------+--------------+-------+----------+-------+
| IFERROR | | * | * | * |
| IFERROR | * | * | * | * |
+-----------------+--------------+-------+----------+-------+
| IFS | | * | | |
+-----------------+--------------+-------+----------+-------+
Expand Down
Binary file added tests/resources/IFERROR.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/xlfunctions/test_logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ def test_NOT_with_direct_values(self):

def test_TRUE(self):
self.assertTrue(logical.TRUE())

def test_IFERROR(self):
self.assertEqual(logical.IFERROR(10/2,0),5) # Check if value is OK
self.assertEqual(logical.IFERROR(10/0,0),0) # Check if value cause error
self.assertEqual(logical.IFERROR(10/0,"ERROR"),"ERROR") # Check returning string
self.assertEqual(logical.IFERROR(logical.IFERROR(10/0,0) + 1,0),1) # Check nested IFERROR

37 changes: 37 additions & 0 deletions tests/xlfunctions_vs_excel/iferror_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from .. import testing

from xlcalculator.xlfunctions import xlerrors


class SqrtTest(testing.FunctionalTestCase):
filename = "IFERROR.xlsx"

def test_evaluation_C1(self):
self.assertEqual(
self.evaluator.get_cell_value('Sheet1!C1!'),
self.evaluator.evaluate('Sheet1!C1')
)

def test_evaluation_C2(self):
self.assertEqual(
self.evaluator.get_cell_value('Sheet1!C2!'),
self.evaluator.evaluate('Sheet1!C2')
)

def test_evaluation_C3(self):
self.assertEqual(
self.evaluator.get_cell_value('Sheet1!C3!'),
self.evaluator.evaluate('Sheet1!C3')
)

def test_evaluation_C4(self):
self.assertEqual(
self.evaluator.get_cell_value('Sheet1!C4!'),
self.evaluator.evaluate('Sheet1!C4')
)

def test_evaluation_C5(self):
self.assertEqual(
self.evaluator.get_cell_value('Sheet1!C5!'),
self.evaluator.evaluate('Sheet1!C5')
)
12 changes: 12 additions & 0 deletions xlcalculator/xlfunctions/logical.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,15 @@ def TRUE() -> func_xltypes.XlBoolean:
true-function-7652c6e3-8987-48d0-97cd-ef223246b3fb
"""
return True

@xl.register()
@xl.validate_args
def IFERROR(value: func_xltypes.XlExpr,
value_if_error: func_xltypes.XlAnything) -> func_xltypes.XlAnything:
"""Evaluate value, return value if has no error, otherwise return value_if_error.
https://support.microsoft.com/en-au/office/iferror-function-c526fd07-caeb-47b8-8bb6-63f3e417f611
"""
if isinstance(value(), xlerrors.ExcelError):
return value_if_error
return value()

0 comments on commit a9ffb8e

Please sign in to comment.