Answered
This topic has been closed for replies.
Hi @Mahesh12, here is a script that sets table rows to alternating cell styles. It's a quick one, so please test and let me know how it goes.
- Mark
/*
by m1b
here: https://community.adobe.com/t5/indesign-discussions/alternate-fills-color-is-not-working-for-table/td-p/13280870
*/
// table cell style names for alternating rows:
var cellStyleNames = [
'Body',
'Body Alt',
];
function main() {
if (app.selection.length == 0) {
alert('Please make a selection and run script again.')
return;
}
if (
!app.selection[0].hasOwnProperty('parentStory')
|| !app.selection[0].parentStory.tables[0].isValid
) {
alert('No table found in selection.')
return;
}
var doc = app.activeDocument,
table = app.selection[0].parentStory.tables[0],
rows = table.rows,
alternator = 0,
cellStyles = [],
clearOverrides = true;
// collect the cell styles
cellStyleNamesLoop:
for (var i = 0; i < cellStyleNames.length; i++)
for (var j = 0; j < doc.allCellStyles.length; j++)
if (cellStyleNames[i] == doc.allCellStyles[j].name) {
cellStyles.push(doc.allCellStyles[j]);
continue cellStyleNamesLoop;
}
// must have at least two!
if (cellStyles.length < 2) {
alert('Not enough cell styles found.');
return;
}
// apply to each row
for (var i = 0; i < rows.length; i++) {
if (rows[i].rowType === RowTypes.BODY_ROW) {
rows[i].cells.everyItem().appliedCellStyle = cellStyles[alternator];
if (clearOverrides)
rows[i].cells.everyItem().clearCellStyleOverrides(false);
// toggle the alternator
alternator = (alternator + 1) % cellStyles.length;
}
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Set alternating table styles");
Sign up
Already have an account? Login
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inSign in to Adobe Community
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.
