14 Replies Latest reply on May 22, 2018 4:10 AM by frontline design

    move text from one table field to another

    frontline design

      Hi,

       

      I am looking for help moving text from one table field to another and have around 80 tables to modify in one document.

       

      The text I want to move has it's own para style which will hopefully help with the script.

       

      Here is an example of what I currently have:

       

      Column 1Column 2Column 3
      empty field

      1234 (with paragraph style 'partno' followed by a return)

      ABCD (with paragraph style 'desc')

      qty
      empty field

      123456 (with paragraph style 'partno' followed by a return)

      ABCD (with paragraph style 'desc')

      qty
      empty field

      12345 (with paragraph style 'partno' followed by a return)

      ABCD (with paragraph style 'desc')

      qty

       

      Here is an example of the finished result I would like to achieve:

       

      Column 1Column 2Column 3
      1234

      ABCD (description with paragraph style 'desc')

      qty
      123456

      ABCD (description with paragraph style 'desc')

      qty
      12345

      ABCD (description with paragraph style 'desc')

      qty


      I am just discovering the amazing things that can be done using scripts in indesign and how efficient they make laborious tasks.

       

      Appreciate any help I receive.

       

      thanks

      David

        • 1. Re: move text from one table field to another
          BarlaeDC Adobe Community Professional

          Hi,

           

          You should be able to use code something like this, I have made it more verbose than is probably needed as you say you are just discovering what can be done, and I wanted to make it as easy to follow as possible.

           

          // get active document

          var myDoc = app.activeDocument;

           

          // get all tables from that document

          var myPageItems = myDoc.pageItems.everyItem();

          var myTables = myPageItems.tables.everyItem().getElements();

           

          // for each table

          for ( var i = 0; i < myTables.length; i++)

          {

              // get all cells

              var curTable = myTables[i];

              var curTableCells = curTable.cells;

           

              // for each cell

              for ( var j = 0; j < curTableCells.length; j++)

              {

                  var curCell = curTableCells[j];

                  // get all text ranges - a text range is made everytime a style is changed

                  var curTextStyleRanges = curCell.textStyleRanges;

                 

                  for ( var k = 0; k < curTextStyleRanges.length; k++)

                  {

                      var curTextStyleRange = curTextStyleRanges[k];

                      // check the name of the text ranges appliedParagraphStyle

                      if ( curTextStyleRange.appliedParagraphStyle.name === "partno")

                      {

                          // as your example the empty cell is always the one before the cell we currently have

                          var jMinusOne = j - 1;

                          var newDestCell = curTableCells [jMinusOne];

                          // only copy the cells contents that match our range

                          newDestCell.contents = curTextStyleRange.contents;

                          // set the new contents to have the same paragraph style

                          newDestCell.textStyleRanges[0].appliedParagraphStyle = myDoc.paragraphStyles.itemByName("partno");

                          // delete the contents from the original cell

                          curTextStyleRange.remove();               

                      }

                  }

              }

          }

           

          Hope this helps

           

          Malcolm

          2 people found this helpful
          • 2. Re: move text from one table field to another
            Laubender Adobe Community Professional & MVP

            Hi David,

            if you like to move text, would you like the formatting of the text stay the same after it is moved?

            If that's the case one would use method move() with object Text and would not use property contents.

             

            Regards,
            Uwe

            2 people found this helpful
            • 3. Re: move text from one table field to another
              Level 4

              A simplistic and short writing of a way working in such a sample [transfer of the first para of each cell of the second column to the corresponding cell of the first column]!

               

              var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements(),
              R = myRows.length,  r;
              for ( r = 0; r < R; r++ ) {
                  myRows[r].cells[1].paragraphs[0].characters.itemByRange(0, myRows[r].cells[1].paragraphs[0].characters.length-2).move(LocationOptions.after, myRows[r].cells[0].insertionPoints[0]);
                  myRows[r].cells[1].paragraphs[0].contents = "";
              }
              

               

              Best,

              Michel, from FRIdNGE

              1 person found this helpful
              • 4. Re: move text from one table field to another
                frontline design Level 1

                Hi Malcolm, thanks for helping out.

                 

                I ran your script and get the following error:

                Screen Shot 2018-05-17 at 10.09.52 AM.png

                • 5. Re: move text from one table field to another
                  frontline design Level 1

                  Hi Michel, thank you your help.

                   

                  I ran your script and get the following error:

                  Screen Shot 2018-05-17 at 10.09.00 AM.png

                  • 6. Re: move text from one table field to another
                    frontline design Level 1

                    Hi Uwe,

                     

                    you are right, the paragraph formatting of the 1st para in column 2 would remain the same once it was moved to column 1.

                     

                    thanks for your help

                    David

                    • 7. Re: move text from one table field to another
                      Laubender Adobe Community Professional & MVP

                      Hi David,

                      then the approach of Michel's script is the right one.

                       

                      But maybe there are some exceptions in one of your tables in the document.

                      E.g. some empty cells in column two. Or some graphic cells here and there.

                       

                      To make that script run without errors some if statements should be included.

                      But for that it would be perfect to see into the original InDesign document.

                       

                      Regards,
                      Uwe

                      1 person found this helpful
                      • 8. Re: move text from one table field to another
                        BarlaeDC Adobe Community Professional

                        HI,

                         

                        Can you provide a sample document so that we can see what the issues is, it is probably just some graphic object or other non text item, which doesn't have the properties required by the scripts.

                         

                        Regards

                         

                        Malcolm

                        • 9. Re: move text from one table field to another
                          frontline design Level 1

                          Hi Malcolm, thank you.

                           

                          here is a url to download a sample spread. - hopefully this shines some light on the issue.

                           

                          Dropbox - sample.indd

                           

                          I do hope it is a simple thing to resolve. I also believe we are in opposite hemispheres but if I receive a reply - which may be late evening for me - I will hopefully be able to check on any feedback I receive.

                           

                          thanks Again for your help

                          • 10. Re: move text from one table field to another
                            Laubender Adobe Community Professional & MVP

                            Hi David,

                            no wonder Michel's script is tripping. Your sample InDesign document presents a totally different structure in the table cells compared with what you presented in your initial post.

                             

                            The best bet—as seen from the sample doc—is to inspect the fill color of cell 1 and cell 2 of a given row. If it is the same and if it is "Titanium" and if the first cell is empty, then inspect cell 2. If cell two contains two paragraphs and if paragraph one consists of numbers only, then move paragraph one to the empty cell 1 and finally get rid of the pargaraph sign at the end of the paragraph.

                             

                            That would be a kind of algorithm that could work. However it could fail silently or do the wrong thing, if anywhere in your document is an exception. Maybe a cell is not filled with right fill color or white space sneaked in in the cell that should be empty or in the paragraph that should contain only numbers. Other exceptions are possible as well… :-)

                             

                            Regards,
                            Uwe

                            • 11. Re: move text from one table field to another
                              Laubender Adobe Community Professional & MVP

                              Oh, and another thing: Your sample document has no paragraph style named "desc". Another thing, that could go wrong, if a script is working with that to identify the right cell.

                               

                              Regards,
                              Uwe

                              • 12. Re: move text from one table field to another
                                frontline design Level 1

                                Hi Uwe,

                                 

                                Thanks for the honest feedback.

                                 

                                I guess my thinking was a script would look for text styled partno in

                                column 2, check the row it’s in then copy ( cut) the partno text Into

                                column 1 of the corresponding row. Then find the next instance of partno...

                                but i have no idea about scripting hence asking for some help.

                                 

                                Even if a script helped with 90% it would save me time.

                                 

                                Thanks

                                David

                                • 13. Re: move text from one table field to another
                                  Level 4

                                  Hi,

                                   

                                  Uwe is totally right! … Be careful on what you ask and what you provide! …

                                   

                                  Once again, this could work on your file sample but at your own risks! [if safer, think about paying a scripter to achieve such a script!]

                                   

                                  var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().getElements(),  
                                  R = myRows.length,  r;  
                                  for ( r = 0; r < R; r++ ) {
                                      try {
                                          if ( myRows[r].cells[1].paragraphs[0].appliedParagraphStyle.name == "partno" ) {
                                              myRows[r].cells[1].paragraphs[0].characters.itemByRange(0, myRows[r].cells[1].paragraphs[0].characters.length-2).move(LocationOptions.after, myRows[r].cells[0].insertionPoints[0]);  
                                              myRows[r].cells[1].paragraphs[0].contents = "";
                                          }
                                      } catch(e) {}
                                  }
                                  

                                   

                                  Best,

                                  Michel

                                  • 14. Re: move text from one table field to another
                                    frontline design Level 1

                                    Hi Michel, thank you for your help with the script, it works well and has saved a lot of tedious work.

                                     

                                    I did find that some text in the document had a paragraph style and character style which I think cause the script to falter but after removing the character style and making sure the para style was not is a folder all was good.

                                     

                                    thanks again

                                    David