dgorissen / pycel

A library for compiling excel spreadsheets to python code & visualizing them as a graph
GNU General Public License v3.0
565 stars 147 forks source link

accounting for white spaces in ranges #137

Open igheorghita opened 2 years ago

igheorghita commented 2 years ago

Previously, we had the following behavior for variations of '=SUM(Sheet2!A1:A2)':

from openpyxl import Workbook
wb = Workbook()
wb.create_sheet('Sheet1')
wb.create_sheet('Sheet2')
ws = wb['Sheet2']
ws['A1'], ws['A2'] = 1, 2

ws = wb['Sheet1']
ws['A1'] = f'=SUM(\'Sheet2\' !A1:A2)'    # FAILED - this one is a syntax error in Excel too, but you're prompted to correct it
ws['A2'] = f'=SUM(\'Sheet2\'! A1:A2)'    # #NAME?
ws['A3'] = f'=SUM(\'Sheet2\'!A1 :A2)'    # FAILED
ws['A4'] = f'=SUM(\'Sheet2\'!A1: A2)'    # #NAME?
ws['A5'] = f'=SUM(\'Sheet2\'!A1 : A2)'   # FAILED

Now these should all work (i.e., evaluate to 3). They'll also work with multiple whitespaces because the openpyxl tokenizer tokenizes it the same way.

I had to add the any(c in '!:' for c in (last_token.value[-1], next_token.value[0]) condition because without it other tests fail - like this possibility '=SUM((A:A A1:B1))'

codecov-commenter commented 2 years ago

Codecov Report

Merging #137 (a967e2c) into master (6ce2bc2) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #137   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           17        17           
  Lines         3757      3766    +9     
  Branches       912       914    +2     
=========================================
+ Hits          3757      3766    +9     
Impacted Files Coverage Δ
src/pycel/excelformula.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 6ce2bc2...a967e2c. Read the comment docs.