3 Replies Latest reply on Mar 22, 2012 12:59 PM by Zoffix222

    Lost In The Docs / Data Merge

    Zoffix222 Level 1

      Hey,

       

      I'm trying to cook up a script that would add one of the items in my "data merge" into a text frame.

       

      I have the code below, and I'm trying to understand the scripting help file, but so far I was only able to output some kind of resolve(....) text.

       

      The "310-7251" is a name of one item in my data merge list. I want the text frame to contain it's.. um.. reference(?), the "<< 410-7251 >>" text that I would get if I did this manually, that would turn into the field's value when I would create a merged document.

       

      What am I doing wrong?

       

      var doc      
      = app.documents.add();
      
      var text_frame = doc.pages[0].textFrames.add();
      text_frame.geometricBounds = [
      
      
      '0pt', '115pt', '240pt', '240pt',
      
      ];
      
      doc.dataMergeProperties.selectDataSource('E:/Temp/Merge-Test.csv');
      text_frame.parentStory.insertionPoints[-1].contents += doc.dataMergeTextPlaceholders.item('310-7251').toSpecifier();
      
        • 1. Re: Lost In The Docs / Data Merge
          Zoffix222 Level 1

          This code inserts a type of thing I want, but it works only with the .firstItem() on the last line. I want to call up the field by name, but when I use doc.dataMergeProperties.dataMergeFields.item('310-7251') there, it errors out saying "expected DataMergeField but received nothing". I even outputted the fieldName out of the .firstItem() to ensure I got the name right, but it still doesn't resolve to anything :/

           

           

          var doc      
          = app.documents.add();
          
          var text_frame = doc.pages[0].textFrames.add();
          text_frame.geometricBounds = [
          
          '0pt', '115pt', '240pt', '240pt',
          
          ];
          doc.dataMergeProperties.selectDataSource('E:/Temp/Merge-Test.csv');
          doc.dataMergeTextPlaceholders.add(
          
          text_frame.parentStory,
          
          text_frame.parentStory.insertionPoints[-1],
          
          doc.dataMergeProperties.dataMergeFields.firstItem()
          
          );
          
          • 2. Re: Lost In The Docs / Data Merge
            Zoffix222 Level 1

            Weird, if I do alert(doc.dataMergeProperties.dataMergeFields.item('310-7251')) I receive a message with "[object DataMergeField]", same as with firstItem(), but even if I try to view it's .fieldName property, I get an error saying invalid object.

            • 3. Re: Lost In The Docs / Data Merge
              Zoffix222 Level 1

              Well, I gave up and wrote a function that would traverse all the items and find the one I want.

               

              With my extremely limited knowledge of the language, it seems there's a bug with the item() method; it doesn't accept field name string properly.

               

              Here's my code that finally worked, but I'd love to know what I was doing wrong.. Thanks.

               

              var doc          = app.documents.add();
              var text_frame = doc.pages[0].textFrames.add();
              text_frame.geometricBounds = [
                  '0pt', '115pt', '240pt', '240pt',
              ];
              doc.dataMergeProperties.selectDataSource('E:/Temp/Merge-Test.csv');
              doc.dataMergeTextPlaceholders.add(
                  text_frame.parentStory,
                  text_frame.parentStory.insertionPoints[-1],
                  get_field('310-7251', doc)
              );
              
              function get_field(field_name, doc) {
                  var fields = doc.dataMergeProperties.dataMergeFields;
              
                  for ( var i = 0, l = fields.length; i < l; i++ ) {
                      if (fields[i].fieldName == field_name) {
                          return fields[i];
                      }
                  }
              
                  alert('Error: did not find any fields with name ' + field_name);
              }