I am working on building formula reference map from spreadsheet xml using python. formula is like
=IF(AND(LEN(R[-2]C[-1])>0,R[-1]C),WriteCurve(OFFSET(R16C6, 0,0,R9C7,R10C7),R15C6,R10C3, R8C3),"NONE")
I am only interested in getting nth argument of writecurve function. here i comes up very C style program basically counting coma which is not inside bracket. there are a lot of nested formula
def parseArguments(t, func, n):
start=t.find(func)+len(func)+1
bracket = 0
ss = t[start:]
lastcomma = 0
for i, a in enumerate(ss):
if a=="(":
bracket +=1
elif a==")":
if bracket==0:
break
bracket-=1
elif a == ",":
if bracket==0 and n==0:
break
elif bracket ==0:
if n-1==0:
lastcomma = i
n-=1
if lastcomma == 0:
return ss[:i]
else:
return ss[lastcomma+1:i]
Is there pythonic way of doing this? or is there a better recursive way to parse the whole formula? Many thanks