vinci1it2000 / formulas

Excel formulas interpreter in Python.
https://formulas.readthedocs.io/
European Union Public License 1.1
358 stars 75 forks source link

I want to run excel without an excel file, and I think the to_dict and from_dict for this project are great #135

Closed toca-Y closed 6 months ago

toca-Y commented 6 months ago

Is your feature request related to a problem? Please describe. When I execute from_dict, I don't add finish() because adding this results in an error This results in the inability to calculate cell data with ranges

Additional context This is my code

import formulas

# it's formulas/test/test_files/excel.xlsx
fpath, dir_output = 'test/test_files/excel.xlsx', 'output'  # doctest: +SKIP
xl_model = formulas.ExcelModel().loads(fpath).finish()
xl_model2 = formulas.ExcelModel().from_dict(xl_model.to_dict()).finish()

# INPUT_A=A2, INPUT_B=A3 (They are defining names in the excel file.)
input_ = {
    "'[excel.xlsx]'!INPUT_A": 7,  # To overwrite the default value. default is 2
    "'[excel.xlsx]'!INPUT_B": 3,  # To overwrite the default value. default is 6
}

# B2=A2+A3
output = [
    "'[excel.xlsx]DATA'!B2", "'[excel.xlsx]DATA'!C2", "'[excel.xlsx]DATA'!C4",
    "'[excel.xlsx]DATA'!B4"
]

res1 = xl_model.calculate(inputs=input_, outputs=output)
res2 = xl_model2.calculate(inputs=input_, outputs=output)

print("xl_model B2:",
      res1.get("'[excel.xlsx]DATA'!B2").value[0, 0])  # xl_model B2: 10.0

print("xl_model2 B2:",
      res2.get("'[excel.xlsx]DATA'!B2").value[0, 0])  # xl_model2 B2: 10.0

print("xl_model B4:",
      res1.get("'[excel.xlsx]DATA'!B4"))  # xl_model B2:  <Ranges>('[excel.xlsx]DATA'!B4)=[[10.0]]

print("xl_model2 B4:",
      res2.get("'[excel.xlsx]DATA'!B4"))  # xl_model2 B2: <Ranges>('[excel.xlsx]DATA'!B4)=[[#REF!]]

This is the running information

Error in loading `'[excel.xlsx]DATA'!A3:A4`:
[Errno 2] No such file or directory: 'E:\\Projects\\PythonProjects\\POther\\formulas\\excel.xlsx'
xl_model B2: 10.0
xl_model2 B2: 10.0
xl_model B4: <Ranges>('[excel.xlsx]DATA'!B4)=[[10.0]]
xl_model2 B4: <Ranges>('[excel.xlsx]DATA'!B4)=[[#REF!]]

I want to achieve such A demand, A has excel files, B does not need excel files to execute relevant excel applications. I don't know if you understand what I said. Thank you very much for your help.

vinci1it2000 commented 6 months ago

Sorry I forgot to commit one line in the previous commit. try it without .finish().

toca-Y commented 6 months ago

抱歉,我忘记在上一次提交中提交一行。尝试不使用.finish().

If you do not use finish, the result is the same as before

xl_model B2: 10.0
xl_model2 B2: 8.0
xl_model B4: <Ranges>('[excel.xlsx]DATA'!B4)=[[10.0]]
xl_model2 B4: <Ranges>('[excel.xlsx]DATA'!B4)=[[8.0]]
vinci1it2000 commented 6 months ago

Have you tried to re-install my dev? because I overwritten the commit.

toca-Y commented 6 months ago

Have you tried to re-install my dev? because I overwritten the commit.

Thank you very much, I have tested the dev branch and this is available. I love using this library. Please allow me to trouble you again if there are other questions