Skip to main content
FalconArt
Known Participant
May 6, 2024
Question

Script to Merge Duplicate Table Cells

  • May 6, 2024
  • 1 reply
  • 233 views

Hello! I am trying to modify this script (which I can't seem to get to work).

It is intended to merge all consecutive duplicate cells:

//Merge Same-text Table Cells by Sunil Yadav
var myDocument = app.activeDocument;  

var allTables = app.selection[0].parentStory.tables.everyItem().getElements();

for (var tf = 0; tf < app.selection.length; tf++) {

  var mySelection = app.selection[0];

var myStory = mySelection.hasOwnProperty("storyOffset") ? mySelection.storyOffset.parentStory : mySelection.insertionPoints[0].parentStory;

  Merge(mySel);

}

function Merge (mySel){  

    for( var j = 0; j< mySel.tables.length ; j++ ){  

        for (var f = 0; f < mySel.tables.columns.length; f++){  

            for( var i = 0; i< mySel.tables.columns.cells.length ;i++ ){  

                var a = mySel.tables.columns.cells.contents;  

                if(i < mySel.tables.columns.cells.length-1){  

                    if(mySel.tables.columns.cells[i+1].contents==a){  

                        mySel.tables.columns.cells.contents = "";  

                        mySel.tables.columns.cells.merge(mySel.tables.columns.cells[i+1]);  

                        mySel.tables.columns.cells.verticalJustification = 1667591796;  

                        f = -1;  

                        i = -1;  

                        }  

                    }  

                }  

            }  

        }  

    }  

 Any pointers?

Initial:

After script:

 

This topic has been closed for replies.

1 reply

Robert at ID-Tasker
Legend
May 6, 2024

 

I'm not JS guy, but I think in this line:

 

var a = mySel.tables.columns.cells.contents;   

 

You forgot to indicate - [] - from which cell in column you want the get contents.

 

Same few rows later.