Skip to content

Commit

Permalink
accounting for white spaces in ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
igheorghita committed Oct 30, 2021
1 parent 6ce2bc2 commit a967e2c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/pycel/excelformula.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ def _items(self):

# convert or remove unneeded whitespace
tokens = []
consumed = False
for prev_token, token, next_token in zip(t, t[1:], t[2:]):
last_token = tokens[-1] if tokens else prev_token
if consumed:
consumed = False
continue
if token.type != Token.WSPACE or not prev_token or not next_token:
# ::HACK:: this is code to make the tokenizer behave like
# this change to the openpyxl tokenizer.
Expand Down Expand Up @@ -92,6 +97,15 @@ def _items(self):
elif not token.matches(type_=Token.OP_PRE, value='+'):
tokens.append(token)

elif token.type == Token.WSPACE and \
last_token.matches(type_=Token.OPERAND, subtype=Token.RANGE) and \
next_token.matches(type_=Token.OPERAND, subtype=Token.RANGE) and \
any(c in '!:' for c in (last_token.value[-1], next_token.value[0])):
tokens.pop()
tokens.append(Token(last_token.value + next_token.value,
type_=Token.OPERAND, subtype=Token.RANGE))
consumed = True

elif (
prev_token.matches(type_=Token.FUNC, subtype=Token.CLOSE) or
prev_token.matches(type_=Token.PAREN, subtype=Token.CLOSE) or
Expand Down
28 changes: 28 additions & 0 deletions tests/test_excelformula.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ def stringify_rpn(e):
'=SUM(B5:B15 A7:D7)',
'B5:B15|A7:D7| |SUM',
'sum_(_R_(str(_REF_("B5:B15") & _REF_("A7:D7"))))'),
FormulaTest(
'=SUM( sheet1!A1:A2)',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM(sheet1!A1:A2 )',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM(sheet1! A1:A2)',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM(sheet1 !A1:A2)',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM(sheet1!A1 :A2)',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM(sheet1!A1: A2)',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM(sheet1!A1 : A2)',
'sheet1!A1:A2|SUM',
'sum_(_R_("sheet1!A1:A2"))'),
FormulaTest(
'=SUM((A:A,1:1))',
'A:A|1:1|,|SUM',
Expand Down

0 comments on commit a967e2c

Please sign in to comment.