Skip to main content
Participant
June 28, 2012
Answered

Replace text in an existing document

  • June 28, 2012
  • 1 reply
  • 1293 views

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;

}

This topic has been closed for replies.
Correct answer Muppet Mark

You have a couple of options for doing this kind of thing… Firstly you could use AI's data driven graphics ( no need of script ) it will however want your info in XML format… You could make and use styles to apply your text formatting if you create from new or you could make a template with dummy text content in the correct formatting and replace the content it will retain the format…

1 reply

Muppet MarkCorrect answer
Inspiring
June 29, 2012

You have a couple of options for doing this kind of thing… Firstly you could use AI's data driven graphics ( no need of script ) it will however want your info in XML format… You could make and use styles to apply your text formatting if you create from new or you could make a template with dummy text content in the correct formatting and replace the content it will retain the format…

Participant
June 29, 2012

Been looking into data driven graphics this morning. This is exactly what I need. Now the issue will be updating an xml file based on the data I have in my spreadsheet without going in and doing it by hand...

Inspiring
June 29, 2012

XML is one of those on the to do list things so I can't help you with that… You might even be better served by the general form ( much more users there ). It should be possible to have an Excel macro format to XML but again out of what I do and know… As I already script there is little or no advantage of data driven graphics for me…