Given AVERAGE(D1:D4)
you can evaluate the argument of the Excel command D1:D4
with eval()
in Python. Here is an example of how you could parse AVERAGE(D1:D4)
import numpy as np
import re
excel_command = "AVERAGE(D1:D4)"
D0, D1, D2, D3, D4, D5 = [i for i in range(2, 8)]
def excel_to_py(excel_command):
if excel_command[:7] == 'AVERAGE':
command = excel_command[7:][1:-1] # strip command of 'AVERAGE' and parenthesis
# handle range (e.g. D1:D4)
range_command = command.split(':')
# determine the column id, in example it's 'D'
column = range_command[0][0]
if len(range_command) == 2: # if splitting by ':' gave two elements, it's a range
# get range: e.g. for D245:D366 we need only numerical values 245 and 366 for rows
complete_range = list(range(int(range_command[0][1:]), int(range_command[1][1:]) + 1))
print(complete_range)
argument = ', '.join([column+str(n) for n in complete_range])
print(argument)
average = np.mean(eval(argument)) # evaluating the
return average
average = excel_to_py(excel_command)
print(average)
Output is:
[1, 2, 3, 4]
D1, D2, D3, D4
4.5
where 4.5 is equal to np.mean(D1, D2, D3, D4) = (3 + 4 + 5 + 6)/4 = 4.5
Of course this is a basic example, where:
- the range (D1:D4) is only applied over a single column (D).
- only the range is handled, and not simply multiple elements (AVERAGE(D1, D3, D4))
- you could easily implement that in a similar way to what I provided above.
- other commands are not handled (summing, subtracting, etc...).
- again, follow my example above and have fun implementing those too if needed.
- only a single AVERAGE operation is handled, instead of an entire, more complex, expression of multiple different operations.
- it's simply a matter of parsing the input command and handling the operations in the right order.
Good luck!