Skip to main content
nathang78560588
Inspiring
July 27, 2019
Question

Using a loop to compare LOTS of pairs of field values

  • July 27, 2019
  • 2 replies
  • 1158 views

Hello all, I really hope someone can help me with this issue. (I'm not a hugely well-educated javascripter, so please feel free to assume zero knowledge in your responses...)

(I'm running Acrobat DC)

I have a form that contains a LOT of fields. the form is called "front.pdf". The fields are arranged on it in 32 rows of sixty fields (I know: that's a LOT!). Each field is named as follows:

A.01, A.02, A.03, A.04 ... up to ... A.59, A.60

B.01, B.02, B.04 ... => ... B.59, B.60

The rows go up to Z, then AA, BB up to GG. (All rows go from 1–60)

There is another pdf form, called "back.pdf" that is attached (as an attachment) to the first. It contains the same amount of fields, named exactly the same way, the only difference on this one is that the rows run from 60 down to 1 rather than from 1 up to 60.

Each cell, on both forms, will have a value that is either empty: ("",) or the lowercase letter Z: ("z").

I'm trying to write a script that, when attached onMouseUp to a button on the parent pdf (front.pdf), will loop through all of the cells on front.pdf, and compare the value of the cell with the value of a cell on back.pdf, and if they are both the same, set the value of the cell on front.pdf to "j".

The pairs to compare need to be as follows:

Front Cell               Back Cell

A.01                       A.60

A.02                       A.59

A.03                       A.58

...and so on, so that when the number on front.pdf INCREASES, the number on back.pdf DECREASES, although the Row letter stays the same.

So far, I have come up with the following (please don't scoff: I'm new and trying ever so hard, lol):

(I have a document level script that opens the attached back.pdf, and puts it into a var called ("vBack".)

for (var i = 0 ; i < this.numFields-4 ; i++) {

var f = this.getField(this.getNthFieldName(i));

var fString = String(f);

var fStringArr = fString.split('.');

var fLett = fStringArr[0];

var fNum = fStringArr[1];

var bNum;

if (fNum.value == 01){

bNum.value = 60;}

if (fNum.value == 02){

bNum.value = 59;}

if (fNum.value == 03){

bNum.value = 58;}

if (fNum.value == 04){

bNum.value = 57;}

if (fNum.value == 05){

bNum.value = 56;}

if (fNum.value == 06){

bNum.value = 55;}

if (fNum.value == 07){

bNum.value = 54;}

if (fNum.value == 08){

bNum.value = 53;}

if (fNum.value == 09){

bNum.value = 52;}

if (fNum.value == 10){

bNum.value = 51;}

if (fNum.value == 11){

bNum.value = 50;}

if (fNum.value == 01){

bNum.value = 49;}

if (fNum.value == 01){

bNum.value = 48;}

if (fNum.value == 01){

bNum.value = 47;}

if (fNum.value == 01){

bNum.value = 46;}

if (fNum.value == 01){

bNum.value = 45;}

if (fNum.value == 01){

bNum.value = 44;}

if (fNum.value == 01){

bNum.value = 43;}

if (fNum.value == 01){

bNum.value = 42;}

if (fNum.value == 01){

bNum.value = 41;}

if (fNum.value == 01){

bNum.value = 40;}

if (fNum.value == 01){

bNum.value = 39;}

if (fNum.value == 01){

bNum.value = 38;}

if (fNum.value == 01){

bNum.value = 37;}

if (fNum.value == 01){

bNum.value = 36;}

if (fNum.value == 01){

bNum.value = 35;}

if (fNum.value == 01){

bNum.value = 34;}

if (fNum.value == 01){

bNum.value = 33;}

if (fNum.value == 01){

bNum.value = 32;}

if (fNum.value == 01){

bNum.value = 31;}

if (fNum.value == 01){

bNum.value = 30;}

if (fNum.value == 01){

bNum.value = 29;}

if (fNum.value == 01){

bNum.value = 28;}

if (fNum.value == 01){

bNum.value = 27;}

if (fNum.value == 01){

bNum.value = 26;}

if (fNum.value == 01){

bNum.value = 25;}

if (fNum.value == 01){

bNum.value = 24;}

if (fNum.value == 01){

bNum.value = 23;}

if (fNum.value == 01){

bNum.value = 22;}

if (fNum.value == 01){

bNum.value = 21;}

if (fNum.value == 01){

bNum.value = 20;}

if (fNum.value == 01){

bNum.value = 19;}

if (fNum.value == 01){

bNum.value = 18;}

if (fNum.value == 01){

bNum.value = 17;}

if (fNum.value == 01){

bNum.value = 16;}

if (fNum.value == 01){

bNum.value = 15;}

if (fNum.value == 01){

bNum.value = 14;}

if (fNum.value == 01){

bNum.value = 13;}

if (fNum.value == 01){

bNum.value = 12;}

if (fNum.value == 01){

bNum.value = 11;}

if (fNum.value == 01){

bNum.value = 10;}

if (fNum.value == 01){

bNum.value = 09;}

if (fNum.value == 01){

bNum.value = 08;}

if (fNum.value == 01){

bNum.value = 07;}

if (fNum.value == 01){

bNum.value = 06;}

if (fNum.value == 01){

bNum.value = 05;}

if (fNum.value== 01){

bNum.value = 04;}

if (fNum.value == 01){

bNum.value = 03;}

if (fNum.value == 01){

bNum.value = 02;}

if (fNum.value == 01){

bNum.value = 01;}

var fComp = fLett + "." + fNum;

var bComp = fLett + "." + bNum;

var b = fLett + "." + bNum;

if (this.getField(fComp).value == vBack.getField(bComp).value){

f.value = "j";

f.textColor = color.gray;

}

}

It's not working, and I'm at a loss.

If anyone can advise on what I need to do to tidy this up, or can point me in the direction of knowing WHY this is a bad way to do it, and that there might be a much better, simpler, cleaner way, I'm all ears, and will be incredibly grateful. Obsequiously so, in all probability...

Many thanks in advance for your willingness to share your time and your knowledge to help a beginner. It really IS appreciated.

NathanG

This topic has been closed for replies.

2 replies

Bernd Alheit
Community Expert
Community Expert
July 27, 2019

Why does you use the field object at line 3 and 4?

try67
Community Expert
Community Expert
July 27, 2019

If the values you're comparing to are strings then you have to place them within quotes, like this:

if (fNum.value == "01"){ 

bNum.value = 60;} 

Also, I don't know why you've duplicated the same comparison multiple times, each time assigning a different value to bNum, but of course that's a mistake. Only the last one of these comparisons is going to work, as they'll override each other.

Another mistake you've made is that you've defined an empty variable called bNum and then tried to access its value property. You don't need to do that. Just apply the value to the variable directly. Only fields (and literal objects) have a "value" property.

nathang78560588
Inspiring
July 28, 2019

Hey try67 thanks so much for this, I really appreciate your hints and tips, they've been really helpful.

You were right of course, and I  didn't complete my numbering properly (I've also put all those comparisons into a big "if/else if" statement, as I felt they really ARE all connected, for each iteration of the loop!). (Thanks for the tip about strings in quotes, and for the value thing to do with my bNum variable!

I've updated the script as much as I know how to, but unfortunately, it's now returning the error that one of the fields in this line:

if (this.getField(f).value == vBack.getField(b).value){

is undefined. (Full code supplied below.)

I'm afraid I don't know what to look at next? I feel I"m tantalisingly close to getting it to do what I want, as the code seems to run until right at the very end, but I thought I had done everything I could to make all the bits talk to each other. Seemingly not...

Any ideas?

Thanks again,

NathanG

for (var i = 0 ; i < this.numFields-4 ; i++) {

var f = this.getField(this.getNthFieldName(i));

var fString = String(f);

var fStringArr = fString.split('.');

var fLett = fStringArr[0];

var fNum = fStringArr[1];

var bNum;

if (fNum == "01"){

bNum = "60";

}

else{

if (fNum == "02"){

bNum = "59";

}

else{

if (fNum == "03"){

bNum = "58";

}

else{

if (fNum == "04"){

bNum = "57";

}

else{

if (fNum == "05"){

bNum = "56";

}

else{

if (fNum == "06"){

bNum = "55";

}

else{

if (fNum == "07"){

bNum = "54";

}

else{

if (fNum == "08"){

bNum = "53";

}

else{

if (fNum == "09"){

bNum = "52";

}

else{

if (fNum == "10"){

bNum = "51";

}

else{

if (fNum == "11"){

bNum = "50";

}

else{

if (fNum == "12"){

bNum = "49";

}

else{

if (fNum == "13"){

bNum = "48";

}

else{

if (fNum == "14"){

bNum = "47";

}

else{

if (fNum == "15"){

bNum = "46";

}

else{

if (fNum == "16"){

bNum = "45";

}

else{

if (fNum == "17"){

bNum = "44";

}

else{

if (fNum == "18"){

bNum = "43";

}

else{

if (fNum == "19"){

bNum = "42";

}

else{

if (fNum == "20"){

bNum = "41";

}

else{

if (fNum == "21"){

bNum = "40";

}

else{

if (fNum == "22"){

bNum = "39";

}

else{

if (fNum == "23"){

bNum = "38";

}

else{

if (fNum == "24"){

bNum = "37";

}

else{

if (fNum == "25"){

bNum = "36";

}

else{

if (fNum == "26"){

bNum = "35";

}

else{

if (fNum == "27"){

bNum = "34";

}

else{

if (fNum == "28"){

bNum = "33";

}

else{

if (fNum == "29"){

bNum = "32";

}

else{

if (fNum == "30"){

bNum = "31";

}

else{

if (fNum == "31"){

bNum = "30";

}

else{

if (fNum == "32"){

bNum = "29";

}

else{

if (fNum == "33"){

bNum = "28";

}

else{

if (fNum == "34"){

bNum = "27";

}

else{

if (fNum == "35"){

bNum = "26";

}

else{

if (fNum == "36"){

bNum = "25";

}

else{

if (fNum == "37"){

bNum = "24";

}

else{

if (fNum == "38"){

bNum = "23";

}

else{

if (fNum == "39"){

bNum = "22";

}

else{

if (fNum == "40"){

bNum = "21";

}

else{

if (fNum == "41"){

bNum = "20";

}

else{

if (fNum == "42"){

bNum = "19";

}

else{

if (fNum == "43"){

bNum = "18";

}

else{

if (fNum == "44"){

bNum = "17";

}

else{

if (fNum == "45"){

bNum = "16";

}

else{

if (fNum == "46"){

bNum = "15";

}

else{

if (fNum == "47"){

bNum = "14";

}

else{

if (fNum == "48"){

bNum = "13";

}

else{

if (fNum == "49"){

bNum = "12";

}

else{

if (fNum == "50"){

bNum = "11";

}

else{

if (fNum == "51"){

bNum = "10";

}

else{

if (fNum == "52"){

bNum = "09";

}

else{

if (fNum == "53"){

bNum = "08";

}

else{

if (fNum == "54"){

bNum = "07";

}

else{

if (fNum == "55"){

bNum = "06";

}

else{

if (fNum == "56"){

bNum = "05";

}

else{

if (fNum == "57"){

bNum = "04";

}

else{

if (fNum == "58"){

bNum = "03";

}

else{

if (fNum == "59"){

bNum = "02";

}

else{

if (fNum == "60"){

bNum = "01";

}

}}}}}}}}}}

}}}}}}}}}}

}}}}}}}}}}

}}}}}}}}}}

}}}}}}}}}}

}}}}}}}}}}

var b = fLett + "." + bNum;

if (this.getField(f).value == vBack.getField(b).value){

f.value = "j";

f.textColor = color.gray;

}

}

try67
Community Expert
Community Expert
July 28, 2019

vBack is defined in the document level scripts. When I had a standalone version of this script, just working in ONE single pair of fields, I used the same declaration, and it worked ok, but should I be declaring it here specifically?


It has to be declared in the same, or a higher context. For example, if you define it in a doc-level script, outside a function, then it should work.