• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Merge same value cells in a table

New Here ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Hi,

How to merge same value cells in a table in InDesign Document using scripts.

Herewith I attached the table which want to merge :

original.PNG

After running the script the output table is :

Capture.PNG

I tried the below code for merging the cells :

var myDocument = app.activeDocument;

var allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();

for(var FitmyCounter = 0; FitmyCounter < app.activeDocument.activeLayer.textFrames.length; FitmyCounter++)

{                                           

                  myStoryy = app.activeDocument.activeLayer.textFrames.item(FitmyCounter);                                  

                          Merge(myStoryy);

                                   

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<3)

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

                        {

                            alert("a");

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

                           // break;

                            }

                                            }

                                    }

                        }

    }

From this code I get the below table :

Capture1.PNG

Please do guide me to get the exact output.

Regards,

Saranya

TOPICS
Scripting

Views

3.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Mar 08, 2019 Mar 08, 2019

Hi there,

You are almost there.

Just a bit changes will do the task,

Try this code, It will work...

var myDocument = app.activeDocument;

var allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();

for(var FitmyCounter = 0; FitmyCounter < app.activeDocument.activeLayer.textFrames.length; FitmyCounter++){

    myStoryy = app.activeDocument.activeLayer.textFrames.item(FitmyCounter);

    Merge(myStoryy);

    }

function Merge (mySel){

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

...

Votes

Translate

Translate
People's Champ ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

The main issue here is the extra level of logic. It's easy to merge similar contents, it's harder to not do it given the context. because, you need to set the logic beyond it. And on top of it that may not be the same logic over every single table.

Not impossible but I think it's in your hands at this stage.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

Hi Loic,

yes. It's not doable in one single loop.

After done with the first column the rows to consider should be restricted to the number of rows that are merged with each cell of column one.

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2018 Mar 30, 2018

Copy link to clipboard

Copied

And I would add a column to the right that will not be touched at all before doing any calculations.

So you can be sure that at any stage of the script the number of rows is static. That new column can be removed after the script is done with merging cells.

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 02, 2018 Apr 02, 2018

Copy link to clipboard

Copied

Hi Loic and Uwe,

Thanks you so much for your responses...

The script provided by Loic for merging the table cells  works fine as expected...Thank you..

Regards,

Saranya

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 07, 2019 Mar 07, 2019

Copy link to clipboard

Copied

Can you share it if its for free?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2019 Mar 07, 2019

Copy link to clipboard

Copied

Hm. FWIW:

Don't know if it's wise to "simplify" a table like we are seeing in the second screenshot of the first post of our OP.

Always ask: Is a human reader be able to digest the tabular information better now after merging cells to that extend?

After running the script the output table is :

Capture.PNG

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 07, 2019 Mar 07, 2019

Copy link to clipboard

Copied

No they couldn't digest it better but when there is too much tables on single spread with a lot of other element, this approach is better in my opinion.
It simplify page design and focus on pictures more than the table.

WhatsApp Image 2019-03-07 at 10.57.50.jpeg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 07, 2019 Mar 07, 2019

Copy link to clipboard

Copied

Also consider what it means to update data in the catalog after merging cells.

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 07, 2019 Mar 07, 2019

Copy link to clipboard

Copied

Didn't thinked about that.
Actually the  catalog  I'm designing doesn't have any excel file with data.
I have to use various OCR software to  recognize text and tables.
Then copy table part from excel to indesign and use script to create table, delete empty columns and row, merge emtpy cell, create header etc.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 08, 2019 Mar 08, 2019

Copy link to clipboard

Copied

Hi there,

You are almost there.

Just a bit changes will do the task,

Try this code, It will work...

var myDocument = app.activeDocument;

var allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();

for(var FitmyCounter = 0; FitmyCounter < app.activeDocument.activeLayer.textFrames.length; FitmyCounter++){

    myStoryy = app.activeDocument.activeLayer.textFrames.item(FitmyCounter);

    Merge(myStoryy);

    }

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;

                        }

                    }

                }

            }

        }

    }

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 08, 2019 Mar 08, 2019

Copy link to clipboard

Copied

Thank you very much Sunil.

For people want to run it only on selected text frame. Use this code. 

var myDocument = app.activeDocument; 

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

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

  var mySel = app.selection[tf];

  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; 

                        } 

                    } 

                } 

            } 

        } 

    } 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 08, 2019 Mar 08, 2019

Copy link to clipboard

Copied

LATEST

Yes,

Variables can be modified easily as per need.

Best

Sunil

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines