Skip to main content
Participant
July 7, 2023
Answered

Limit a calculateNow script to a single page or pages?

  • July 7, 2023
  • 2 replies
  • 558 views

Currently I am using the following script I found by Thom Parker to create a button that toggles automatic calculations on and off:

 

if(event.target.borderStyle == border.b){
// Perform Push Down Actions
event.target.borderStyle = border.i;
event.target.buttonSetCaption("Turn Calculations On");
// Darken Down Colors
event.target.textColor = ["RGB", 0.8, 0.8, 0.4];
event.target.fillColor = ["RGB", 0, 0, 0.7];
// Turn Calculations Off
this.calculate = false;
}else{
// Perform Pop Up Actions
event.target.borderStyle = border.b;
event.target.buttonSetCaption("Turn Calculations Off");
// Restore Up Colors
event.target.textColor = ["RGB", 1, 1, 2/3];
event.target.fillColor = ["RGB", 0, 0, 1];
// Turn Calculations On
this.calculate = true;
this.calculateNow();

 

Would it be possible to limit turning the automatic calculations on and off to a specific page or range of pages? I have over 600 radio buttons and limiting the calculation to a single page and/or a range of pages would really speed things up! Thank you for your help!

This topic has been closed for replies.
Correct answer Thom Parker

There are a few ways to handle too many calculations. 

1. Only use calculation scripts were necessary. 

2. Setup the form scripts so calculations aren't causing looping cascades.   

3. Qualify calculations so they aren't being performed unnecessarily.  The event.source property is the field that triggered the calculation. It is null on a form reset and when importing data. 

4. A more controlled version of #3 is to put multiple calculations into a single script (as suggested by JR), then use event.source to selectively execute only the calculations that are necessary. You could divide calculations per page by putting all the calcs for a single page into a text field on that page, then use this code to qualify it.

 

if(!event.source || (event.target.page == event.source.page))

{

}

 

This only works if target and source are really on the same page, and neither field has more then one instance. 

 

 

2 replies

JR Boulay
Community Expert
Community Expert
July 7, 2023

The answer is no.
To speed things up, there is another method: instead of putting calculation operations in a multitude of fields, it's better to put all the calculations (for a page, for example) in a single field, possibly hidden.

 

This also has the advantage of making modifications and maintenance easier, because all the calculations are in the same place.

Acrobate du PDF, InDesigner et Photoshopographe
CassadyCAuthor
Participant
July 7, 2023

Thank you!

 

How would you then reference the answers to the single calculation? For example, on a page I have 15 groups of radio buttons divided into three columns (five groups per column). Each of these five groups is then averaged (so three averages). What takes so long is that any time a button is clicked, it recalculates all averages. This was partly solved by turning calculations on/off. How can I combine those three averages into one calculation while still showing their individual outputs?

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
July 7, 2023

There are a few ways to handle too many calculations. 

1. Only use calculation scripts were necessary. 

2. Setup the form scripts so calculations aren't causing looping cascades.   

3. Qualify calculations so they aren't being performed unnecessarily.  The event.source property is the field that triggered the calculation. It is null on a form reset and when importing data. 

4. A more controlled version of #3 is to put multiple calculations into a single script (as suggested by JR), then use event.source to selectively execute only the calculations that are necessary. You could divide calculations per page by putting all the calcs for a single page into a text field on that page, then use this code to qualify it.

 

if(!event.source || (event.target.page == event.source.page))

{

}

 

This only works if target and source are really on the same page, and neither field has more then one instance. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
July 7, 2023

Following is possible:

Add a button on the page and use all calculations of the page at this button.