Skip to main content
Participant
November 8, 2019
Question

Need script to find range of numbers in Indesign table

  • November 8, 2019
  • 2 replies
  • 1140 views

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;

}

 
 
  •  
This topic has been closed for replies.

2 replies

Community Expert
November 11, 2019

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

https://community.adobe.com/t5/InDesign/Is-it-possible-to-write-a-script-in-InDesign-to-do-conditional/m-p/10574246#M150783

 

Regards,
Uwe Laubender

( ACP )

Community Expert
November 9, 2019

Hi Sunil,

 

Your code has number of issues, i have fixed them and i am also listing below the issues

  • You were iterating over tables and cells collection, which is fine but inside the loop body you were not accessing the objects using the index operator and hence was calling methods and properties on the tables and cell collection when you had to call them on table and cell instances. So you should have written tables[i] and cells[c] in the loop body
  • You used parseInt on a decimal value, you should use pareFloat
  • Even though it works but i would suggest never to check floating point numbers using an equality operator
  • You created color and stored them in var named green, red, white but when using these colors you used Blue, Red, Green which are obviously undefined

 

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

-Manan
Known Participant
January 29, 2020

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.

Community Expert
January 30, 2020

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

-Manan