Copy link to clipboard
Copied
Hi,
Could someone help me with script to find range of numbers in tables to apply set of colors for cells.
For example
0-29 blue
30-69 Red
70-100 Green
Following is the code where I tried to change colors in cells, but getting Error 21.
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;
}
Copy link to clipboard
Copied
Hi Sunil,
Your code has number of issues, i have fixed them and i am also listing below the issues
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 blue = swatchCreator("Blue", [100,88,0,14]);
var red = swatchCreator("Red", [0,100,2,0]);
var green = swatchCreator("Green", [76,0,38,0]);
tables = sel.tables;
for (var i = 0; i < tables.length; i++)
{
var cells = tables[i].cells;
for (var c = 0; c < cells.length; c++)
{
var cellValue = parseFloat(cells[c].texts[0].contents);
if (cellValue <= 0.29)
{
cells[c].fillColor = blue;
continue;
}
if (cellValue >= 0.30 && cellValue <=0.69)
{
cells[c].fillColor = red;
continue;
}
if (cellValue > 1.00)
{
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;
}
Also look into using the findchangelist script, or using the find change functionality of InDesign to solve this, it would be more easy and less or no code would be needed
https://helpx.adobe.com/indesign/using/find-change.html
-Manan
Copy link to clipboard
Copied
Sorry for the delay, but the changes you have mentioned are quite valuable even though I don't know java script. Could you please guide me how to learn java script which is necessary for Indesign scripting. Basically I'm not developer. Hope you understand. Thanks a lot for your insights on the above script.
Copy link to clipboard
Copied
Hi Sunil,
You could start with basics of javascript there are tons of tutorials on the web for this, w3schools.com would be one site that you could check for this. After that you could look into the Javascript Tools Guide CCpdf and Adobe Intro To Scripting.pdf shipped with Extendscript. Apart from that for the object model help refer
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html
-Manan
Copy link to clipboard
Copied
Hi Manan,
Tried applying linear gradient for the above script but couldn't work. In the above script we have applied solid colors but right now I'm experimenting same colors in gradient fashion.
For example:
Blue should start from 0 and end at 0.3
red should start from 0.31 and end at 0.7
green should start from 0.71 and end at 1.00
all these colors should start and fade out when they reach tip
tried multiple ways but haven't succeed, please guide me if I'm missing something.
Copy link to clipboard
Copied
Hi Sunil,
please open a new thread with the issue you have.
In general applying gradients as fills for table cells is a very "tough" issue and should be discussed on its own.
Thanks,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi together,
seems that somwhow this thread is related:
Is it possible to write a script in InDesign to do conditional formatting in table cells?
danielleg87395190, Aug 14, 2019
Regards,
Uwe Laubender
( ACP )