Skip to content
Closed
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
17 changes: 15 additions & 2 deletions pyxlsb2/ptgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ def __init__(self, idx, reserved, *args, **kwargs):
self.idx = idx
self._reserved = reserved

# FIXME: We need to read names to stringify this
def stringify(self, tokens, workbook):
defined = workbook.defined_names[workbook.list_names[self.idx - 1]]
return '%s (%s)' % (defined.name, defined.formula)

@classmethod
def read(cls, reader, ptg):
Expand Down Expand Up @@ -622,6 +624,10 @@ def __init__(self, extern_sheet_id, row, col, row_rel, col_rel, *args, **kwargs)
self.col_rel = col_rel

def stringify(self, tokens, workbook):
if (not self.col_rel) and (self.col & 0x2000 == 0x2000):
self.col -= 16384
if (not self.row_rel) and (self.row & 0x80000 == 0x80000):
self.row -= 1048576
cell_add = self.cell_address(self.col, self.row, self.col_rel, self.row_rel)

supporting_link, first_sheet_idx, last_sheet_idx = workbook.resolve_extern_sheet_id(self.extern_sheet_idx)
Expand All @@ -630,6 +636,8 @@ def stringify(self, tokens, workbook):
if supporting_link.brt == rt.SUP_SAME or supporting_link.brt == rt.SUP_SELF:
if first_sheet_idx == last_sheet_idx and first_sheet_idx >= 0:
address = workbook.sheets[first_sheet_idx].name + '!' + cell_add
elif first_sheet_idx == last_sheet_idx and first_sheet_idx == -2:
address = cell_add

if address is None:
raise NotImplementedError('External address not supported')
Expand All @@ -645,6 +653,11 @@ def read(cls, reader, ptg):
col_rel = col & 0x4000 == 0x4000
return cls(sheet_extern_idx, row, col & 0x3FFF, not row_rel, not col_rel, ptg)

def cell_address(self, col, row, col_rel, row_rel):
col_name = '$' + ClassifiedPtg.convert_to_column_name(col + 1) if col_rel else '@%s' % str(col)
row_name = '$' + str(row + 1) if row_rel else '@%s' % str(row)
return '[' + col_name + ', ' + row_name + ']'


class Area3dPtg(ClassifiedPtg):
ptg = 0x3B
Expand Down Expand Up @@ -922,7 +935,7 @@ def __init__(self, idx, argc, prompt, ce, *args, **kwargs):
def stringify(self, tokens, workbook):
args = list()
for i in xrange(self.argc):
arg = tokens.pop().stringify(tokens, workbook)
arg = tokens.pop().stringify(tokens, workbook).strip()
args.append(arg)

return '{}({})'.format(function_names[self.idx][0], ', '.join(reversed(args)))
Expand Down
2 changes: 2 additions & 0 deletions pyxlsb2/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def _parse(self):
self.external_sheet_ids = None
self.externals = None
self.defined_names = {}
self.list_names = []

workbook_rels = self._pkg.get_workbook_rels()
with self._pkg.get_workbook_part() as f:
Expand All @@ -63,6 +64,7 @@ def _parse(self):
elif rectype == rt.SUP_SELF or rectype == rt.SUP_SAME:
self.externals['SupportingLinks'].append(rec)
elif rectype == rt.NAME:
self.list_names.append(rec.name)
self.defined_names[rec.name] = rec
rec.formula = Formula.parse(rec.formula_raw).stringify(self)
# break
Expand Down