Skip to main content
tpk1982
Legend
October 13, 2016
Answered

Table paragraph split and move

  • October 13, 2016
  • 2 replies
  • 1867 views

Hi All,

I hope it can doable in script. But am struck from a point.

Requirement:

If a table cell contains more than one line then we need to insert the rows and move the content

Logic used:

1) Count the line in each cell

2) If it contains more than one line then add the rows before

3) move the contents

Tried coding (Code got from forum, thanks to Chinna):

var doc = app.activeDocument,          

    _tables = doc.stories.everyItem().textStyleRanges.everyItem().getElements(),    

    i, j, k, l, _columns, _cells, tempVar = "", page; 

    

for (i = 0; i < _tables.length; i++) 

    for (j = 0; j < _tables.tables.length; j++) 

    { 

        tempVar += "\r"; 

        _columns = _tables.tables.columns; 

        for (k = 0; k < _columns.length; k++) 

        { 

            _cells = _columns.cells; 

            tempVar += "\r"; 

            for (l = 0; l < _cells.length; l++) 

            { 

                if(_cells.paragraphs.length>1){

                    alert(_cells[1].contents)

//~                     tempVar += _cells.paragraphs + "\t";          //Copy contents from table cell to a temporary variable 

                    _cells.rows.add( LocationOptions.BEFORE, _cells.rows[0] );

                   

                }

            } 

        } 

    } 

}    

Thanks,

K

This topic has been closed for replies.
Correct answer Skemicle

I may have over complicated this a bit.. but here is my script:

/*

scripted by Skemicle

11:42 AM 10/14/2016

*/

if(parseFloat(app.version) < 6){

    main();

}else{

    app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Seperate Table Rows");

}

function main(){

    var doc = app.activeDocument,

    text = doc.textFrames;

    for(t=0;t<text.length;t++){

        table = text.tables;

        for(T=0;T<table.length;T++){

            row = table.rows;

            for(r=0;r<row.length;r++){

                cell = row.cells;

                if(cell[0].lines.length > 1){

                    table.rows.add(LocationOptions.AFTER, row)

                    for(c=0;c<table.columns.length;c++){

                        row[r+1].cells.contents = cell.lines[-1].contents;

                        cell.lines[-1].contents = "";

                        cell.characters[-1].contents = "";

                    }r--;

                }

            }

        }

  

    }

}

This could be a good starting point for modifications Obi-wan​.

2 replies

Skemicle
SkemicleCorrect answer
Inspiring
October 14, 2016

I may have over complicated this a bit.. but here is my script:

/*

scripted by Skemicle

11:42 AM 10/14/2016

*/

if(parseFloat(app.version) < 6){

    main();

}else{

    app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Seperate Table Rows");

}

function main(){

    var doc = app.activeDocument,

    text = doc.textFrames;

    for(t=0;t<text.length;t++){

        table = text.tables;

        for(T=0;T<table.length;T++){

            row = table.rows;

            for(r=0;r<row.length;r++){

                cell = row.cells;

                if(cell[0].lines.length > 1){

                    table.rows.add(LocationOptions.AFTER, row)

                    for(c=0;c<table.columns.length;c++){

                        row[r+1].cells.contents = cell.lines[-1].contents;

                        cell.lines[-1].contents = "";

                        cell.characters[-1].contents = "";

                    }r--;

                }

            }

        }

  

    }

}

This could be a good starting point for modifications Obi-wan​.

tpk1982
tpk1982Author
Legend
October 15, 2016

Hi Skemicle

Exactly this is i want.. thank you so much

Thanks Obi for your interest on this thread

Regards,

K

tpk1982
tpk1982Author
Legend
October 18, 2016

Maybe I did not understand your requirements.
I thought that every single line should get its own cell.

And you still have the formatting issue.

Set some text in line 2 or above to e.g. to "Bold" and run the script again.
You'll see, that the format cannot travel over to the new cell. It will get the basic format of the cell, that the first insertion point will dictate.

Moving the text with the move() method will do that appropriately.
Assigning the contents is the wrong way.

Regards,
Uwe


Hi Uwe,

Sorry if i was not explained my requirement clearly.

I need to split the cell contents paragraph by paragraph NOT line by line. Also i was not worried about formatting while you said. But now i am worried it may become challenge in future.

I will try move() method, and let you know if i need any help.

Thanks for your help again.

Regards,

K

tpk1982
tpk1982Author
Legend
October 13, 2016

Any suggestions please?

Obi-wan Kenobi
Legend
October 14, 2016

Cool and interesting topic!  I'll take a look to a script this WE! 

(^/)

tpk1982
tpk1982Author
Legend
October 14, 2016

Thanks Obi