1

The AppleScript Numbers dictionary has info on single cells, and a range of cells. I’d like to specify individual, non-contiguous cells.

My example works, but repeating lines like this bugs me. Is there a simple, more elegant/efficient solution? Can the cell names/numbers go in a list? What would the syntax be?

These all give various errors:

set the background color of cell "A1" and "A5" to cellColour 
set the background color of cell "A1" & "A5" to cellColour 
set the background color of cell {“A1”, "A5”} to cellColour 
set the background color of cell {“A1:A5”} to cellColour
set the background color of cell {A1:A5} to cellColour
set the background color of cell (A1,A5) to cellColour
set the background color of cells {A1,A5} to cellColour

My script:

-- Opens a Numbers spreadsheet, then sets non contiguous cells' background colour
set cellColour to {0, 0, 0}

tell application "Numbers"
   activate
     if not (exists document 1) then make new document
       tell the front document
         tell active sheet
           set thisTable to ¬
               make new table with properties ¬
                  {row count:5, column count:3}
           tell thisTable
                set the width of every column to 30
                set the height of every row to 30
                -- make all cells white first
                repeat with i from 1 to the count of cells
                  set the background color of cell i to {65535, 65535, 65535}
                end repeat
                set the background color of cell "A1" to cellColour
                set the background color of cell "A5" to cellColour
                set the background color of cell "B2" to cellColour
                set the background color of cell "C4" to cellColour
           end tell
        end tell
    end tell
end tell

Thanks in advance :-)

1

Try the following:

set thisColor to {32767, 32766, 32767}
set theseCells to {"A1", "A5", "B2", "C4"}

tell application "Numbers"
    activate
    if not (exists document 1) then make new document
    tell the active sheet of the front document to set thisTable to ¬
        make new table with properties {row count:5, column count:3}
    tell thisTable
        set the width of its columns to 30
        set the height of its rows to 30
        set the background color of its cells to {65535, 65535, 65535}
        repeat with aCell in theseCells
            set the background color of cell aCell to thisColor
        end repeat
    end tell
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .