Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Mar 23, 2024 Mar 23, 2024

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)

TOPICS
Acrobat SDK and JavaScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 24, 2024 Mar 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 = "";}}

Translate
Community Expert ,
Mar 24, 2024 Mar 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 = "";}}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2024 Mar 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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 24, 2024 Mar 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'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2024 Mar 24, 2024

Ohhhhhh! PERFECT. Where do you learn this stuff?!?! Need to learn.

Thank you - this worked (of course).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 24, 2024 Mar 24, 2024
LATEST

You'll find loads of tutorials and samples here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 24, 2024 Mar 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-)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2024 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2024 Mar 24, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 24, 2024 Mar 24, 2024
quote

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


By Laura356523064few

Certainly!

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines