Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pyxlsb2/formula.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .ptgs import NamePtg
from .tokenreader import TokenReader


Expand All @@ -11,10 +12,18 @@ def __repr__(self):
def __str__(self):
return self.stringify()

def stringify(self, workbook):
def stringify(self, workbook, row=None, col=None):
tokens = self._tokens[:]
return tokens.pop().stringify(tokens, workbook)
current_token = tokens.pop()
if isinstance(current_token, NamePtg):
return current_token.stringify(tokens, workbook, row, col)
else:
return current_token.stringify(tokens, workbook)

@classmethod
def parse(cls, data):
return cls(TokenReader(data))

@property
def tokens(self):
return self._tokens
16 changes: 11 additions & 5 deletions pyxlsb2/ptgs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import sys
from enum import Enum
from . import recordtypes as rt
Expand Down Expand Up @@ -26,9 +27,6 @@ def write(self, writer):
pass





class ClassifiedPtg(BasePtg):
def __init__(self, ptg, *args, **kwargs):
super(ClassifiedPtg, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -392,10 +390,18 @@ def __init__(self, idx, reserved, *args, **kwargs):
self.idx = idx
self._reserved = reserved

def stringify(self, tokens, workbook):
def stringify(self, tokens, workbook, row=None, col=None):
defined = workbook.defined_names[workbook.list_names[self.idx - 1]]
# return '%s (%s)' % (defined.name, defined.formula)
return defined.formula
formula = defined.formula
if row is not None and col is not None:
for link in re.findall(r'R\[?-?\d+\]?C\[?-?\d+\]?[,)\s]', formula):
address = re.match(r'R\[?-?\d+\]?C\[?-?\d+\]?', link).group()
p = address[1:].split('C')
r = int(p[0][1:-1]) + row + 1 if p[0].startswith('[') else row + 1
c = ClassifiedPtg.convert_to_column_name(int(p[1][1:-1]) + col + 1 if p[1].startswith('[') else col + 1)
formula = formula.replace(link, link.replace(address, '%s%s' % (c, r)))
return formula

@classmethod
def read(cls, reader, ptg):
Expand Down
1 change: 0 additions & 1 deletion pyxlsb2/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __init__(self, pkg):
self._pkg = pkg
self._parse()


def __enter__(self):
return self

Expand Down
9 changes: 5 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
for row in sheet:
for cell in row:
formula_str = Formula.parse(cell.formula)
if formula_str._tokens:
if formula_str.tokens:
try:
print(formula_str.stringify(wb))
pass
print(formula_str.stringify(wb, cell.row_num, cell.col))
except NotImplementedError as exp:
print('ERROR({}) {}'.format(exp, str(cell)))
except Exception:
print('ERROR ' + str(cell))
except Exception as e:
print('ERROR ' + str(cell)) + ' (%s)' % e
d = time.time() - a
print('Done! ({} seconds)'.format(d))