Question
There is a simple table.
In one of the columns, I calculate the field value in the code. And in the result, I take listCount
.
In conditional formatting, I have the same code that outputs in the comment, listJoin
of the same list used in listCount in the Code.
The problem is that the calculation itself is heavy and is calculated twice. How can I pass the intermediate calculation result (the list itself) from the Code field to the "field formatting" in the same field, so it doesn't calculate twice?
There is a solution to calculate the list separately in a separate field and use the result in the necessary code. But I wondered: is it possible to do it without an additional column?
Answer
In totum, the order of code calculation in the field is as follows ā Code, Action Code, Select Code, Formatting Code. Therefore, in earlier codes, you can write data to a process variable and use it in subsequent code.
In this case, we will create a process variable in Code and use it in Formatting Code.
To ensure that process variables work individually for each row, the row id must be used in the name of the process variable. As many variables are created as there are rows recalculated.
Code in the Code section:
=: listCount(list: $procvar)
procvar: procVar(name: str`"list" + #id`; value: $some_code)
some_code: "some code here"
In the formatting code of the same field:
f1=: setFormat(comment: $join)
join: listJoin(list: $procvar; str: ", ")
procvar: procVar(name: str`"list" + #id`; default: $some_code)
some_code: "some code here"
When we call the procVar function in the formatting code without specifying the value parameter, totum will check for its existence, and if the variable exists, it will return its value. If not, the default parameter will be used, which will calculate the required list similarly to the Code.
Thus, if the Code was executed, the formatting code will not perform a repeated calculation. But if the code was not executed, then it will be necessary to calculate this list directly in the formatting code.