8 Replies Latest reply on May 24, 2013 4:48 AM by Jump_Over

    Data Merge not picking up all data fields

    SteveE91

      Hi,

       

      I've been having a problem, posted here: http://forums.adobe.com/thread/1217090

       

      Essentially, I have been using the script 'CSV2Tables' to prepopulate simple 2 column tables with information drawn from a CSV data source.

       

      The tables consist of a title, and a block of information about that specific subject. Many of the blocks of information I am importing also contain data tags (<<DataTag>>)

       

      The plan is to use CSV2Tables to populate the tables, then change data source and run a normal ID Data Merge to replace all of the <<tags>> within the tables with information from a second CSV data source.

       

      The problem is, CSV2Tables is importing the data and prepopulating the tables just fine, but the data tags that are found within each body of text are being picked up as plain text (as Peter Spier has pointed out, they are being drawn from a plain text stream rather than being dragged from the Mail Merge panel) so when I try to run the ID Mail Merge, none of the fields are found within my document.

       

      My question is - is there a method, or script available out there that can essentially do a Find/Change within a document, and replace plain text that appears like this <<PlainText>> with a Data Tag that is recognised by ID, which will allow me to run a Data Merge?

       

      I have been told to check out some of the commercial plugins available out there, but registering and setting up demo's would require me to contact our IT provider for each, and that would be an exhaustive process.

       

       

      Any help is much appreciated.

        • 1. Re: Data Merge not picking up all data fields
          SteveE91 Level 1

          Do I have everyone stumped?

          • 2. Re: Data Merge not picking up all data fields
            Jump_Over Level 5

            Hi,

             

            As far as I know you need in fact a script to replace 'plain text' with dataMergeTextPlaceholders.

             

            a small example;

            Array.prototype.find = function (string) {
                for (var s = 0; s < this.length; s++)
                    if (this[s] == string) return s;
                    return -1;
                }
            
            var mFile, mDoc, mFields, mFieldsNames, mStory, mPlainTxt, mNameToCompare, mField_ID;
            
            // edit this with proper .csv path
            mFile = File(Folder.myDocuments + "/" + "Merge.csv");
            mDoc = app.activeDocument;
            mDoc.dataMergeProperties.selectDataSource(mFile);
            mFields = mDoc.dataMergeProperties.dataMergeFields;
            mFieldsNames = mFields.everyItem().fieldName;
            
            // script assumes a text box with "plain texts" to replace is selected
            mStory = app.selection[0].parentStory;
            app.findGrepPreferences = null;
            app.findGrepPreferences.findWhat = "<<.+?>>";
            mPlainTxt = mStory.findGrep(true);
            for (var k = 0; k < mPlainTxt.length; k++) {
                mNameToCompare = mPlainTxt[k].contents.slice(2,-2);
                mField_ID = mFieldsNames.find(mNameToCompare);
                if (mField_ID > -1) {
                      mField = mFields[mField_ID];
                    mDoc.dataMergeTextPlaceholders.add (
                        mStory,
                        mPlainTxt[k].insertionPoints[0],
                        mField
                        )
                mPlainTxt[k].remove();
                 }
                }
            

             

            At this point you could run a 'normal Data Merge'.

            This should work for linked frames (one selected) or table inside selected frame

            I hope it could be helpful for more complex task

             

            rgds

            Jarek

             

            Message was edited by: Jump_Over ==> brackets added for last if statement

            • 3. Re: Data Merge not picking up all data fields
              SteveE91 Level 1

              Ran a test using this, I'm no ID expert so it took a bit of thinking.. however, it worked flawlessly so thank you!

               

              Ideally now, if I can link all of the frames together (only a couple run onto a second page so they won't have any physical effect on each other) and do a rerun with this, hopefully the entire document will be done.

               

              Thanks once again, cracking answer. I will be back to report.

              • 4. Re: Data Merge not picking up all data fields
                Jump_Over Level 5

                Hi,

                 

                If you tell more about targered text boxes it could be easy modyfied,

                 

                Could the boxes be named?

                or

                Script could replace all "<<..>>" occurences inside entire doc if it is safe from your side.

                 

                rgds

                Jarek

                • 5. Re: Data Merge not picking up all data fields
                  SteveE91 Level 1

                  I tried linking them all, one to the next, but as some of the frames are much larger than the text/table they contain (to allow for the mail merged content) every time I tried to thread one to another, it created a new frame.

                   

                  What would you like to know about them?

                   

                  And yes, they could all be named individually - Would naming each one in order TF1, TF2 etc?

                  • 6. Re: Data Merge not picking up all data fields
                    Jump_Over Level 5

                    Hi,

                     

                    is modyfied

                    Array.prototype.find = function (string) {
                        for (var s = 0; s < this.length; s++)
                            if (this[s] == string) return s;
                            return -1;
                        }
                    
                    var mFrames, mFile, mDoc, mFields, mFieldsNames, mStory, 
                         mPlainTxt, mNameToCompare, mField_ID;
                    
                    // edit this with proper .csv path
                    mFile = File(Folder.myDocuments + "/" + "Merge.csv");
                    mDoc = app.activeDocument;
                    mDoc.dataMergeProperties.selectDataSource(mFile);
                    mFields = mDoc.dataMergeProperties.dataMergeFields;
                    mFieldsNames = mFields.everyItem().fieldName;
                    
                    mFrames = mDoc.textFrames.everyItem().getElements();
                    for (var i = 0; i < mFrames.length; i ++) {
                        if (!mFrames[i].name.match(/TF\d*$/) ) continue;
                        // script assumes a targered text boxes are named like: TF1, TF2... 
                         // (TF plus any integer) or just TF (no integer)
                        // - they could have the same name
                        // any other name exludes frame from this loop
                        mStory = mFrames[i].parentStory;
                        app.findGrepPreferences = null;
                        app.findGrepPreferences.findWhat = "<<.+?>>";
                        mPlainTxt = mStory.findGrep(true);
                        for (var k = 0; k < mPlainTxt.length; k++) {
                            mNameToCompare = mPlainTxt[k].contents.slice(2,-2);
                            mField_ID = mFieldsNames.find(mNameToCompare);
                            if (mField_ID > -1) {
                                  mField = mFields[mField_ID];
                                mDoc.dataMergeTextPlaceholders.add (
                                    mStory,
                                    mPlainTxt[k].insertionPoints[0],
                                    mField
                                    )
                            mPlainTxt[k].remove();
                            }
                        }
                    }
                    

                     

                    script assumes a targered text boxes are named like:

                    TF1, TF2... (TF plus any integer) or just TF (no integer) -

                    - they could have the same name.

                     

                    Jarek

                    • 7. Re: Data Merge not picking up all data fields
                      SteveE91 Level 1

                      Thank you - again, worked perfectly.

                       

                      I really thought I'd have to give up with this method and retain some of the slower processes.

                      • 8. Re: Data Merge not picking up all data fields
                        Jump_Over Level 5

                        Hi,

                        Never give up

                        (with scripts on the side)

                         

                        Jarek