• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using a loop to compare LOTS of pairs of field values

Explorer ,
Jul 27, 2019 Jul 27, 2019

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

628

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2019 Jul 27, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 27, 2019 Jul 27, 2019

Copy link to clipboard

Copied

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;

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2019 Jul 27, 2019

Copy link to clipboard

Copied

Is the sum of fNum and bNum always 61?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

Hello @Bernd,

Yes, it is. I think you may be onto something that I haven't thought about, to streamline that code a LOT! What would I need to do to implement that knowledge?

Is it something like:

bNum = 61 - fNum;

Is it that simple?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

Yes, pretty much... Although again, fNum is a string, so try this:

bNum = 61 - Number(fNum);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

You can use this:

bNum = util.printf("%02d", 61 - fNum);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

try67 I'm getting closer I believe. Your tips about naming variables and keeping track of what type of variable I am working with/need at any one point is REALLY useful, so thanks very much.

Bernd Alheit Thank you so much for your input too, you've given me some really handy perspectives I wouldn't have thought of til you mentioned them.

To all:

Unfortunately, the now (vastly) simplified script I'm trying now still isn't working. The error returned now is that vBack.getField(b) is undefined.

I'm totally stumped, and just can't spot where I need to fill in the apparent gap in the code!

I feel it's close. and I'm getting very excited that it might work with just one or two more tweaks.

Here's the current version:

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 = util.printf("%02d", 61 - fNum);

var b = fLett + "." + String(bNum);

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

f.value = "j";

f.textColor = color.gray;

}

}

If anyone can point out the final stage, I will be eternally grateful,

All the best,

NathanG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

Where are you defining "vBack"? It has to be a reference to a Document object if you want to use the getField method.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

Hmm... Sadly it's not working yet.

I'm using the following line of code at document level:

var vBack = this.openDataObject("back.pdf");

but is there more to it than that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

Is the file you're opening disclosed?

Anyway, without seeing the full code it's hard to help you further with this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

LATEST

I'm afraid I don't know what disclosed means (yet!). I'll do some digging... THanks for the heads up

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2019 Jul 28, 2019

Copy link to clipboard

Copied

The f variable is already pointing to a Field object after this line:

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

So doing this later on is incorrect:

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

It needs to be:

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

It's important to keep track of the types of your variables.

A common way to do that is by the first letter in the variable name. If that letter is "n", for example, the variable is usually a Number.

If it's "b" it's usually a Boolean. If it's "c" (or "s") it's usually a String, etc.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2019 Jul 27, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines