javascriptadobe

Javascript or other way to calculate Sum depending on value of other column


In Adobe Acrobat Pro I am trying to add fields depending on different column. I have no idea where to start. I already tried to use the Sum fuction of Adobe but here I can only select fields. Can someone please help me here? Maybe JavaScript or another solution?

Tha Table below shows what i am trying to achieve.

Col1 Col2
20 A
30 B
40 C
50 A
60 B

Col1 ist contains number fields Col2 contains only one Char e.g. A, B or C.

I am trying to add all values Col1 where value of col2 is for example A.

My table looks like this:


Solution

  • Try this code

    var total = 0;
    var rows = 15; // Set number of your actual rows
    
    for (var i = 1; i <= rows; i++) {
        var valueField = this.getField("Col1_" + i);
        var labelField = this.getField("Col2_" + i);
    
        if (valueField && labelField && labelField.value === "A") {
            var num = parseFloat(valueField.value);
            if (!isNaN(num)) {
                total += num;
            }
        }
    }
    
    event.value = total;
    

    Here I assumed Numeric input fields are named like this Col1_1, Col1_2, ..., Col1_15 and Category label fields are named like this Col2_1, Col2_2, ..., Col2_15. And set your actual number of rows here var rows.