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 :
After running the script the output table is :
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
{
for( var i = 0; i< mySel.tables
{
var a = mySel.tables
if(i<3)
if(mySel.tables
{
alert("a");
mySel.tables
// break;
}
}
}
}
}
From this code I get the below table :
Please do guide me to get the exact output.
Regards,
Saranya
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++
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.
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
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
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
Copy link to clipboard
Copied
Can you share it if its for free?
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 :
Regards,
Uwe
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.
Copy link to clipboard
Copied
Also consider what it means to update data in the catalog after merging cells.
Regards,
Uwe
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.
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
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;
}
}
}
}
}
}
Copy link to clipboard
Copied
Yes,
Variables can be modified easily as per need.
Best
Sunil