dgorissen / pycel

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

NPV function fails when passed range of cashflows #89

Closed Twoy519 closed 4 years ago

Twoy519 commented 4 years ago

What actually happened

I have an excel file (call it "test.xlsx") with the following layout:

Sheet1!A1 -> =NPV(0.1,B1:C1) Sheet1!B1 -> 10 Sheet1!C1 -> 20

I am attempting to evaluate cell Sheet1!A1 with the following code in test.py

  from pycel import ExcelCompiler
  excel = ExcelCompiler(filename="test.xlsx")
  print("A1 is %s" % excel.evaluate('Sheet1!A1'))
  excel.set_value('Sheet1!B1', 200)
  print('A1 is now %s' % excel.evaluate('Sheet1!A1'))

This produces a value error I believe caused by passing a range instead of a comma-separated list of cells:

env ❯ python test.py
A1 is 25.6198347107438
A1 is now #VALUE!

What was expected to happen

I would expect this to evaluate A1 to 198.347 and produce the following console log:

env ❯ python test.py
A1 is 25.6198347107438
A1 is now 198.3471074380165

If I change the formula in cell A1 to =NPV(0.1, B1,C1) this does evaluate correctly

Environment

env ❯ pip freeze --local
decorator==4.4.1
et-xmlfile==1.0.1
jdcal==1.4.1
networkx==2.4
numpy==1.18.1
openpyxl==3.0.3
pycel==1.0b22
python-dateutil==2.8.1
ruamel.yaml==0.16.10
ruamel.yaml.clib==0.2.0
six==1.14.0
env ❯ python -V
Python 3.7.5
>>> import platform
>>> platform.platform()
Darwin-19.3.0-x86_64-i386-64bit'
rmorel commented 4 years ago

@jpp-0 I improved the code from your pull request (#96) to work with numpy types as input cashflows.

Otherwise it will treat numpy types as invalid inputs to the NPV cashflow.