Skip to main content
Known Participant
March 30, 2023
Question

Custom Calculation Script

  • March 30, 2023
  • 4 replies
  • 2407 views

I have a page that has custom calculation scripts. I need that page duplicated an unknown amount of times. It could be once or fifty times. However when I duplicate it, the custom calculation scripts dont change when the forms are automatically renamed.

So the script attached to form A on original page, is the same script for form A on the duplicate page even though the page 2 form has a new name.

Is there a way to have the script automatically change with the new name of the form field?

This topic has been closed for replies.

4 replies

JR Boulay
Brainiac
April 12, 2023

As said above, you must manage prefixes too.

Try this (not tested):

 

var fieldName = event.target.name.;
var aFieldName = fieldName.split(".");
var suffix = aFieldName.pop();
var prefix = aFieldName.shift();
prefix.slice(1);
var RawGrossA = getField("P" + prefix + ".RawGrossA." + suffix);
var RawTareA = getField("P" + prefix + ".RawTareA." + suffix);
var RawPalletWeightA = getField("P" + prefix + ".RawPalletWeightA." + suffix);

var result = RawGrossA.value - RawTareA.value - RawPalletWeightA.value;

event.value = result;

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Brainiac
April 3, 2023

You need to detect the P number and put it in a variable, for example :

 

var sPrefix = "P" + this.pageNum-1;
var v1 = getField(sPrefix + ".RawGross.0").value;
var v2 = getField(sPrefix + ".RawTare.0").value;
var v3 = getField(sPrefix + ".RawPalletWeight.0").value
event.value = v1 - v2 - v3;

 

Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
April 4, 2023

I tried the script you mentioned, and it didnt work. I have attached the document I am working on.

Bernd Alheit
Brainiac
April 4, 2023
JR Boulay
Brainiac
March 31, 2023

Assuming that you are spawning templates, you should use the bRename parameter as true, then handle the prefix in the field names.

Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
April 3, 2023

This is the script I am using to generate the template page.

 

var t = this.getTemplate("RawTally");
t.spawn({nPage: this.numPages, bRename: true, bOverlay: false});

 

The fields are all renamed as new fields, so that part works.

However, the scripts in the calculations, didn't change from page to page. So the field on the new page has "P4" in front of everything except in the calculation script.

 

var v1 = getField("RawGross.0").value;
var v2 = getField("RawTare.0").value;
var v3 = getField("RawPalletWeight.0").value

event.value = v1 - v2 - v3;

 

If the script would have auto changed to the below, it would work.

 

var v1 = getField("P4.RawGross.0").value;
var v2 = getField("P4.RawTare.0").value;
var v3 = getField("P4.RawPalletWeight.0").value

event.value = v1 - v2 - v3;

 

Bernd Alheit
Brainiac
March 31, 2023

Change the script such that it can handle the new names.