Copy link to clipboard
Copied
Need help please...
I have 4 check boxes with values of 0,1,5,5 when I calculate and use 'maximum' it works, since I need to get "5" maximum score.
However, on another section, I have 5 check boxes with values of 0,1,1,1,1 where I can't use 'maximum', since I need to get "3" max score. I also can't use 'sum(+)', as it will exceed "3" max when four boxes with values of "1" are selected.
Copy link to clipboard
Copied
OK, then you can use something like this as the field's custom calculation script:
var fields = ["RF3-1", "RF3-2", "RF3-3", "RF3-4", "RF3-5"];
var maxValue = 3;
var total = 0;
for (var i in fields) {
var v = this.getField(fields[i]).valueAsString;
if (v!="Off")
total+=Number(v);
}
event.value = Math.min(total, maxValue);
If you need to use it multiple times I would recommend putting the code in a doc-level function, passing to it the fields array and the max value as parameters.
Edited: Sorry, it should be Math.min, not Math.max in the last line of code... Fixed it now.
Copy link to clipboard
Copied
What is the difference between using 5 as the maximum and using 3? I don't understand your issue.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
By "limit" do you mean prevent ticking the check-boxes, or just showing a maximum value in the calculation?
Copy link to clipboard
Copied
Not to prevent from ticking, but to limit max score of section to "5" regarless of ticking 1 and 5 for example.
Copy link to clipboard
Copied
Are you trying to perform a calculation (if so, which one), or to limit the selection in case a maximum value has been reached? Your question is not very clear.
Copy link to clipboard
Copied
I need section 1's max score to be just "5" and section 3's max score to be just "3", then normally add them with the rest of the sections.
Copy link to clipboard
Copied
So just create a (hidden) text field for the total of each group (using the "maximum" option in the Calculate tab), then add up those total fields for the grand total.
Copy link to clipboard
Copied
yes, mentioned that on my OP, that section 1 is fine to use "maximum" for calculation.
However doing the same method for section 3 does not work since the values there are 0,1,1,1,1. when they tick all 1's the score will remain 1, will not even add anything up.
Copy link to clipboard
Copied
I see what you mean, although that approach for section 1 is not a very good one.
What are the names of the fields in section 3, then?
Copy link to clipboard
Copied
agreed, the approach just happen to work given the circumstances. will not work anymore if values of boxes are limited to lower than "5".
I named section 3 fields as RF3-1, RF3-2, RF3-3, RF3-4, RF3-5 with values of 0,1,1,1,1 respectively.
Copy link to clipboard
Copied
OK, then you can use something like this as the field's custom calculation script:
var fields = ["RF3-1", "RF3-2", "RF3-3", "RF3-4", "RF3-5"];
var maxValue = 3;
var total = 0;
for (var i in fields) {
var v = this.getField(fields[i]).valueAsString;
if (v!="Off")
total+=Number(v);
}
event.value = Math.min(total, maxValue);
If you need to use it multiple times I would recommend putting the code in a doc-level function, passing to it the fields array and the max value as parameters.
Edited: Sorry, it should be Math.min, not Math.max in the last line of code... Fixed it now.
Copy link to clipboard
Copied
A W E S O M E ! ! !
used for both sections that needs max, it works like a gem! thank you so much.
Btw, is there a way to prevent computing delay when using invisible fields. Coz the visible Total Score show delay when clicking boxes on section 1 and 3 but not on other sections.
Copy link to clipboard
Copied
Check the fields calculation order. It's not related to them being invisible.
Copy link to clipboard
Copied
thanks! still have a delay, but that'll do it for today. will leave the rest some other time. Much appreciated.
Copy link to clipboard
Copied
used the formula you gave to fix the delay, using sum(+) causes delay but not with JS script. Thanks again!
var fields = ["RF1_Total", "RF3_Total", "RF2-1", "RF2-2", "RF2-3", "RF2-4", "RF4-1", "RF4-2", "RF4-3", "RF4-4", "RF5-1", "RF5-2", "RF5-3", "RF5-4", "RF6-1", "RF6-2"];
var maxValue = 50;
var total = 0;
for (var i in fields) {
var v = this.getField(fields[i]).valueAsString;
if (v!="Off")
total+=Number(v);
}
event.value = Math.min(total, maxValue);
Copy link to clipboard
Copied
In Prepare Form mode click More and there you will find the Set Fields Calc. Order command.
Copy link to clipboard
Copied
The simplest way to do this to create the first check box in the section (in this example, call it "SEC3"). Then in form editing mode, right-click and select "Create multiple copies" and select 1 across and 5 down. The field names will then be SEC3.0, SEC3.1 through SEC3.4. Add the export values. In the text field, calculation tab, select "Value is the maximum of the following fields" and only select "SEC3":
You can do the same thing for the subtotal fields. Make 1 and call it Subtotal, right click >create multiple copies, then in the grand total field select "Value is the sum of the following fields": 'Subtotal'.
Copy link to clipboard
Copied
Thank you, tried this way initially. But it doesn't work with section 3 since the values of 5 check boxes are 0,1,1,1,1. choosing max will show subtotal of "1" even though 4 boxes with values of 1 are ticked.
Copy link to clipboard
Copied
Section 3 should have "3" as max score even 4 boxes are ticked with values of "1" each.
Copy link to clipboard
Copied
I completely misunderstood what you were trying to accomplish.
Copy link to clipboard
Copied
no worries, have a great day ahead.