app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Conditional cell formatting"); function main() {
sel = app.selection[0];
if (sel.constructor.name != "TextFrame") {
alert("Please select the textframe that contains the table.");
exit();
}
// create color swatches
var green = swatchCreator("Blue", [100,88,0,14]);
var red = swatchCreator("Red", [0,100,2,0]);
var white = swatchCreator("Green", [76,0,38,0]);
tables = sel.tables;
for (var i = 0; i < tables.length; i++) {
var cells = tables.cells;
for (var c = 0; c < cells.length; c++) {
var cellValue = parseInt(cells.texts[0].contents);
if (cellValue <= 0.29) {
cells.fillColor = Blue;
continue;
}
if (cellValue >= 0.30 && cellValue <=0.69) {
cells.fillColor = Red;
continue;
}
if (cellValue > 1.00) {
cells.fillColor = Green;
}
}
}
}
function swatchCreator(colorname, values) {
var swatch = app.activeDocument.colors.item(colorname);
if (!swatch.isValid) {
swatch = app.activeDocument.colors.add({
name: colorname,
model: ColorModel.PROCESS,
space: ColorSpace.CMYK,
colorValue: values
});
}
return swatch;
}