Hi, guys ,I am new to applesccript writing, I just want to format the background color of a table in Numbers.
Divide the table into 3 parts, each part has different color. I divide rows count by 3, then don’t know how the select the first, the second, and the third selection. anyone who can help, thanks!!!
tell application "Numbers"
activate
try
if not (exists document 1) then error number 1000
tell document 1
try
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
on error
error number 1001
end try
set the hotColor to {63222, 45746, 48573} -- red
-- set background color of one third of Rows
tell selectedTable
set maxRow to count rows
set theseRow to maxRow div 3 as integer
set selection range to row "1:(theseRow)" -- I know something wrong here, but don't know how to figure
set the background color of selection range to hotColor
end tell
end tell
end try
end tell
tell application "Numbers"
activate
try
if not (exists document 1) then error number 1000
tell document 1
try
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
on error
error number 1001
end try
set the hotColor to {63222, 45746, 48573} -- red
-- set background color of one third of Rows
tell selectedTable
set maxRow to count rows
set theseRow to maxRow div 3 # as integer # no need div returns an integer
name of last cell of row theseRow
set selection range to range ("A1:" & result)
set the background color of selection range to hotColor
end tell
end tell
end try
end tell
When I tested I discovered that the app doesn’t accept to extract the name of the last cell in the set selection range instruction.
set selection range to range (“A1:” & (get name of last cell of row theseRow))
was my first attempt but it failed. It’s why I split the instruction into two ones.
In such case, I don’t create an explicit variable but use the result one.
Yvan KOENIG (VALLAURIS, France) vendredi 27 mars 2015 10:31:36
Much Much Thanks to Yvan KOENIG, you solved my problem.
and you inspired me to know how to format the second part of the table.
You are awesome!!!
tell application "Numbers"
activate
try
if not (exists document 1) then error number 1000
tell document 1
try
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
on error
error number 1001
end try
set the hotColor to {63222, 45746, 48573} -- red
-- set background color of one third of Rows
tell selectedTable
set maxRow to count rows
set theseRow to maxRow div 3 # as integer # no need div returns an integer
set thisRow to theseRow + 1
set x to name of first cell of row thisRow
set thatRow to theseRow + theseRow
set y to name of last cell of row thatRow
set selection range to range ¬
(x & ":" & y)
set the background color of selection range to hotColor
end tell
end tell
end try
end tell
Your script becomes more concise if, instead of constructing a range, you simply color the rows:
tell application "Numbers"
tell document 1 to tell active sheet
tell (first table whose selection range's class is range)
set rowsInBand to row count div 3
set background color of rows 1 thru rowsInBand to "red"
end tell
end tell
end tell