Hi,
Updated the code it has both options included so that the decision is up to you as to which you use. Just for curiosity and so people can see the code and what changes depending on if you use columns or rows.
var userRows = true; // change to false to use the columns
if (app.selection.length > 0) {
var _selection = getTableObjectFromSelection( app.selection[0]);
if (_selection != null ){
(userRows) ? mergeCellsUsingRows ( _selection) : mergeCellsUsingColumns ( _selection);
}
}
function mergeCellsUsingRows ( selectionObj){
var _cells = selectionObj.rows.everyItem().cells.everyItem().getElements();
for ( var i = 0; i < _cells.length - 1; i+2){
var i2 = i++;
mergeCellsCleanly ( _cells[i], _cells[i2]);
}
}
function mergeCellsUsingColumns ( selectionObj){
var _cells = selectionObj.columns.everyItem().cells.everyItem().getElements();
for (var i = 0; i < _cells.length/2; i++) {
// as it is by column, the cell next to it is half the size of the table
var i2 = i + _cells.length/2;
mergeCellsCleanly ( _cells[i], _cells[i2]);
}
}
function getTableObjectFromSelection ( selectionObj){
// altered this just to make sure _selection, points at a table.
if ( _selection.parent.constructor.name === "Table"){
_selection = _selection.parent;
} else if ( _selection.parent.parent.constructor.name === "Table"){
_selection = _selection.parent.parent
} else {
_selection = null;
}
return _selection;
}
function mergeCellsCleanly ( cell1, cell2){
if ( cell1.contents === cell2.contents){
cell1.contents = "";
cell1.merge(cell2);
}
}
... View more