cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Jackie_
Level VI

Replace cell values

Hi,

 

I am trying to replace all the failing cells in the attached data table, which is highlighted in red with 1 and passing cells with 0. 

Jackie__0-1715089228220.png

 

Found this but something doesn't work-

How do you get the color of a specific cell in a data table using JSL? 

 

 

Is there any better way to replace the cell values?

 

Here is what I tried

Names Default To Here( 1 );
dt = Current Data Table();
cols = dt << get column group( "Tests" );

get_cell_color = Function( {col, r}, 

	s = col << get script;

     //Locate argument "Color Cells" in column script

	For( i = 1, i <= N Arg( s ), i++, 

		If( Head Name( Arg( s, i ) ) == "Color Cells", 

			p = i;

			Break();

		)

	);

     //Identify the sublist that contains the color of row r

	L = Arg( Arg( s, p ), 1 );

	For( i = 1, N Items( L ), i++, 

		If( N Row( Loc( L[i][2], r ) ) == 1, 

			color = L[i][1];

			Break();

		)

	);

	color;

);

// 


test_cols = Transform Each( {col}, cols, col << get name );
dt << Select All Rows;
selrow = dt << get Selected Rows();
dt << begin data update;

For Each( {testcol, idx}, test_cols, 

	For Each( {ro, idx2}, selrow, 
	
		
		cellcolor = get_cell_color( Column( testcol ), ro );
		If( !cellcolor == 3, 
		
			Column( testcol )[ro] = 0
		
		)

		;
	)
);
dt << end data update;

Thanks,

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: Replace cell values

This is one way how you could modify the script found from that post

View more...
Names Default To Here(1);

get_colored_cells = Function({dt, colname}, {Default Local},
	colscript = Column(dt, colname) << Get Script;
	l = Substitute(Name Expr(colscript), Expr(New Column()), List());
	colored_cells = {};
	Try(
		colors = l["Color Cells"];
		If(Type(colors[1]) == "List",
			jmptrickery = 1,
			jmptrickery = 0
		);
		For Each({color}, colors,
			If(jmptrickery,
				Insert Into(colored_cells, color[2]),
				Insert Into(colored_cells, color)
			)
		);
	);
	Return(Matrix(colored_cells));
);

colored_to_pass_fail = function({dt, colname}, {Default Local},
	failed_rows = get_colored_cells(dt, colname);
	row_vals = J(1, N Rows(dt), 0);
	row_vals[failed_rows] = 1;

	Column(dt, colname) << set values(row_vals);
	// dt[0, colname] = row_vals`; // or data table subscripting
);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

wait(1);
dt << Select Rows([4,5,6,7, 10,11, 14, 15]);
:height << Color Cells(3);
dt << clear select;
wait(1);


dt << Begin Data Update;
For Each({colname}, dt << Get Column Names("String", Continuous),
	colored_to_pass_fail(dt, colname);
);
dt << End Data Update;

wait(0);
-Jarmo

View solution in original post

3 REPLIES 3
jthi
Super User

Re: Replace cell values

How to get the row number of colored cells in a column and assign it a tag (say "1") in a newly crea... might provide other suggestion. With small modification to create_failed_column_after (you should also rename it) you can get it to update the values instead of creating new column with the values.

-Jarmo
jthi
Super User

Re: Replace cell values

This is one way how you could modify the script found from that post

View more...
Names Default To Here(1);

get_colored_cells = Function({dt, colname}, {Default Local},
	colscript = Column(dt, colname) << Get Script;
	l = Substitute(Name Expr(colscript), Expr(New Column()), List());
	colored_cells = {};
	Try(
		colors = l["Color Cells"];
		If(Type(colors[1]) == "List",
			jmptrickery = 1,
			jmptrickery = 0
		);
		For Each({color}, colors,
			If(jmptrickery,
				Insert Into(colored_cells, color[2]),
				Insert Into(colored_cells, color)
			)
		);
	);
	Return(Matrix(colored_cells));
);

colored_to_pass_fail = function({dt, colname}, {Default Local},
	failed_rows = get_colored_cells(dt, colname);
	row_vals = J(1, N Rows(dt), 0);
	row_vals[failed_rows] = 1;

	Column(dt, colname) << set values(row_vals);
	// dt[0, colname] = row_vals`; // or data table subscripting
);

dt = Open("$SAMPLE_DATA/Big Class.jmp");

wait(1);
dt << Select Rows([4,5,6,7, 10,11, 14, 15]);
:height << Color Cells(3);
dt << clear select;
wait(1);


dt << Begin Data Update;
For Each({colname}, dt << Get Column Names("String", Continuous),
	colored_to_pass_fail(dt, colname);
);
dt << End Data Update;

wait(0);
-Jarmo
Jackie_
Level VI

Re: Replace cell values

Thank Jarmo