Need help with some code to fill in and save forms to different folders

Explorer ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

!

So, I'm trying to fill in a form I made with information in a txt file and then save it to one of a number of folders according to information in the form. I have a series of folders setup named after number ranges (i.e. 1-49, 50-99, 100-149, etc.) and some of the folders are preceded by the letter "z" because our numbering system for what we're tracking with these forms includes some names formatted with a Z at the end ( the people here before me kinda suck).

The logic of the code I'm trying to correct is such (if you just want to see the code it's below):

import text data within a while loop -> use indexOf to search the target field value for the letter Z -> use switch to setup 2 blocks of code, 1 for values that contain "z" and 1 for values without "z" -> determine which code block to run based on the "z"  -> use substr(0,3) to pull the first three characters from the value and use that in the code blocks to compare to our number ranges to see which range the number falls in -> set a variable with a string according to which range it falls in (i.e. "100-149/") -> go to save the PDF -> use the previously set variable to amend the output directory of saveAs to which ever range it switched our variable to and save the PDF in the corresponding folder named as the number range -> increment a variable and repeat the loop

My problem now is that the variable we're setting with the switch code is coming out as just a zero. So instead of adding my string and making it create the PDFs in the correct subfolders, it just adds a 0 to the beginning of the file name and saves it in the top level folder. I thought I had my logic in the switch section correct but maybe I'm just not familiar enough with JS to see my issue.

If there's a better method/place of posting the code so it stays formatted like it is in the editor just let me know.

Code:

ImportES = app.trustedFunction(function() {


var fileName = "/c/Users/jcoleman/Desktop/Endless Slings/Endless Sling Data.txt";
var outputDir = "/c/Users/jcoleman/Desktop/Endless Slings/";
var whichCert = this.getField("PFFSID").value;
var firstThree = whichCert.substr(0,3);
var whichFolder = "";
var whichCase = whichCert.indexOf("Z");

var err = 0;
var idx = 0;
while (err == 0) {
err = this.importTextData(fileName, idx);

switch (whichCase) {
 
case 3: {
  if (0 < firstThree && firstThree <49)
   whichFolder = "z1-49/";
  else if (49 < firstThree && firstThree <99)
   whichFolder = "z50-99/";
  else if (99 < firstThree && firstThree <149)
   whichFolder = "z100-149/";
  else if (149 < firstThree && firstThree <199)
   whichFolder = "z150-199/";
  else if (199 < firstThree && firstThree <249)
   whichFolder = "z200-249/";
  else if (249 < firstThree && firstThree <299)
   whichFolder = "z250-299/";
  else if (299 < firstThree && firstThree <349)
   whichFolder = "z300-349/";
  else if (349 < firstThree && firstThree <399)
   whichFolder = "z350-399/";
  else if (399 < firstThree && firstThree <449)
   whichFolder = "z400-449/";
  else if (449 < firstThree && firstThree <499)
   whichFolder = "z450-499/";
  else if (499 < firstThree && firstThree <549)
   whichFolder = "z500-549/";
  else if (549 < firstThree && firstThree <599)
   whichFolder = "z550-599/";
  else if (599 < firstThree && firstThree <649)
   whichFolder = "z600-649/";
  else if (649 < firstThree && firstThree <699)
   whichFolder = "z650-699/";
  else if (699 < firstThree && firstThree <749)
   whichFolder = "z700-749/";
  else if (749 < firstThree && firstThree <799)
   whichFolder = "z750-799/";
  else if (799 < firstThree && firstThree <849)
   whichFolder = "z800-849/";
  else if (849 < firstThree && firstThree <899)
   whichFolder = "z850-899/";
  else if (899 < firstThree && firstThree <949)
   whichFolder = "z900-949/";
  else if (949 < firstThree && firstThree <999)
   whichFolder = "z950-999/";
}
 
  break;

case -1: {
  if (0 < firstThree && firstThree <49)
   whichFolder = "1-49/";
  else if (49 < firstThree && firstThree <99)
   whichFolder = "50-99/";
  else if (99 < firstThree && firstThree <149)
   whichFolder = "100-149/";
  else if (149 < firstThree && firstThree <199)
   whichFolder = "150-199/";
  else if (199 < firstThree && firstThree <249)
   whichFolder = "200-249/";
  else if (249 < firstThree && firstThree <299)
   whichFolder = "250-299/";
  else if (299 < firstThree && firstThree <349)
   whichFolder = "300-349/";
  else if (349 < firstThree && firstThree <399)
   whichFolder = "350-399/";
  else if (399 < firstThree && firstThree <449)
   whichFolder = "400-449/";
  else if (449 < firstThree && firstThree <499)
   whichFolder = "450-499/";
  else if (499 < firstThree && firstThree <549)
   whichFolder = "500-549/";
  else if (549 < firstThree && firstThree <599)
   whichFolder = "550-599/";
  else if (599 < firstThree && firstThree <649)
   whichFolder = "600-649/";
  else if (649 < firstThree && firstThree <699)
   whichFolder = "650-699/";
  else if (699 < firstThree && firstThree <749)
   whichFolder = "700-749/";
  else if (749 < firstThree && firstThree <799)
   whichFolder = "750-799/";
  else if (799 < firstThree && firstThree <849)
   whichFolder = "800-849/";
  else if (849 < firstThree && firstThree <899)
   whichFolder = "850-899/";
  else if (899 < firstThree && firstThree <949)
   whichFolder = "900-949/";
  else if (949 < firstThree && firstThree <999)
   whichFolder = "950-999/";
}
}


if (err == -1)
  app.alert("Error: Cannot Open File");
else if (err == -2)
  app.alert("Error: Cannot Load Data");
else if (err == 1)
  app.alert("Warning: User Cancelled File Select");
else if (err == 2)
  app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
  app.alert("Warning: Missing Data");
else if (err == 0) {
  this.saveAs(outputDir + whichFolder + "ES-" + this.getField("PFFSID").value + ".pdf");
  idx++;
}
}
});

