Skip to main content
Known Participant
May 16, 2018
Answered

move text from one table field to another

  • May 16, 2018
  • 2 replies
  • 2512 views

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

This topic has been closed for replies.
Correct answer FRIdNGE

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


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.cells[1].paragraphs[0].appliedParagraphStyle.name == "partno" ) {

            myRows.cells[1].paragraphs[0].characters.itemByRange(0, myRows.cells[1].paragraphs[0].characters.length-2).move(LocationOptions.after, myRows.cells[0].insertionPoints[0]); 

            myRows.cells[1].paragraphs[0].contents = "";

        }

    } catch(e) {}

}

Best,

Michel

2 replies

Community Expert
May 16, 2018

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

FRIdNGE
Inspiring
May 16, 2018

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.cells[1].paragraphs[0].characters.itemByRange(0, myRows.cells[1].paragraphs[0].characters.length-2).move(LocationOptions.after, myRows.cells[0].insertionPoints[0]);

    myRows.cells[1].paragraphs[0].contents = "";

}

Best,

Michel, from FRIdNGE

Known Participant
May 17, 2018

Hi Michel, thank you your help.

I ran your script and get the following error:

BarlaeDC
Community Expert
May 16, 2018

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;

    var curTableCells = curTable.cells;

    // for each cell

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

    {

        var curCell = curTableCells;

        // 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;

            // 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

Known Participant
May 17, 2018

Hi Malcolm, thanks for helping out.

I ran your script and get the following error:

BarlaeDC
Community Expert
May 17, 2018

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