Hi @DWheels If you don’t mind running a script, I’ve written one that is configured for your case. Give it a try. You can edit the color values and the coloring logic. I’ve marked “Make the colors” and “Coloring Logic” in the script. (Or you can edit the colors later, from Indesign.) But first just select a table, or some cells and see what it does.
—Mark
/**
* @file Color Table Cells By Numbers.js
*
* Colors selected table cells based on the number in cell.
*
* @author m1b
* @version 2026-04-08
* @discussion https://community.adobe.com/questions-671/how-to-apply-conditional-cell-colors-in-indesign-tables-based-on-numeric-values-1556751
*/
function main() {
if (
0 === app.documents.length
|| 0 === app.activeDocument.selection.length
)
return alert('Please select some table cells and try again.');
var doc = app.activeDocument;
var counter = 0;
var cells = getCells(doc.selection);
if (!cells)
return alert('Please select some table cells and try again.');
/* ------------------ *
* Make the colors *
* ------------------ */
var colors = {
green: makeSwatch(doc, 'Traffic Green', [40, 0, 100, 0]),
yellow: makeSwatch(doc, 'Traffic Yellow', [0, 10, 100, 0]),
red: makeSwatch(doc, 'Traffic Red', [0, 100, 90, 5]),
};
// color each numbered cell
doThing(colorByNumber, cells);
/**
* Set cell color based on the number found in cell contents.
* @param {Cell} cell - an Indesign Table Cell.
*/
function colorByNumber(cell) {
var n = asNumber(cell.contents);
if (undefined === n)
return;
var color;
/* ----------------- *
* COLORING LOGIC *
* ----------------- */
if (n >= 80 && n <= 100)
color = colors.green;
else if (n >= 50 && n <= 79)
color = colors.yellow;
else if (n >= 0 && n <= 49)
color = colors.red;
if (!color)
return;
cell.fillColor = color;
// counter is just for reporting at end
counter++;
};
// show results
alert('Colored ' + counter + ' cells.');
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Color Cells By Number');
/**
* Performs function `doThisTo` on each element of `things`.
* @param {Function} doThisTo - the function to execute, given a thing.
* @param {Array<*>} things - array of things.
*/
function doThing(doThisTo, things) {
for (var i = things.length - 1; i >= 0; i--)
doThisTo(things[i]);
};
/**
* Returns array of cells, given an object.
* @author m1b
* @version 2024-05-03
* @param {*} obj - an object, can be an array, eg. a document selection.
* @returns {Array<Cell>}
*/
function getCells(obj) {
var cells = [];
if (obj == undefined)
return [];
else if (Array === obj.constructor)
for (var i = 0; i < obj.length; i++)
cells = cells.concat(getCells(obj[i]));
else if (Document === obj.constructor)
cells = getCells(getThingsFromDocument(obj, 'stories'));
else if (
obj.hasOwnProperty('cells')
&& obj.cells.length > 0
)
cells = obj.cells.everyItem().getElements();
else if (
obj.hasOwnProperty('tables')
&& obj.tables.length > 0
)
cells = getCells(obj.tables.everyItem().cells.everyItem().getElements())
else if (Cell === obj.constructor)
cells = [obj];
else if (
obj.hasOwnProperty('parent')
&& Cell === obj.parent.constructor
)
cells = getCells(obj.parent.parent);
else if (Table === obj.constructor)
cells = obj.cells.everyItem().getElements();
return cells;
};
/**
* Returns array of things found in `doc`.
* `thingPlural` is a Document property name,
* for example 'formFields' or 'swatches'.
* @author m1b
* @version 2024-05-03
* @param {Document} doc - an Indesign Document.
* @param {String} thingPlural - collective property of Document eg. 'checkBoxes'.
* @returns {?Array<*>} - the things.
*/
function getThingsFromDocument(doc, thingPlural) {
if (
!doc.hasOwnProperty(thingPlural)
|| 'function' !== typeof doc[thingPlural].everyItem
)
return;
var things = doc[thingPlural].everyItem().getElements();
if (
doc.hasOwnProperty('groups')
&& doc.groups.length > 0
&& doc.groups[0].hasOwnProperty(thingPlural)
)
// also collect things in groups
things = things.concat(doc.groups.everyItem()[thingPlural].everyItem().getElements());
if (
doc.hasOwnProperty('stories')
&& doc.stories.length > 0
&& doc.stories[0].hasOwnProperty(thingPlural)
) {
// also collect any anchored things
things = things.concat(doc.stories.everyItem()[thingPlural].everyItem().getElements());
if (doc.stories.everyItem().tables.length > 0)
// also collect any thing in tables
things = things.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem()[thingPlural].everyItem().getElements());
}
return things;
};
/**
* Returns a number, if possible
* @param {String} str
* @returns {Number?}
*/
function asNumber(str) {
var n = Number(str);
if (!isNaN(n))
return n;
};
/**
* Finds or creates a process color swatch.
*
* Color values:
* [gray] (0–100)
* [r,g,b] (0–255)
* [c,m,y,k] (0–100)
*
* @author m1b
* @version 2026-04-02
*
* @param {Document|ColorGroup} container - An InDesign Document or ColorGroup.
* @param {String} name - The swatch name.
* @param {Array<Number>} colorValues - [g], [r,g,b], or [c,m,y,k].
* @returns {Color} The existing or newly created swatch.
*/
function makeSwatch(container, name, colorValues) {
if (!container || !container.isValid)
throw Error('makeSwatch: bad `container` supplied.');
if (!name)
throw Error('makeSwatch: bad `name` supplied.');
if (!colorValues || !colorValues.length)
throw Error('makeSwatch: bad `colorValues` supplied.');
var doc;
var swatch;
// search container by name
if (Document === container.constructor) {
doc = container;
swatch = container.swatches.itemByName(name);
}
else if (ColorGroup === container.constructor) {
// if container is a ColorGroup, its parent is the Document.
doc = container.parent;
swatch = findSwatchInColorGroup(container, name);
}
else {
throw new Error('makeSwatch: bad `container` supplied (' + container + ').');
}
// create it if it doesn't exist
if (!swatch || !swatch.isValid) {
var space;
switch (colorValues.length) {
case 1: space = ColorSpace.GRAY; break;
case 3: space = ColorSpace.RGB; break;
case 4: space = ColorSpace.CMYK; break;
default:
throw Error('makeSwatch: colorValues must have length 1, 3, or 4.');
}
// make the new swatch in the root level
swatch = doc.colors.add({
name: name,
model: ColorModel.PROCESS,
space: space,
colorValue: colorValues
});
if (ColorGroup === container.constructor)
// put it in the group
container.colorGroupSwatches.add(swatch);
}
return swatch;
};
/**
* Return a swatch by name inside a ColorGroup.
* @author m1b
* @version 2026-04-02
* @param {ColorGroup} group - the color group to search.
* @param {String} name - the target swatch name.
* @returns {Swatch?}
*/
function findSwatchInColorGroup(group, name) {
var cgs = group.colorGroupSwatches;
for (var i = 0; i < cgs.length; i++) {
var swatch = cgs[i].swatchItemRef;
if (swatch.name === name)
return swatch;
}
};