pycel icon indicating copy to clipboard operation
pycel copied to clipboard

Formula Parsing Error

Open agoddijn opened this issue 1 year ago • 1 comments

What actually happened

[Please include the full traceback if there was an exception]

Traceback (most recent call last): File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelformula.py", line 923, in eval_func excel_formula.compiled_lambda()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelformula.py", line 948, in ### Traceback will show this line if not loaded from a text file ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/pycel/lib/stats.py", line 203, in countif valid = find_corresponding_index(rng, criteria) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelutil.py", line 1104, in find_corresponding_index return tuple(find_corresponding_index_generator(rng, criteria)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelutil.py", line 1113, in for c, item in enumerate(row) if check(item)) ^^^^^^^^^^^ File "/opt/homebrew/lib/python3.11/site-packages/pycel/excelutil.py", line 1031, in return lambda x: x is not None and compiled.match(x.lower()) is not None ^^^^^^^ AttributeError: 'int' object has no attribute 'lower'

What was expected to happen

If I cast x to string before calling lower, I get the right output

Problem description

[If needed, this should explain why the current behavior is a problem and why the expected output is a better solution]

Evaluation is failing when it shouldn't be

Code Sample

# Your code here
workbook = openpyxl.load_workbook(tmp_xlsx)
workbook_compiler = ExcelCompiler(excel=workbook)
workbook_compiler.evaluate(get_cell_coordinate(cell, sheet_name))

[If possible, include a minimal, complete, and verifiable example to help us identify the issue. This also helps check that the issue is not with your own code]

Environment

[Pycel Version, Python Version and OS used. Also any other environment details that you think might be relevant]

Python 3.11 Pycel 1.0b30

agoddijn avatar Feb 20 '24 15:02 agoddijn

I monkey patched build_wildcard_re in my code which solved my issues, but really didn't get too deep into the weeds here because I just need my thing to work

# ========= Start Monkey Patch =========
QUESTION_MARK_RE = re.compile(r'\?(?<!~)')
STAR_RE = re.compile(r'\*(?<!~)')

def build_wildcard_re_fixed(lookup_value):
    regex = QUESTION_MARK_RE.sub('.', STAR_RE.sub('.*', lookup_value))
    if regex != lookup_value:
        # this will be a regex match"""
        compiled = re.compile(f'^{regex.lower()}$')
        return lambda x: x is not None and compiled.match(str(x).lower()) is not None
    else:
        return None

excelutil.build_wildcard_re = build_wildcard_re_fixed
# ========= End Monkey Patch ========= 

agoddijn avatar Feb 20 '24 15:02 agoddijn