Copy link to clipboard
Copied
Hi, I am struggling to find an elegant solution to a script I would like to run. Simply I have 21 check boxes (var a1 – var a21) that each correspond to a test result. At the end of the test I have a button action that inputs the all results that failed ie. those Checkboxes that aren’t ticked into a textbox. At current I have achieved using several text boxes and the following:
// checkboxes for test results
var a1 = this.getField(" cb1 ")
var a2 = this.getField(" cb2 ")
………
var a21 = this.getField(" cb21 ")
// text fields for failed results
var tb1 = this.getField(" Text1.0 ")
……….
var tb9 = this.getField(" Text1.8 ")
// text fields that store pre-defined reults
var bb1 = this.getField(" bk1.0 ")
…………
var bb21 = this.getField(" bk1.20 ")
if(a1.value == "Off" && tb1.value==""){tb1.value = bb1.value;}
if(a2.value == "Off" && tb1.value==""){tb1.value = bb2.value;}
else if(a2.value == "Off" && tb2.value==""){tb2.value = bb2.value;}
if( a3 .value == "Off" && tb1 .value =="") { tb1 .value = bb3 .value;}
else if( a3 .value == "Off" && tb2 .value =="") { tb2 .value = bb3 .value;}
else if( a3 .value == "Off" && tb3 .value =="") { tb3 .value = bb3 .value;}
…….. etc all the way through to var a21.
Surely there is a better way to do this in one text box that just lists the failed results from “bb1 –bb21” if the “a1 – a21” aren’t checked?
Many thanks in advance.
Copy link to clipboard
Copied
Yes, there is a much easier way.
1) Get rid of the individual failed result fields (tb) and replace it with one tall multiline field named "FailedResult"
2) Rename the fields so they are easier to script.
The check boxes can be "cb.0", "cb.1", etc
Leave the predefined result field names. But "cb.0" needs to correspond to "bk1.0" and so forth
3) Use this code
// Get array of checkboxes
var aChecks = this.getField("cb").getArray();
// Initialize combined result text
var strFailedRslt = "";
// Loop over checks and build failed result text
var oDoc = this;
var cFldIdx, oRsltFld;
aChecks.forEach(function(oChk){
if(oChk.value == "Off"){
cFldIdx = oChk.name.split(".").pop();
oRsltFld = oDoc.getField("bk1." + cFldIdx);
strFailedRslt += oRsltFld.value + "\n";
}
});
// Set result text
this.getField("FailedResult").value = strFailedRslt;
This script uses the field grouping feature of Acrobat JavaScript in combination with paralell field naming so we can automatically relate one field to another through the name.
Copy link to clipboard
Copied
Yes, there is a much easier way.
1) Get rid of the individual failed result fields (tb) and replace it with one tall multiline field named "FailedResult"
2) Rename the fields so they are easier to script.
The check boxes can be "cb.0", "cb.1", etc
Leave the predefined result field names. But "cb.0" needs to correspond to "bk1.0" and so forth
3) Use this code
// Get array of checkboxes
var aChecks = this.getField("cb").getArray();
// Initialize combined result text
var strFailedRslt = "";
// Loop over checks and build failed result text
var oDoc = this;
var cFldIdx, oRsltFld;
aChecks.forEach(function(oChk){
if(oChk.value == "Off"){
cFldIdx = oChk.name.split(".").pop();
oRsltFld = oDoc.getField("bk1." + cFldIdx);
strFailedRslt += oRsltFld.value + "\n";
}
});
// Set result text
this.getField("FailedResult").value = strFailedRslt;
This script uses the field grouping feature of Acrobat JavaScript in combination with paralell field naming so we can automatically relate one field to another through the name.
Copy link to clipboard
Copied
Thom,
Thanks heaps! I knew there would be a much cleaner way to do it. I can honestly say I have no idea what a lot of those things do but I'm eager to try it out.
Again many thanks for your help.
Copy link to clipboard
Copied
Hi Thom, the above works perfectly. Is there any way to add an incrementing number to the return of each line? Ie.
"(1) Failed result 1
(2) Failed result 2 etc etc."
I tried playing around with the
= oRsltFld.value + "\n"; and could get it to return the right format but couldn't get the number to increment with each result.
Many thanks in advance.
Luke
Copy link to clipboard
Copied
try this modification:
// Get array of checkboxes
var aChecks = this.getField("cb").getArray();
// Initialize combined result text
var strFailedRslt = "";
// Loop over checks and build failed result text
var oDoc = this;
var cFldIdx, oRsltFld, nInc = 1;
aChecks.forEach(function(oChk){
if(oChk.value == "Off"){
cFldIdx = oChk.name.split(".").pop();
oRsltFld = oDoc.getField("bk1." + cFldIdx);
strFailedRslt += "(" + nInc + ") " + oRsltFld.value + "\n";
++nInc;
}
});
// Set result text
this.getField("FailedResult").value = strFailedRslt;
Copy link to clipboard
Copied
Thanks again Thom, you are a legend. Works perfectly. I tried to also get it to convert to ASCII so I could increment in different formats ie. (A), (B)..... etc but it doesn't seem possible?
Copy link to clipboard
Copied
Yes, letters can be used. But conversion to ASCII requires the correct codes.
To start at "A", initialize the increment from the char code for "A":
nInc = "A".charCodeAt(0);
then to display the letter, convert back to the char code.
strFailedRslt += "(" + String.fromCharCode(nInc)+ ") " + oRsltFld.value + "\n";
Copy link to clipboard
Copied
You. Are. A. Legend! This is perfect, many thanks for your time. I had the first part but couldn't get it back to characters.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more