def test_handle_ifs_op_range(): with pytest.raises(TypeError): handle_ifs(((1, ), (1, )), 2) assert handle_ifs((((1, 2), (3, 4)), ">=3"), ((1, ), (1, ))) == VALUE_ERROR assert handle_ifs((((1, ), ), "=1"), 1) == ((0, 0), )
def test_handle_ifs(data, expected): if isinstance(expected, type(Exception)): with pytest.raises(expected): handle_ifs(data) elif isinstance(expected, str): assert handle_ifs(data) == expected else: assert tuple(sorted(handle_ifs(data))) == expected
def test_handle_ifs(data, result): if isinstance(result, type(Exception)): with pytest.raises(result): handle_ifs(data) elif isinstance(result, str): assert handle_ifs(data) == result else: assert tuple(sorted(handle_ifs(data))) == result
def sumifs(sum_range, *args): # Excel reference: https://support.office.com/en-us/article/ # SUMIFS-function-C9E748F5-7EA7-455D-9406-611CEBCE642B return sum( _numerics((sum_range[r][c] for r, c in handle_ifs(args, sum_range)), keep_bools=True))
def averageifs(average_range, *args): # Excel reference: https://support.office.com/en-us/article/ # AVERAGEIFS-function-48910C45-1FC0-4389-A028-F7C5C3001690 coords = handle_ifs(args, average_range) data = _numerics((average_range[r][c] for r, c in coords), keep_bools=True) if len(data) == 0: return DIV0 return sum(data) / len(data)
def countifs(*args): # Excel reference: https://support.office.com/en-us/article/ # COUNTIFS-function-dda3dc6e-f74e-4aee-88bc-aa8c2a866842 coords = handle_ifs(args) # A returned string is an error code if isinstance(coords, str): return coords return len(coords)
def minifs(min_range, *args): # Excel reference: https://support.office.com/en-us/article/ # minifs-function-6ca1ddaa-079b-4e74-80cc-72eef32e6599 if not list_like(min_range): min_range = ((min_range, ), ) try: return min( _numerics( (min_range[r][c] for r, c in handle_ifs(args, min_range)), keep_bools=True)) except ValueError: return 0
def maxifs(max_range, *args): # Excel reference: https://support.office.com/en-us/article/ # maxifs-function-dfd611e6-da2c-488a-919b-9b6376b28883 if not list_like(max_range): max_range = ((max_range, ), ) try: return max( _numerics( (max_range[r][c] for r, c in handle_ifs(args, max_range)), keep_bools=True)) except ValueError: return 0
def sumifs(sum_range, *args): # Excel reference: https://support.microsoft.com/en-us/office/ # SUMIFS-function-C9E748F5-7EA7-455D-9406-611CEBCE642B if not list_like(sum_range): sum_range = ((sum_range, ), ) coords = handle_ifs(args, sum_range) # A returned string is an error code if isinstance(coords, str): return coords return sum(_numerics((sum_range[r][c] for r, c in coords), keep_bools=True))
def averageifs(average_range, *args): # Excel reference: https://support.office.com/en-us/article/ # AVERAGEIFS-function-48910C45-1FC0-4389-A028-F7C5C3001690 if not list_like(average_range): average_range = ((average_range, ), ) coords = handle_ifs(args, average_range) # A returned string is an error code if isinstance(coords, str): return coords data = _numerics((average_range[r][c] for r, c in coords), keep_bools=True) if len(data) == 0: return DIV0 return sum(data) / len(data)
def minifs(min_range, *args): # Excel reference: https://support.microsoft.com/en-us/office/ # minifs-function-6ca1ddaa-079b-4e74-80cc-72eef32e6599 if not list_like(min_range): min_range = ((min_range, ), ) try: coords = handle_ifs(args, min_range) # A returned string is an error code if isinstance(coords, str): return coords return min( _numerics((min_range[r][c] for r, c in coords), keep_bools=True)) except ValueError: return 0
def maxifs(max_range, *args): # Excel reference: https://support.microsoft.com/en-us/office/ # maxifs-function-dfd611e6-da2c-488a-919b-9b6376b28883 if not list_like(max_range): max_range = ((max_range, ), ) try: coords = handle_ifs(args, max_range) # A returned string is an error code if isinstance(coords, str): return coords return max( _numerics((max_range[r][c] for r, c in coords), keep_bools=True)) except ValueError: return 0
def countifs(*args): # Excel reference: https://support.office.com/en-us/article/ # COUNTIFS-function-dda3dc6e-f74e-4aee-88bc-aa8c2a866842 return len(handle_ifs(args))
def test_handle_ifs_op_range_errors(): with pytest.raises(TypeError): handle_ifs(((1, ), (1, )), 2) with pytest.raises(AssertionError): handle_ifs(((((1, 2), (3, 4)), ">=3")), ((1, ), (1, )))