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

Hi Uwe,

When I've tested Skemicle's script, I saw this problem!

I've fixed it with a simple trick: counting the paras number per cell on each row and temporary make them equal!! As inserting an "@":

Just finally play: find ^@ and replace by nothing!

(^/)


Hi Skemicle

I just noticed what Uwe said. Yes it is a big problem.

Is it possible to help with that?  I tried and failed. Maybe we can use paragraphs ("/r") instead of lines?

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