• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Indesign conditional formatting

Explorer ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Hi all, I have this nice script created with the help of the amazing community.

I need to update it, so that when for example a cell changes into red the cell font color also changes but to white to ensure readability. Can anyone let me know what I need to add in?

 

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", [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;
            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;

}
TOPICS
Scripting

Views

573

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Jun 21, 2021 Jun 21, 2021

Change the following

if (cellValue < 80) {
   cells[c].fillColor = red;
   continue;
}

to

if (cellValue < 80) {
   cells[c].fillColor = red;
   cells[c].texts[0].fillColor = "Paper"
   continue;
}

I have used color Paper, if you need to create your own swatch do it as the other colors are created.

-Manan

Votes

Translate

Translate
Community Expert , Jun 21, 2021 Jun 21, 2021

Try the following

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 black = addBlackTint()
    var red = swatchCreator("Red", [0,100,100,0]);
    var yellow = swatchCreator("Amber", [0,51,99,0]);
    var green = swatchCreator("Green", [69,8,87,0]);
    for (var c = 0; c < cells.length; c++) {
        if(cells[c].contents == "
...

Votes

Translate

Translate
Explorer ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Hi again.

 

Also, what should I script if a cell with no data in it needs to be in black with 20% tint?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Change the following

if (cellValue < 80) {
   cells[c].fillColor = red;
   continue;
}

to

if (cellValue < 80) {
   cells[c].fillColor = red;
   cells[c].texts[0].fillColor = "Paper"
   continue;
}

I have used color Paper, if you need to create your own swatch do it as the other colors are created.

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Thank you for your help Manan, it worked perfectly.

And if I want to change those cells with no value to Black with 20%ink?

Cheers

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

Try the following

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 black = addBlackTint()
    var red = swatchCreator("Red", [0,100,100,0]);
    var yellow = swatchCreator("Amber", [0,51,99,0]);
    var green = swatchCreator("Green", [69,8,87,0]);
    for (var c = 0; c < cells.length; c++) {
        if(cells[c].contents == "")
        {
            cells[c].fillColor = black;
            continue;
        }
        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;
}

function addBlackTint()
{
    var tint = app.activeDocument.tints.item("Black 20");
    if (!tint.isValid) {
        tint = app.activeDocument.tints.add(app.activeDocument.colors.itemByName("Black"), {
            name: "Black 20",
            tintValue: 20
            });
    }
    return tint
}

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 21, 2021 Jun 21, 2021

Copy link to clipboard

Copied

LATEST

Worked like a charm.

 

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines