Copy link to clipboard
Copied
I am trying to pull data from an excel file into a form or text box.
I have tried using importTextData, which works on a one-by-one basis, but can this be used to pull an attached file?
The end result that i'm hoping for (in case there is a better way to do this) is this:
My PDF will be a map with several points on it. I would like each point to be a clickable button/link, and when the user clicks on the point, either a text box pops up with additional info on that site, or there is a form/side panel that displays the additional info.
Copy link to clipboard
Copied
I am trying to pull data from an excel file into a form or text box.
I have tried using importTextData, which works on a one-by-one basis, but can this be used to pull an attached file?
The end result that i'm hoping for (in case there is a better way to do this) is this:
My PDF will be a map with several points on it. I would like each point to be a clickable button/link, and when the user clicks on the point, either a text box pops up with additional info on that site, or there is a form/side panel that displays the additional info.
Copy link to clipboard
Copied
You can do it using the getDataObjectContents method.
Copy link to clipboard
Copied
I saw some into on getDataObjectContents, but how do I make it look up the specific point that i want into for (each point has a unique ID) and output to the form fields?
Copy link to clipboard
Copied
You need to parse the file, searching for the desired data. I don't know the structure of your file so I can't really help beyond that...
However, this is not going to work with an Excel file (xls/xlsx/etc.). The file will have to be in plain-text format.
Copy link to clipboard
Copied
I have saved the file as a tab delimited txt file.
The end result will have more fields, but for my test I only have 3 fields: ID, City, and Employees. These fields are the same in my PDF as they are in my txt file.
I found the code online to run getDataObjectContents, i'm using
var stmData = this.getDataObjectContents("importdata.txt");
var strData = util.stringFromStream(stmData);
I run a app.alert(strData); to make sure the data is pulling, and it all comes up (I only have about 5 records for the text), but i'm not sure how to parse it to isolate the record that i'm looking for and fill the ID, City, and Employees fields?
Thanks for your help!
Copy link to clipboard
Copied
try67 wrote
The file will have to be in plain-text format.
Not necessarily. XSLX is just XML and you can parse the XML using the XMLData object. You'll need to get really good at using XPath... which I don't consider to be trivial... but it is possible. The documentation infers that XMLData is for XFA manipulation but it works with any XML as you can see in the examples for the object.
Copy link to clipboard
Copied
That's true. Thanks for the info!
Copy link to clipboard
Copied
I've made a bit of progress in parsing, using the following:
var nPos = strData.indexOf(num);
var nvalue = strData.substr(nPos+3,10);
this.getField("City").value=nvalue;
i'm able to have it search for an ID and post the first 10 characters after the ID into the city field
but there must be a way to have it go until the next tab instead of just saying "10 characters" right? because not all cities are 10 characters long.
There is a tab between my IDs, Cities, and Employees columns.
Copy link to clipboard
Copied
You can split each line with the split method, and the tab character ("\t")
as the delimiter.
It will return an array with all the values in the line.
Copy link to clipboard
Copied
You can use the split method on the text file to create an array of arrays. First split the entire file by "\n" then split each element of the resulting array by "\t" and you'll get an array of rows that contains an array of cells per row. That's relatively easy but it does involve keeping track of what index element corresponds to which column.
What I prefer to do is convert CSV text to JSON. It's much easier to work with and update the code later.
Copy link to clipboard
Copied
Sorry but i'm really new to this and i'm still having troubles.
I can split my data by "\n" and it splits at the line breaks, OR I can split it by "\t" and it splits at tabs, but I can't seem to do both.
Copy link to clipboard
Copied
You have to do one, then the other.
You're starting with a long string that has line separators and within each line, cell separators.
You first split by line ("\n"). This will return an array where each element of the array is a string that contains tabs.
You now loop through each element of the array of lines and split that by the tab ("\t");
You now have an array of arrays... let's call this myTable
So the third cell in the 5th row can be accessed like this... arrays are zero based
var myCell = myTable[4][2]
The code to do all that might look like this...
var myTable = yourData.split('\n');
for (var i=0; i<myTable.length; i++) {
y = myTable.split('\t');
myTable = y;
}
Copy link to clipboard
Copied
That worked great! Thank you!
Now I need to figure out how to find the ID that the user clicks on in the array, and return the next two values.
I found this code online to search, but when I put it into my code and run it, i'm getting a value of -1, any ideas?
var num = this.getField("ID").value;
var stmData = this.getDataObjectContents("importdata.txt");
var strData = util.stringFromStream(stmData);
var strArray = strData.split("\n");
for(var i=0; i<strArray.length; i++){
y=strArray.split('\t');
strArray=y;
}
if(typeof Array.prototype.indexOf != "function"){
Array.prototype.indexof = function (el){
for(var b = 0; b < this.length; b++) if (el === this) return b;
return -1;
}
}
app.alert(strArray.indexOf(num));
Copy link to clipboard
Copied
First... indexOf is a defined method of the String object in Acrobat JavaScript. You don't need to define it... plus there's a typo in your definition so it wouldn't work as you've coded it anyway.
Second... You'll need to be more specific on exactly what you want to have happen. How did that value of "ID" get set? What exactly do you mean by "I need to figure out how to find the ID that the user clicks on in the array"?
Copy link to clipboard
Copied
ID is name name of a form field in my document.
so i'm typing in one of the ids thats in my .csv file, for example 124. I would like it to search the array, find 124, and then spit out the following value, which will be a city, and put it into the form field called "city", and then go to the next value, employees, and put it into the form field called "employees"
Copy link to clipboard
Copied
Just loop over the outer array, get the id from each row and compare it to the ID entered. The following value would be the index of the id +1
There's a good example for looping over a multidimensional array here
For loop in multidimensional javascript array - Stack Overflow
Copy link to clipboard
Copied
Yes, thats what I thought I could do (the index +1 and +2) but I can't seem to get the index part working?
This code is returning -1
var num = this.getField("ID").value;
var stmData = this.getDataObjectContents("importdata.txt");
var strData = util.stringFromStream(stmData);
var strArray = strData.split("\n");
for(var i=0; i<strArray.length; i++){
y=strArray.split('\t');
strArray=y;
}
var result = strArray.indexOf(num);
app.alert(result);
Copy link to clipboard
Copied
But you know the Excel column that the ID is in? You don't need to use indexOf.
If, for example, the ID is in column C, which would have an array index of 2...you'd get each row... lets call the current row "i" then just get the ID using it's index.
var id = strArray[2];
Copy link to clipboard
Copied
yes, all of my IDs are in column 1, cities are in column 2, and employees are in column 3
so I need to search column 1 for the value that is entered in ID, how would I find the row number so that I could get strArray[1][0]
Copy link to clipboard
Copied
Use the same kind of loop to compare the value of the field to the the value of the ID for that row. Google how to iterate over an array in JavaScript.