Use the pipe operator
The pipe operator "|" lets you add computed columns to a frame, which results in a new augmented frame.
To add a column to a frame, use the pipe operator "|" followed by an expression in the following format: @{column_name} = {value}.
So, to add a column "foo" with the value "bar" to a frame in cell A1, we'd use this formula:
To better understand how it works, let's take this spreadsheet data as an example:
A | B | |
---|---|---|
1 | product | |
2 | apples | |
3 | bananas | |
4 | kiwi | |
5 | melons | |
6 | strawberries | |
7 | peaches |
Let's make a frame from the data above, and then add to the frame a column "quantity" and a column "price" with random numbers:
The resulting frame is shown below (your result may differ due to random numbers):
Now, to add to the resulting frame a column "amount" that contains the result of multiplying the quantity by the price, use this formula:
Here is what we get as a result:
Let's modify the quantity column in the frame above by deducting 10 from its values:
The resulting frame:
Add columns with values based on a condition
To add values based on a condition, use the IF() function in the expression.
For example, to add a column "discount" whose values are based on whether the quantity is lower than 30:
The resulting frame:
Now, let's try a more convoluted example. Let's add a column "color", where the values are determined by the parity of the row number. If the row number is even, the color is gray; if the row number is odd, the color is white.
We can then use the colors to add a zebra-striping to a component in the UI.
To determine the row number, we use the special column expression @row, which returns the row number:
The resulting frame is shown in the figure below:
To determine if the row number is even or odd, we use the MOD() function that returns the remainder of dividing the first parameter by the second parameter.
For example, the following formula returns the remainder of dividing 42 by 2, which equals 0. It means that 42 is even.
So, given above, to add the color column to the frame, let's use this formula:
The resulting frame:
Now, we can use the color to add a zebra-striping to a component on the page:
To add the zebra-striping:
- Add a Text component to the page
- Bind the repeater of the Text component to the product column in the nested spreadsheet
- Bind the Text value to the first cell in product column
- Bind the Color property of the Text component to the color column in the nested spreadsheet