Colors for text in cells with script

Hello

In the script below that SGIII wrote to me almost 3 years ago, I would like to be able to change the text colors for each of the 2 different row colors.

Thank you!


The discussion can be found at the following link:

https://discussions.apple.com/thread/8304672?answerId=33110614022#33110614022


set testVal to "Montréal"

set testCol to 2 -- column "B"

set highlightColor1 to {65535, 65535, 2713} --"yellow"

set highlightColor2 to {0, 0, 65416} -- "blue"

tell application "Numbers"

tell front document's active sheet

tell (first table whose selection range's class is range)

repeat with r in rows 2 thru -1 -- skip header row

if r's cell testCol's value contains testVal then

set r'sbackground color to highlightColor1

else

set r'sbackground color to highlightColor2

set r'scells'stext color to highlightColor1

end if

end repeat

end tell

end tell

end tell

iMac, OS X 10.11

Posted on Nov 30, 2020 11:14 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 30, 2020 06:00 PM

Here is a slight mod so you can set text color and background color for the two independently. As written below you'll have yellow text on a blue background for the one and red text on green background for the other.


set testVal to "Montréal"
set testCol to 2 -- column "B"
set textColor1 to {65535, 65535, 2713} --"yellow"
set backgroundColor1 to {0, 0, 65416} -- "blue"
set textColor2 to {65416, 0, 0} -- "red"
set backgroundColor2 to {0, 33768, 0} -- "green"
tell application "Numbers"
	tell front document's active sheet
		tell (first table whose selection range's class is range)
			repeat with r in rows 2 thru -1 -- skip header row
				if r's cell testCol's value contains testVal then
					set r's background color to backgroundColor1
					set r's cells's text color to textColor1
				else
					set r's background color to backgroundColor2
					set r's cells's text color to textColor2
				end if
			end repeat
		end tell
	end tell
end tell



5 replies
Question marked as Top-ranking reply

Nov 30, 2020 06:00 PM in response to Marc-Andr St-Pierre

Here is a slight mod so you can set text color and background color for the two independently. As written below you'll have yellow text on a blue background for the one and red text on green background for the other.


set testVal to "Montréal"
set testCol to 2 -- column "B"
set textColor1 to {65535, 65535, 2713} --"yellow"
set backgroundColor1 to {0, 0, 65416} -- "blue"
set textColor2 to {65416, 0, 0} -- "red"
set backgroundColor2 to {0, 33768, 0} -- "green"
tell application "Numbers"
	tell front document's active sheet
		tell (first table whose selection range's class is range)
			repeat with r in rows 2 thru -1 -- skip header row
				if r's cell testCol's value contains testVal then
					set r's background color to backgroundColor1
					set r's cells's text color to textColor1
				else
					set r's background color to backgroundColor2
					set r's cells's text color to textColor2
				end if
			end repeat
		end tell
	end tell
end tell



Dec 1, 2020 06:47 AM in response to Marc-Andr St-Pierre

Here's a short JavaScript for Applications (JXA) script that does the same thing. The usage is the same except when you paste into Script Editor be sure to change the dropdown in the dropdown in the upper left to JavaScript.


const testval="Montréal"
const testcol = "B"  // change column as needed
const txtcolor1 = [65535, 65535, 2713]  // yellow
const bkgcolor1 = [0, 0, 65416]         // blue
const txtcolor2 = [65416, 0, 0]         // red
const bkgcolor2 = [0, 33768, 0]         //green

const tbls=Application("Numbers").documents[0].activeSheet.tables,
	  seltbl=tbls[tbls.selectionRange().findIndex(el => !!el)],
	  status = seltbl.columns[testcol].cells.value();

for (let i=seltbl.headerRowCount(); i<seltbl.rowCount(); i++) {
       if (status[i]===testval) {
               seltbl.rows[i].backgroundColor=bkgcolor1; 
	           seltbl.rows[i].textColor=txtcolor1;
			} else {
			   seltbl.rows[i].backgroundColor=bkgcolor2; 
	           seltbl.rows[i].textColor=txtcolor2;
			}
}


You can find the values for a color you like by selecting a cell with its fill set to that color and running the AppleScript in the original thread for that purpose.


Or you can run this JavaScript equivalent:


const tbls=Application("Numbers").documents[0].activeSheet.tables,
	  seltbl=tbls[tbls.selectionRange().findIndex(el => !!el)];
	  selrng=seltbl.selectionRange(),
	  getjscolor = range => range.backgroundColor(),  //null if no fill
	  jscolor2as = arr => arr? arr.map(el => Math.trunc(el * 65535)) : null;

let jscolor = getjscolor(selrng);
let ascolor = jscolor2as(jscolor);

ascolor  // to display in result window of Script Editor


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Colors for text in cells with script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.