dgorissen / pycel

A library for compiling excel spreadsheets to python code & visualizing them as a graph
GNU General Public License v3.0
565 stars 147 forks source link

Formula parsing error #158

Open jt269 opened 1 year ago

jt269 commented 1 year ago

I have a spreadsheet with lots of range calculations (Sumif).

This is an example Excel formula:

=IF($Q13=0,0,IF(OR($D13>X$7,X$11<IF($D13=$X$3,$AI$2,$U12)),0,MAX(0,MIN($T13,$Q13-SUM($W13:W13),$S13-IF($D13>=$X$3,SUMIF($D$12:$D12,">="&$X$3,X$12:X12),SUMIF($D$12:$D12,"<"&$X$3,X$12:X12))))))

When Pycel tries to parse the function I get this error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Traceback error:

Traceback (most recent call last):
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelformula.py", line 923, in eval_func
    excel_formula.compiled_lambda())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelformula.py", line 948, in <lambda>
    ### Traceback will show this line if not loaded from a text file
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelcompiler.py", line 782, in _evaluate_range
    data = tuple(
           ^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelcompiler.py", line 783, in <genexpr>
    tuple(self._evaluate(addr.address) for addr in row)
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelcompiler.py", line 783, in <genexpr>
    tuple(self._evaluate(addr.address) for addr in row)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelcompiler.py", line 809, in _evaluate
    value = self.eval(cell)
            ^^^^^^^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelcompiler.py", line 172, in _eval
    return eval_ctx(
           ^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelformula.py", line 934, in eval_func
    error_logger('error', f"{address}{excel_formula.python_code}",
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelformula.py", line 877, in error_logger
    raise exc(error_msg)
pycel.excelformula.FormulaEvalError: Traceback (most recent call last):
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelformula.py", line 923, in eval_func
    excel_formula.compiled_lambda())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excelformula.py", line 948, in <lambda>
    ### Traceback will show this line if not loaded from a text file

  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excellib.py", line 333, in sumif
    return sumifs(sum_range, rng, criteria)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jtown\AppData\Local\Programs\Python\Python311\Lib\site-packages\pycel\excellib.py", line 348, in sumifs
    return sum(_numerics(
           ^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Eval: BS4 Schedule!X14: if_(_C_("BS4 Schedule!Q14") == 0, 0, if_(or_(_C_("BS4 Schedule!D14") > _C_("BS4 Schedule!X7"), _C_("BS4 Schedule!X11") < if_(_C_("BS4 Schedule!D14") == _C_("BS4 Schedule!X3"), _C_("BS4 Schedule!AI2"), _C_("BS4 Schedule!U13"))), 0, max_(0, min_(_C_("BS4 Schedule!T14"), _C_("BS4 Schedule!Q14") - sum_(_C_("BS4 Schedule!W14")), _C_("BS4 Schedule!S14") - if_(_C_("BS4 Schedule!D14") >= _C_("BS4 Schedule!X3"), sumif(_R_("BS4 Schedule!D12:D13"), ">=" & _C_("BS4 Schedule!X3"), _R_("BS4 Schedule!X12:X13")), sumif(_R_("BS4 Schedule!D12:D13"), "<" & _C_("BS4 Schedule!X3"), _R_("BS4 Schedule!X12:X13")))))))
Eval: BS4 Schedule!X9: sum_(_R_("BS4 Schedule!X13:X47"))
Eval: BS4 Schedule!X249: _C_("BS4 Schedule!X9")
Eval: BS4 Schedule!W250: sumproduct(_R_("BS4 Schedule!X249:EB249"), _R_("BS4 Schedule!X250:EB250"))
Eval: Sheet1!D1: _C_("BS4 Schedule!W250")

Environment

Pycel Version most recent from PyPi, Python Version 3.11 and Windows 10.