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

How to properly use the excel_helper decorator? #138

Open igheorghita opened 2 years ago

igheorghita commented 2 years ago

What actually happened and expected behavior

Sorry, this is probably more a question than an issue, but I'm trying to use the excel_helper decorator and it's not behaving the way I would have expected, so I'm wondering if I've misunderstood the purpose of it. For example, I wanted to write a columns function. My first idea was to do something along the lines of (ignoring edge cases here):

@excel_helper(ref_params=0)
def columns(ref):
    # Excel reference: https://support.microsoft.com/en-us/office/
    #   columns-function-4e8e7b4e-e603-43e8-b177-956088fa48ca
    if ref.is_range:
        return ref.end.col_idx - ref.start.col_idx

I would have expected the decorator to force the argument to be an AddressCell or AddressRange, but it doesn't seem to do anything. For example, if I do something like:

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws['E1'] = '=COLUMNS(A1:C1)'

from pycel.excelcompiler import ExcelCompiler
model = ExcelCompiler(excel=wb)
model.evaluate('Sheet!E1')

I see that when the model tries to evaluate 'E1', the ref argument is (None, None, None) and not the AddressRange I expected. Is there somewhere else in the code where I need to indicate that I expected the columns function to accept references? Or is there a reason why this shouldn't be done?

I noticed that with the column function, even if I remove the excel_helper decorator, it still parses the input as an AddressCell, which makes me think I've misunderstood the point of the decorator.

We can define the columns function with something like this instead, but I thought the reference way would be cleaner:

def columns(value):
    return len(value[0])