Skip to content

Commit

Permalink
fix(excel), #134: Correct inverse_references handling when model de…
Browse files Browse the repository at this point in the history
…fined with `from_dict`.
  • Loading branch information
vinci1it2000 committed Mar 22, 2024
1 parent be85a89 commit 7484d06
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 4 additions & 2 deletions formulas/excel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,13 @@ def from_dict(self, adict, context=None, assemble=True, ref=True):
nodes.update(cell.add(self.dsp, context=context))
cells[cell.output] = cell
self._update_refs(nodes, refs)
for cell in cells.values():
nodes.update(cell.compile(references=refs).add(self.dsp))
for k, cell in cells.items():
if k not in refs:
nodes.update(cell.compile(references=refs).add(self.dsp))
self.cells.update(cells)
if assemble:
self.assemble()
self.inverse_references()
return self

def write(self, books=None, solution=None, dirpath=None):
Expand Down
10 changes: 5 additions & 5 deletions test/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ def test_excel_model_full_range(self):

def test_excel_from_dict(self):
xl_model = ExcelModel().from_dict({
'A1': 1, 'B2': '=R[-1]C[-1]', 'A': 2, 'B': '=2'
'A1': 1, 'B2': '=R[-1]C[-1]', 'A': 2, 'B': '=2', 'C': '=A1'
}).finish()
self.assertEqual({
k: v.value if isinstance(v, Ranges) else v
for k, v in xl_model.calculate().items()
}, {'A1': 1, 'B2': 1, 'A': 2, 'B': 2})
self.assertEqual({'A1': 2, 'B2': 2, 'A': 2, 'B': 2, 'C': 2}, {
k: v.value.ravel()[0] if isinstance(v, Ranges) else v
for k, v in xl_model.calculate({'C': 2}).items()
})

def tearDown(self) -> None:
shutil.rmtree(osp.join(mydir, 'tmp'), ignore_errors=True)

0 comments on commit 7484d06

Please sign in to comment.