TOPICS
Acrobat SDK and JavaScript, Windows

Views

319

Likes

translate

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

correct answers 1 Correct Answer

Adobe Community Professional , Nov 09, 2018 Nov 09, 2018
Don't declare them in the loop, but if the "PFFSID"  field is being loaded with "importTextData()" function, then any operations on that field need to be after the import function.In your code the "firstThree" and "whichCase" values never change. They are set before the loop begins.

Likes

translate

Translate

Translate
Most Valuable Participant ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

Change this line:

var firstThree = whichCert.substr(0,3);

To:

var firstThree = Number(whichCert.substr(0,3));

Likes

translate

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 ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

I made the change and added an app.alert just to popup the value of firstThree after I import the data and it's still saying it's 0 and adding it to the beginning of the file name. Though I'm not sure if that's a valid way to check that because like I said I'm not too js savvy yet.

Likes

translate

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
Adobe Community Professional ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

Is the value of the "PFFSID" field imported from the text file?  Because the code for extracting the data from it is not in the loop where the data is imported.

And your error detection is not in the correct location. If the code returns a non-zero value the rest of the code still executes. Place the import function into the while statement like this:

while (0 == this.importTextData(fileName, idx)

{

...

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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 ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

I'm not picking up what you're putting down in the first bit there. Are you saying I need to declare the variables within the loop or is there some bit I'd need to add in order to make sure it gets defined in the loop?

Edit: Forgot to answer you though, I'm using getField to pull the data from the form field. Could that be an issue?

Edit2: I tried defining the variables in the loop and it failed to run. Pretty sure that was a silly idea.

Likes

translate

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
Adobe Community Professional ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

Don't declare them in the loop, but if the "PFFSID"  field is being loaded with "importTextData()" function, then any operations on that field need to be after the import function.

In your code the "firstThree" and "whichCase" values never change. They are set before the loop begins.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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 ,
Nov 12, 2018 Nov 12, 2018

Copy link to clipboard

Copied

Edit: Nevermind, I fixed it! In the midst of trying to sort out the question I had below I came across another issue. Since I will have fields value like 1z, 11z, and 111z I was going to need either separate case blocks to handle those cases where the first 3 characters aren't just numbers (which I've done and it's all working perfectly!) or I will need to evaluate how many char each value is and simply substr out the last char with another else if statement if the length is shorter than expected before the whichFolder block is ran. I might try to get the second way working because it will condense the code a bit.

Thanks for all the help, I think I've got a much better grasp of js syntax and structure now. Expect more of me around here though, haha!

I can't believe it took me an hour this morning to figure out what the heck it was you were saying, I feel like a total tool, haha. Thank you for the help, Thom! It's passing the values through fine for the most part now, however I am having a different issue if you'd be so kind to offer one more bit of advice.

In my firstThree variable I'm just pulling the first three characters of the PFFSID field to shave off any letters at the end to then be compared in my switch statement. However, I hadn't even though about it until just now but some of these values are less than three numbers and then a letter so it's holding on to the letter and my else if statements never evaluate to true so it doesn't change the whichFolder variable and places that created file in the previously set folder option.

I thought maybe I could have it check that the else if comparisons never evaluated out to true and then just .slice (I think that's the proper one, yeah?) off the last character and run it back through the else if block. Or if you've any better ideas I'd love to give them a whirl.

Likes

translate

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
Adobe Community Professional ,
Nov 09, 2018 Nov 09, 2018

Copy link to clipboard

Copied

You should also be using the console window for debug.  It's much better than using app.alert()

You'll find a tutorial on the topic here:

Free Video Content & Full Listing of Available Videos

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

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