Skip to main content
Participant
March 23, 2024
Answered

Script to apply custom calculation to mulitple total textfields with input values (row/column )

  • March 23, 2024
  • 2 replies
  • 1911 views

Not sure if this is possible.

Have a form with text fields:

HoursRow1 RateRow1 TotalRow1

HoursRow2 RateRow2 TotalRow2

...with many more rows (hierarchical)

I want to write a script that will apply if else function for each TotalRow with the corresponding row number (use HoursRow1 and RatesRow1 for TotalRow1, etc)

It’s easy enough for one Total, add custom calculations:

var tot = this.getField("TotalRow1").value;

var hour = this.getField("HoursRow1").value;

var cost = this.getField("RateRow1").value;

    if (hour && cost) {

          event.value = hour * cost;             

    }

     else

       {

        event.value = "";

    }

But there are a lot of rows, so it would be a lot easier (if I knew how) to put this in a script to not have to enter each TotalRow manually. Put some type of wildcard in place , plus dealing with more than one variable, but move through so the right variables are put in for corresponding total

(TotalRow1 with HoursRow1 and RateRow1, TotalRow2 with HoursRow3 and RateRow3, etc)

Do not see how to do this with needing the check all three (Total, Hours, and Rate)

This topic has been closed for replies.
Correct answer Nesa Nurani

You can do it using loop, just replace red X with number of rows you have (for example: if your last fields are named "TotalRow12" then replace X with 12):

for( var i=1; i<=X; i++){
var tot = this.getField("TotalRow"+i);
var hour = Number(this.getField("HoursRow"+i).valueAsString);
var cost = Number(this.getField("RateRow"+i).valueAsString);

if(hour && cost){
tot.value = hour * cost;}
else{
tot.value = "";}}

2 replies

bebarth
Community Expert
Community Expert
March 24, 2024

Hi,

If you don't konw exactly the number of fields or if all indices are not consecutive, you can directly write this script which will find the indice:

 

var ind=event.target.name.substr("TotalRow".length);
var hour=this.getField("HoursRow"+ind).value;
var cost=this.getField("RateRow"+ind).value;
if (hour && cost) event.value=hour*cost;             
else event.value="";

 

 

You can also use a function in document level:

 

function myFunction(ind) {
	var hour=this.getField("HoursRow"+ind).value;
	var cost=this.getField("RateRow"+ind).value;
	if (hour && cost) return hour*cost;             
	else return "";

}

 

and call it with this script:

 

event.value=myFunction(event.target.name.substr("TotalRow".length));

 

 

And if you have a lot of " TotalRow" fields, you can write the call script in all these fields runing this following script from the console window:

 

for (var i=0; i < this.numFields; i++) {
	if (this.getNthFieldName(i).indexOf("TotalRow")==0) this.getField(this.getNthFieldName(i)).setAction("Calculate","event.value=myFunction(event.target.name.substr(\"TotalRow\".length));");;
}

 

That's all folks

8-)

Participant
March 24, 2024

Hi

Like Nesa, this works great when you have the document open, run the script to calculate the values, but I wanted to "auto populate" the TotalRow fields with the formula HoursRow * RateRow (with the appropriate number). Script to put the formula in each individual field Calculate -> Custom calculation

With the method you posted, you would have to run the script each time.  I would like to be able to hand over the PDF to someone who does not have Acrobat, just Adobe Reader, and be able to enter the hours/rate and get the corresponding total

Participant
March 24, 2024

Fair warning, maybe I'm missing something - fairly new at this!

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
March 24, 2024

You can do it using loop, just replace red X with number of rows you have (for example: if your last fields are named "TotalRow12" then replace X with 12):

for( var i=1; i<=X; i++){
var tot = this.getField("TotalRow"+i);
var hour = Number(this.getField("HoursRow"+i).valueAsString);
var cost = Number(this.getField("RateRow"+i).valueAsString);

if(hour && cost){
tot.value = hour * cost;}
else{
tot.value = "";}}

Participant
March 24, 2024

Thank you Nesa - this works to calculate the value (once values are put in). However, I wanted to be able to place the script into the console and have it put the "formula" into each indivial Calculate -> Custom calculation script, to avoid having to manually entering the script for each TotalRow

 

Nesa Nurani
Community Expert
Community Expert
March 24, 2024

You don't need a script in each TotalRow field, the script I gave you will calculate all TotalRow fields, just place it in one of the fields as 'Custom calculation script'.