Beispiel #1
0
def ifs(*args):
    # IFS function
    # Excel 2016
    # Checks whether one or more conditions are met and returns a value that
    # corresponds to the first TRUE condition.
    # Excel reference: https://support.microsoft.com/en-us/office/
    #   ifs-function-36329a26-37b2-467c-972b-4a39bd951d45
    if not len(args) % 2:
        if in_array_formula_context and any(
                isinstance(a, tuple) for a in args):
            return cse_array_wrapper(ifs, tuple(range(len(args))))(*args)

        for test, value in zip(args[::2], args[1::2]):

            if test in ERROR_CODES:
                return test

            if isinstance(test, str):
                if test.lower() in ('true', 'false'):
                    test = len(test) == 4
                else:
                    return VALUE_ERROR

            if test:
                return value

    return NA_ERROR
Beispiel #2
0
def iferror(arg, value_if_error):
    # Excel reference: https://support.microsoft.com/en-us/office/
    #   IFERROR-function-C526FD07-CAEB-47B8-8BB6-63F3E417F611
    if in_array_formula_context and has_array_arg(arg, value_if_error):
        return cse_array_wrapper(iferror, (0, 1))(arg, value_if_error)
    elif arg in ERROR_CODES or is_array_arg(arg):
        return 0 if value_if_error is None else value_if_error
    else:
        return arg
Beispiel #3
0
def ifna(arg, value_if_na):
    # Excel reference: https://support.microsoft.com/en-us/office/
    #   ifna-function-6626c961-a569-42fc-a49d-79b4951fd461
    if in_array_formula_context and has_array_arg(arg, value_if_na):
        return cse_array_wrapper(ifna, (0, 1))(arg, value_if_na)
    elif arg == NA_ERROR or is_array_arg(arg):
        return 0 if value_if_na is None else value_if_na
    else:
        return arg
Beispiel #4
0
def iferror(arg, value_if_error):
    # Excel reference: https://support.office.com/en-us/article/
    #   IFERROR-function-C526FD07-CAEB-47B8-8BB6-63F3E417F611

    if in_array_formula_context and (isinstance(arg, tuple)
                                     or isinstance(value_if_error, tuple)):
        return cse_array_wrapper(iferror, (0, 1))(arg, value_if_error)
    elif arg in ERROR_CODES or isinstance(arg, tuple):
        return 0 if value_if_error is None else value_if_error
    else:
        return arg
def test_cse_array_wrapper(arg_num, f_args, result):
    def f_test(*args):
        return args[arg_num] + 1

    assert cse_array_wrapper(f_test, arg_num)(*f_args) == result
def test_cse_array_wrapper(arg_num, f_args, result):

    def f_test(*args):
        return args[arg_num] + 1

    assert cse_array_wrapper(f_test, arg_num)(*f_args) == result