Skip to main content
Participant
May 9, 2023
解決済み

Can the check boxes calculate a percentage number of items completed in PDF?

  • May 9, 2023
  • 返信数 2.
  • 2806 ビュー

I have a simple list with checkboxes.

Is there a way to calculate a percentage complete based on the number of checkboxes were checked in one row?

If one checkbox is checked then it gives 25$

If two then 50%

If three then 75%

If all checked then 100%

このトピックへの返信は締め切られました。
解決に役立った回答 AhmedCOSTA

Then you need to hard-code them into the script, like this:

 

 

var fields = ["KW MB POP - Dr 1", "KS MB POP - Dr 1", "QT MB POP - Dr 1", "OM MB POP - Dr 1"];
var total = 0;
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="Off") {
		total++;
	}
}
event.value = total/fields.length;

 

[Edited: Code fixed]

 


SUPPER

It works great, just add the first two lines

 

var max = 4;
var total = 0;
var fields = ["KW MB POP - Dr 1", "KS MB POP - Dr 1", "QT MB POP - Dr 1", "OM MB POP - Dr 1"];
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="Off") {
		total++;
	}
}
event.value = total/fields.length;

返信数 2

Participating Frequently
September 4, 2024

Hello, I would like to ask for assistance on my matter, it's similar to AhmedCOSTA's. 
Calculate the percentage of the checkboxes. 
For example; I have 19 checkboxes in total and if all of them are marked as checked. I want the field to get the percentage of 100%.

I followed Try67's code but I got the result 1,163% despite not ticking any of the boxes. 

Nesa Nurani
Community Expert
Community Expert
September 5, 2024

I don't see where you declared 'total' variable?

Other than that it looks ok, although it could be done slightly different.

Participating Frequently
September 5, 2024

I see it now. Thank you, Nesa!
I made a mistake in not including the 'total' variable. 

Do you know how to keep that field blank if there's no checkboxes being ticked? 

Thanks again. 

try67
Community Expert
Community Expert
May 9, 2023

Sure. Let's say the fields are called Drink1 to Drink4. You can then use this code for that calculation:

 

var max = 4;
var total = 0;
for (var i=1; i<=max; i++) {
	if (this.getField("Drink"+i).valueAsString!="Off") {
		total++;
	}
}
event.value = total/max;

 

 

You'll also need to set the text field as having the Percentage format.

AhmedCOSTA作成者
Participant
May 9, 2023

Hi try67,

This is supper, Thanks alot.

But, what if the fields name is diffrent like:

KW MB POP - Dr 1

KS MB POP - Dr 1

QT MB POP - Dr 1

OM MB POP - Dr 1

try67
Community Expert
Community Expert
May 9, 2023

Then you need to hard-code them into the script, like this:

 

 

var fields = ["KW MB POP - Dr 1", "KS MB POP - Dr 1", "QT MB POP - Dr 1", "OM MB POP - Dr 1"];
var total = 0;
for (var i=0; i<fields.length; i++) {
	if (this.getField(fields[i]).valueAsString!="Off") {
		total++;
	}
}
event.value = total/fields.length;

 

[Edited: Code fixed]