diff --git a/src/pycel/excelformula.py b/src/pycel/excelformula.py index 23ab237..3596022 100644 --- a/src/pycel/excelformula.py +++ b/src/pycel/excelformula.py @@ -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. @@ -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 diff --git a/tests/test_excelformula.py b/tests/test_excelformula.py index ad5ec7c..87b5da1 100644 --- a/tests/test_excelformula.py +++ b/tests/test_excelformula.py @@ -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',