Can we store the name of the columns in a table and use it for calculations?

Is it possible to do this for example :

Note: the code below does not work, it is just to illustrate the question.

result = 0
lst = ["COL_A","COL_B","COL_C"]
for i in lst:
	if $lst[i]>0: result+=1
return result

$col is Grist magic which is converted into rec.col. The way to get .col given the string "col" in Python is to use the getattr function, so you can write:

for column_name in lst:
    value = getattr(rec, column_name)
    if value > 0...

However if lst is a long list, i.e. you have many columns with similar meanings, it might be better to restructure your table and combine all these columns into one, with another column to distinguish between different types.

4 Likes

Great! This is exactly what I needed.

My code betrays the PHP developer that I am.

Thanks for the recommendations, really nice. :+1:

1 Like