Skip to main content
Participant
August 21, 2023
Question

Help improving script

  • August 21, 2023
  • 3 replies
  • 248 views

I have this script. I need that the cell data changes to white when the cell is red, otherwise stay black. If the cell doesn't have any data the cell background should be black 20%. Hope someone can help me!

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Conditional cell formatting");
function main() {
    sel = app.selection[0];
	var cells = sel.cells
    // create color swatches
    var red = swatchCreator("Red Cell", [210,35,42]);
    var yellow = swatchCreator("Amber", [0,51,99,0]);
    var green = swatchCreator("Green", [69,8,87,0]);
    for (var c = 0; c < cells.length; c++) {
		var cc = cells[c].texts[0].contents.split("%")
		var cellValue = parseInt(cc[0])
		isNaN(cellValue) && cellValue = 0
		if (cellValue < 80) {
             cells[c].fillColor = red; 
             cells[c].texts[0].fillColor = "Paper";
            continue;
        }
        if (cellValue >= 80 && cellValue < 95) {
            cells[c].fillColor = yellow;
            continue;
        }
        if (cellValue >= 95) {
            cells[c].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;

}

 

 

This topic has been closed for replies.

3 replies

Peter Kahrel
Community Expert
Community Expert
August 22, 2023

Sure. But it's very much the same idea.

Peter Kahrel
Community Expert
Community Expert
August 22, 2023

This looks the same as https://community.adobe.com/t5/indesign-discussions/help-with-a-script/m-p/14024587

If that's true, please remove this post.

NJR5F92Author
Participant
August 22, 2023

Its different. The other code engages when reading percentages on a data cell.

This one changes colours and font colour depending if there is something written in the cell or not.

Community Expert
August 22, 2023

Hi @NJR5F92,

The code you posted applies color based on the value of the cell. It doesn't seem to be doing anything that you asked for. Is this the right script that you posted?

-Manan

-Manan
Robert at ID-Tasker
Legend
August 22, 2023

It's an example that OP needs to be altered - but there is a 2nd thread - as mentioned by @Peter Kahrel