theorchard / openpyxl

Other
58 stars 21 forks source link

`Translator()` failure with percentage value #60

Open brianjmurrell opened 3 months ago

brianjmurrell commented 3 months ago

I'm trying to create a new cell with:

new_cell = ws.cell(row=new_row, column=cell.col_idx, value=Translator(cell.value, origin=cell.coordinate).translate_formula(new_coordinate))

which works for every other cell (and awesomely so for cells with formulae in them -- kudos for Translator()/translate_formula()) I create except for cells that have a percentage formatted float in them. The stacktrace and error are:

Traceback (most recent call last):
  File "/home/brian/tmp/add_row.py", line 33, in <module>
    new_cell = ws.cell(row=new_row, column=cell.col_idx, value=Translator(cell.value, origin=cell.coordinate).translate_formula(new_coordinate))
                                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brian/.local/lib/python3.12/site-packages/openpyxl/formula/translate.py", line 50, in __init__
    self.tokenizer = Tokenizer(formula)
                     ^^^^^^^^^^^^^^^^^^
  File "/home/brian/.local/lib/python3.12/site-packages/openpyxl/formula/tokenizer.py", line 53, in __init__
    self._parse()
  File "/home/brian/.local/lib/python3.12/site-packages/openpyxl/formula/tokenizer.py", line 61, in _parse
    elif self.formula[0] == '=':
         ~~~~~~~~~~~~^^^
TypeError: 'float' object is not subscriptable

Hopefully this is trivial to reproduce, but if not let me know and I will see if I can mock up a reproducer.