6 Replies Latest reply on Jan 16, 2016 8:23 AM by MatLac

    variable in for loop changes by itself

    MatLac Level 1

      I'm trying to replicate the behavior in excel where you can add a blank line to a spreadsheet.  I have a form looking like a spreadsheet where I named my fields according to their line number.  I have a single field where the user inputs the line number where he wants a line to be added and a button to trigger the action.  I created a script that is composed of 2 For Loops inside one another.  The first loop goes through the lines while the second one goes through each individual field of a line.  The goal is to take each value starting from the second to last line and move it to the same field, on the next line.  Value of variable line is 3.  Here is what I came up with:

       

      Sans titre.png

      I inserted a bunch of println to see what was wrong with my code.  Notice that only the first 3 lines are filled so I pasted only the console refering to those lines.  For some reason, as soon as there is a value inside a cell, the variable i switches to 0.  Here is the result:

       

      myData is

      NATURE.3  pasted

      CI.3 copied

      myData is

      CI.3  pasted

      i is 2

      TIME.CALL.2 copied

      myData is

      TIME.CALL.2  pasted

      TIME.1018.2 copied                                      //This is the stange part.  As soon as I hit a value, i switches to 0 when it should stop at 2 since the value of line is 3

      myData is 0900                                            //myData shows the correct value

      TIME.1018.0 0900 pasted                             //TIME.1018.0 should be TIME.1018.2 althought the correct value is pasted to the correct field (strangely)

      TIME.END.0 copied                                     //i switched to 0, skipping 1 in the loop althought it should not even be less than 2

      myData is 0730

      TypeError: this.getField("OPE004.RAQ." + aFields[j] + String(i + 1)) is null 

      29:Field:Mouse Up

        • 1. Re: variable in for loop changes by itself
          MatLac Level 1

          This should be the order for the fields

           

          var aFields = ["TIME.CALL.", "TIME.1018.", "TIME.END.", "TYPE.", "LIEU.", "NATURE.", "CI."];

           

          Also, it should be noticed that if I remove the "pasting" part of my code, it scans correctly throught the values from line 7 to line 3 just as requested.

          • 2. Re: variable in for loop changes by itself
            Karl Heinz Kremer Adobe Community Professional

            To find out what the error message is actually complaining about, break that one line up into three:


            var fieldName = "OPE004.RAQ." + aFields[j] + String(i + 1);
            
            var f = this.getField(fieldName);
            if (f != null) {
                f.value = myData;
            }
            else {
                console.println("ERROR: No field with name " + fieldName);
            
            }
            


            This way, you will get an error message with the field name the script is trying to access. Chances are that something in that name is wrong.

            • 3. Re: variable in for loop changes by itself
              MatLac Level 1

              my problem doesn't reside in an unexisting field.  I understand what you mean.  I tried what you said and it shows the field its trying to reach is named OPE004.RAQ.whatever.01 which should be impossible since the identation goes from 7 to 1.  Why does a leading 0 appears inside the loop.

               

              I tried going another way about it by puting each values in an array from left to right, then top to bottom, just as you read it, I then reset the original field and reapply all the data starting with the second line.  But I get stuck with the same bug.  Althought the loop goes from line 1 to 7 (i starts at 1 and ++ until 7), as soon as I reach a non null value from my array and copy it to the right field, i becomes 0 and the rest of the data gets scattered everywhere.  HOW CAN i BE 0??????  It goes from 1 to 7?

               

              Sans titre.jpg

              Here are the alerts I get:

              Sans titre.jpg

              perfect

              Sans titre.jpg

              its an empty field so no value is shown

              Sans titre.jpg

              i is still 2, that's ok

              Sans titre.jpg

              the value in the second field from the same line is 0730, that still correct

              Sans titre.jpg

              Wait, what?  How can i be 0, We are not even done with the first run through the inside loop?  What is wrong?  And then everything goes wrong and i keeps switching between a value of 0 and a value of 1 in a loop that never stops.  I need to ctrl+alt+del myself out of it.

              • 4. Re: variable in for loop changes by itself
                try67 MVP & Adobe Community Professional

                Since the i variable is not defined inside the definition of the for-loop I would recommend that you use a variable name that isn't already used somewhere else in the script, as there might be scope issues.

                • 5. Re: variable in for loop changes by itself
                  Bernd Alheit Adobe Community Professional & MVP

                  Do you use any code in the text fields?

                  • 6. Re: variable in for loop changes by itself
                    MatLac Level 1

                    You were right.  I changed the name of the variable and everything is fine now.  Seams it was a scope issue.