Replace text in an existing document
Is there a way to access text boxes in an existing document and replace them with a new value taken from an excel table? I've been able to create new documents from scratch and populate them with excel data, but formating these new documents to look like those we already made is proving more difficult than I thought using just Javascript.
Ideally I'd be able specific an index for each text box and replace them in order as i itterate through the excel(or CSV) document.
I already have a way to pull from the table, thanks to another question asked here, but can't figure out the object model to put the new info in a specific place.
Below is what I came up with to create a very rough looking document from scratch:
#target Illustrator
var csvFile = new File('C:/users/whatever/spreadsheet.csv');
if(!csvFile.exists){
alert('notafile');
app.quit(); }
var fileArray = readInCSV(csvFile);
var length = fileArray.length;
var j = (length * 30) + 10;
var myDocument = app.documents.add(DocumentColorSpace.CMYK, 330, j);
var myDoc = app.activeDocument;
for (i in fileArray){
k= j-15;
if (j < 20){
break;
}
if (i == 0){
var myTextFrame = myDocument.textFrames.add();
myTextFrame.position = [100,(j-15)];
myTextFrame.contents = fileArray.toString();
}
else if (i == 1){
var myTextFrame = myDocument.textFrames.add();
var artLayer = myDoc.layers[0];
myTextFrame.position = [15,j];
myTextFrame.contents = fileArray.toString();
app.defaultStroked = true;
app.defaultFilled = false;
var rect = artLayer.pathItems.rectangle((j+3),12,300, 18);
}
else{
var myLine = myDoc.pathItems.add();
var myTextFrame = myDocument.textFrames.add();
myTextFrame.position = [15,j];
myTextFrame.contents = fileArray.toString();
//set stroked to true so we can see the path
myLine.stroked = true;
myLine.setEntirePath(Array(Array(15, k),Array(300, k)));
}
j = j - 30;
}
function readInCSV(fileObj) {
var fileArray = new Array();
fileObj.open('r');
while(!fileObj.eof) {
var thisLine = fileObj.readln();
var csvArray = thisLine.split(',');
fileArray.push(csvArray);
}
fileObj.close();
return fileArray;
}