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

Custom Calculation Script

Explorer ,
Mar 30, 2023 Mar 30, 2023

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?

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF forms
2.5K
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 30, 2023 Mar 30, 2023

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

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 31, 2023 Mar 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
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
Explorer ,
Apr 03, 2023 Apr 03, 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;

 

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 ,
Apr 03, 2023 Apr 03, 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
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
Explorer ,
Apr 04, 2023 Apr 04, 2023

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

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 ,
Apr 04, 2023 Apr 04, 2023

To build the field names use the page number and the template name.

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
Explorer ,
Apr 04, 2023 Apr 04, 2023

Bernd Alheit,

I do not understand how to do that. 

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 ,
Apr 04, 2023 Apr 04, 2023
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
Explorer ,
Apr 05, 2023 Apr 05, 2023

Yeah I have still be unable to.

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
Explorer ,
Apr 11, 2023 Apr 11, 2023

I am using the below script for my calculation script: //

 

var fieldName = event.target.name;
var suffix = fieldName.substring(fieldName.lastIndexOf(".") + 1);
var RawGrossA = getField("RawGrossA." + suffix);
var RawTareA = getField("RawTareA." + suffix);
var RawPalletWeightA = getField("RawPalletWeightA." + suffix);

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

event.value = result;

 

I am using the below script for my button: //

 

var t = this.getTemplate("RawTally");
var XO = t.spawn(this.numPages, true, true);

 

The new forms on the new page are named P(n).RawTally.(Field Name).

 

This is a bit too complicated for myself. I can't get the custom calculation script to work on the new pages.

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 ,
Apr 12, 2023 Apr 12, 2023
LATEST

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
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