Applescript: How to test if a cell is empty in Numbers?

I'm a newbie to applescripts. I want to install an Automator Service to Numbers which implement the following function: mark out the duplicate cells in the selected range of Numbers. So I copy an script from web (Sorry to the author for I can't remember the original URL!) and make it successfully according to the steps provided.

tell application "Numbers" to tell front document to tell active sheet

set selected_table to first table whose class of selection range is range

tell selected_table

set selected_range to the selection range

tell selected_range

set values_list to {}

repeat with i from 1 to countcells

set cv to value of celli as text

if values_list contains (cv) then set background color of celli to "red"

set end of values_list to value of celli as text

end repeat

end tell

end tell

end tell

But I noticed that the empty cells are also marked with red color. So I tried to exclude the empty cell from comparison and test it with following lines as I thought:

if cv is equal to "" then set background color of cell i to "green"

if cell i is empty then ...

if cell i is blank then ...

But none of them do the thing. Can somebody give me some help?

MacBook Air, OS X Yosemite (10.10.4)

Posted on Jul 18, 2015 10:36 PM

Reply
3 replies

Jul 19, 2015 4:21 AM in response to xh.lfree

The test you want to do is for missing value. I've added to your script to exclude empty cells having their background colour set to red, and commented-out an else to colour all empty cells green instead.


The other thing to note is that there's now an intermediate step where cv becomes first the value of the cell (and is then tested for missing value) and then to the text string of its value.


tell application "Numbers" to tell front document to tell active sheet

set selected_table to first table whose class of selection range is range

tell selected_table

set selected_range to the selection range

tell selected_range

set values_list to {}

repeat with i from 1 to count cells

set cv to value of cell i

if cv is not missing value then --if the cell is empty skip it

set cv to cv as text

if values_list contains (cv) then set background color of cell i to "red"

set end of values_list to value of cell i as text


--else --optionally colour green by uncommenting these lines


--set background color of cell i to "green"

end if

end repeat

end tell

end tell

end tell

Jul 20, 2015 9:03 AM in response to xh.lfree

Thanks for pointing out the blank cells issue.


My style has evolved a bit since posting that original script/service over on the Numbers for Mac discussions last year.


Now I'd simply do a shorter script as below...


SG



tell application "Numbers" to tell front document to tell active sheet

set t to first table whose selection range's class is range

set selRng to t'sselection range

set vv to {missing value}

repeat with i from 1 to (count selRng's cells)

tell selRng's cell i

if its value is not in vv then

copy its value to end of vv

else

if its value is not missing value then set its background color to "red"

end if

end tell

end repeat

end tell

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.

Applescript: How to test if a cell is empty in Numbers?

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