Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accounting for white spaces in ranges #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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