These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

SUMIF Equivalent

JimBeam
Registered: Feb 10 2009
Posts: 6
Answered

Is there a SUMIF equivalent in Acrobat?

What I'm looking to do is get a sum of the "B" fields (b_1, b_2, b_3,...b_52) where the code in the corresponding "A" fields (a_1, a_2, a_3,...a_52) matches x, y or z. I just need a total of the amounts in "B" separated by their coding in "A". Is this possible?

Something like this would work in excel (three separate expressions):
=SUMIF(A$2:A$52, “X”, B$2:B$52)
=SUMIF(A$2:A$52, “Y”, B$2:B$52)
=SUMIF(A$2:A$52, “Z”, B$2:B$52)

Thanks in advance for any help you can give.

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
No, you but you can write a JavaScript function or a couple of functions to perform that task.

A possilbe solution is:
//document level funcitonfunction SUMIF(aRange, sCriteria, aSum_Range) {/* sumif functionInputs:

aRange - array of field name for comparison

sCriteria - value to match each element of aField1

aSum_Range - array of field names to sum if a match to aField1 element

Returns summed value

*/
var fSum = 0; // summed value// loop through the aRange namesfor(i = 0; i < aRange.length; i++) {// test for sumif(this.getField(aRange[i]).value.toString() == sCriteria) {fSum = fSum + Number(this.getField(aSum_Range[i]).value)} // if criteria matched} // end loop thropugh aRangereturn fSum;} // end SUMIF funciton// end of doucment level function // custom calculation script for a fieldvar aIn = new Array("A.0", "A.1", "A.2", "A.3", "A.4");var aSum = new Array("B.0", "B.1", "B.2", "B.3", "B.4");event.value = SUMIF(aIn, "X", aSum);

George Kaiser

JimBeam
Registered: Feb 10 2009
Posts: 6
Thanks. I'm no coder, but I was able to cobble this together last night from a bunch of earlier posts I found:

var sum = 0; var f1 = this.getField("A.1").value;var f2 = this.getField("A.2").value;var f3 = this.getField("A.3").value;var f4 = this.getField("A.4").value;var f5 = this.getField("A.5").value; var v1 = this.getField("B.1").value;var v2 = this.getField("B.2").value;var v3 = this.getField("B.3").value;var v4 = this.getField("B.4").value;var v5 = this.getField("B.5").value; if (f1 == "X") sum += v1;if (f2 == "X") sum += v2;if (f3 == "X") sum += v3;if (f4 == "X") sum += v4;if (f5 == "X") sum += v5; event.value = sum;

I figured that there would be a far more elegant way to do this, but I couldn't find any examples.

I used this code in three different fields, each time getting the sum of a different code from the A column by changing the f variable (f1 == "X"), (f1 == "Y"), (f1 == "Z").
JimBeam
Registered: Feb 10 2009
Posts: 6
gkaiseril wrote:
No, you but you can write a JavaScript function or a couple of functions to perform that task.A possilbe solution is:
//document level funcitonfunction SUMIF(aRange, sCriteria, aSum_Range) {/* sumif functionInputs:

aRange - array of field name for comparison

sCriteria - value to match each element of aField1

aSum_Range - array of field names to sum if a match to aField1 element

Returns summed value

*/
var fSum = 0; // summed value// loop through the aRange namesfor(i = 0; i < aRange.length; i++) {// test for sumif(this.getField(aRange[i]).value.toString() == sCriteria) {fSum = fSum + Number(this.getField(aSum_Range[i]).value)} // if criteria matched} // end loop thropugh aRangereturn fSum;} // end SUMIF funciton// end of doucment level function // custom calculation script for a fieldvar aIn = new Array("A.0", "A.1", "A.2", "A.3", "A.4");var aSum = new Array("B.0", "B.1", "B.2", "B.3", "B.4");event.value = SUMIF(aIn, "X", aSum);
Thanks! This worked like a charm!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
And is reusable for other fields as necessary.

George Kaiser