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

Need script to find range of numbers in Indesign table

New Here ,
Nov 08, 2019 Nov 08, 2019

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;

}

 
 
  •  
TOPICS
Scripting

Views

803

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 ,
Nov 08, 2019 Nov 08, 2019

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

  • 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

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 ,
Jan 29, 2020 Jan 29, 2020

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.

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 ,
Jan 30, 2020 Jan 30, 2020

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

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 ,
Oct 22, 2020 Oct 22, 2020

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.

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 ,
Oct 23, 2020 Oct 23, 2020

Copy link to clipboard

Copied

LATEST

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 )

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 ,
Nov 11, 2019 Nov 11, 2019

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

https://community.adobe.com/t5/InDesign/Is-it-possible-to-write-a-script-in-InDesign-to-do-condition...

 

Regards,
Uwe Laubender

( ACP )

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