Calculating with numbers in grids in EDC/CDMS
Table of Contents
The instructions described in the article Calculate with grid fields renders the objects in string (plain text).
If you want to do mathematical operations with numbers, you will need to convert that text into numbers. For that you would use the parseInt() function.
Calculate the sum of a row within one grid
For example, we want to get the sum of the values in the first row of the grid shown below:
To get the sum of the first row (in this case it is 16), we would use:
parseInt(Object.values(obj[0])[0]) + parseInt(Object.values(obj[0])[1]);
You cannot calculate with a (half-)empty grid field - all the cells that are used in the calculation have to be filled in the grid to be able to execute the calculation.
If the numbers contain decimals we will need to use the function parseFloat() instead. For example, we want to get the sum of the values in the first row of the grid shown below:
To obtain as result 29.5 we have used this code:
parseFloat(Object.values(obj[0])[0]) + parseFloat(Object.values(obj[0])[1]);
Calculate the sum of rows within multiple grids
Below we will show you how to calculate the value of two separate grids. For this example, we want to calculate the sum of the values entered in Grid 1 and in Grid 2 as shown below:
To get the sum of the cells (in this case 15), we would use:
var obj = JSON.parse('{grid_1}');
var sum_grid1 = parseInt(Object.values(obj[0])[0])+parseInt(Object.values(obj[0])[1]);
var obj2 = JSON.parse('{grid_2}');
var sum_grid2 = parseInt(Object.values(obj2[0])[0])+parseInt(Object.values(obj2[0])[1]);
sum_grid1 + sum_grid2;
It is also possible to calculate the sum of only several cells in the grid, for example Column 1:Row 1 in Grid 1 and Column 1: Row 1 in Grid 2:
Below is the calculation template to calculate the sum of Column 1:Row 1 in Grid 1 and Column 1: Row 1 in Grid 2:
var obj = JSON.parse('{grid_1}');
var output = Object.values(obj[0])[0];
var obj2 = JSON.parse('{grid_2}');
var output2 = Object.values(obj2[0])[0];
parseInt(output)+parseInt(output2);
When calculating with some cells of the grid, all the cells that are used in the calculation have to be filled in both grids to be able to execute the calculation.