简体   繁体   中英

Adobe Acrobat Pro DC custom calculation script advice

I'm trying to create a form with:

  1. a series of numerators and denominators to calculate
  2. if the field is left blank, it is not included in the calculation
  3. the calculation to be updated in real time, and not wait for each field to have a number in it before it calculates.

This is the only calculation in the form, so changing the calculation order is not used. In the document javascript, I have

this.calculateNow();

and in the custom calculation script for the field, I have

(function () { 

var v1 = +getField("Text1").value;
var v2 = +getField("Text2").value;
var v3 = +getField("Text3").value;
var v4 = +getField("Text4").value;
var v5 = +getField("Text5").value;
var v6 = +getField("Text6").value;
var v7 = +getField("Text7").value;
var v8 = +getField("Text8").value;
var v9 = +getField("Text9").value;
var v10 = +getField("Text10").value;

event.value = (v2 * v4 * v6 * v8 * v10) !== 0 ? ((v1 * v3 * v5 * v7 * v9) / (v2 * v4 * v6 * v8 * v10)) : "";
})();

The issues I'm having:

  1. that the calculation does not calculate in real time
  2. all fields still have to be entered for the calculation to occur.
  3. if zero is entered, the calculation will not run.

Your code is a bit repetitive. You may use an array containing the field ids:

const fields = Array.from({length:10}, (_,i)=>"Text"+(i+1));

Then we can map the fields to their value and use the or operator to fill unset fields:

const values = fields.map( f => +getField(f).value || 1 );

That array then can be reduced to a certain value, eg with mutliplication:

const result = values.reduce((a,b) => a*b)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM