Copy link to clipboard
Copied
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)
1 Correct answer
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 = "";}}
Copy link to clipboard
Copied
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 = "";}}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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'.
Copy link to clipboard
Copied
Ohhhhhh! PERFECT. Where do you learn this stuff?!?! Need to learn.
Thank you - this worked (of course).
Copy link to clipboard
Copied
You'll find loads of tutorials and samples here:
https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
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-)
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Fair warning, maybe I'm missing something - fairly new at this!
Copy link to clipboard
Copied

