Skip to main content
oboto
Known Participant
April 30, 2020
Answered

Calculation - Can I deactivate form fields from beeing counted?

  • April 30, 2020
  • 1 reply
  • 625 views

Hi,

 

I have a list of items and each item has a point value that is summed up in a "Total" calculation.

 

Is there a way that I can deactivate one or more items from beeing counted? Perfect would be a checkbox for each item, that I could click and the item would not be counted - without having to delete the item.

 

And if I uncheck the checkbox it would be counted again?

 

Tim

This topic has been closed for replies.
Correct answer try67

Yes, that is possible, using a script. You would need to iterate over the check-boxes and if they are ticked then you add the value of the corresponding text field to the total, and apply the total to the calculating field at the end of the process.

This is not too complicated, provided the names of the check-boxes and text fields are matching and consistent.

For example, if they are called CB1, Text1, CB2, Text2, etc. then you can use this code as the custom calculation script of the total field to do it (let's assume there are 20 such sets of fields):

 

var total = 0;
for (var i=1; i<=20; i++) {
	if (this.getField("CB"+i).valueAsString!="Off") total+=Number(this.getField("Text"+i).valueAsString);
}
event.value = total;

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 30, 2020

Yes, that is possible, using a script. You would need to iterate over the check-boxes and if they are ticked then you add the value of the corresponding text field to the total, and apply the total to the calculating field at the end of the process.

This is not too complicated, provided the names of the check-boxes and text fields are matching and consistent.

For example, if they are called CB1, Text1, CB2, Text2, etc. then you can use this code as the custom calculation script of the total field to do it (let's assume there are 20 such sets of fields):

 

var total = 0;
for (var i=1; i<=20; i++) {
	if (this.getField("CB"+i).valueAsString!="Off") total+=Number(this.getField("Text"+i).valueAsString);
}
event.value = total;
oboto
obotoAuthor
Known Participant
April 30, 2020

That sounds very promising.

Is it possible to handle it the other way around?

So the values are not counted when the checkbox is ticked?

Would I have to set valueAsString!=  to "On" ?

Thom Parker
Community Expert
Community Expert
April 30, 2020

You have the correct idea, but the default On value (i.e. export value) for a checkbox is "Yes".

However, the best strategy is to change the comparsion condition to 

 

valueAsString == "Off"

 

The reason for this is that "Off" is always the Off value for a checkbox. So this code will work, regardless of the actual export value from the checkbox. 

 

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