Numbers checkbox conditional question

I posted this in the wrong forum originally:

I need the cells from the second second table: "Enable Preset Group" to set pre-determined values for the cells "Item 1" though "Item 12". I further need to still be able to check and uncheck the boxes in the "item" cells.

In other words, I click Enable Preset Group 2, this enables Items 2,3,5 and 7. I can then change the values of Items 2,3,5 and 7 by clicking on the button myself, (I don't care what happens to "Enable Preset Group 2 at this point)

Is it possible to do this with Numbers?

Image: !http://files.me.com/calebmouse/ga5zuc!

iMac, Mac OS X (10.5.7)

Posted on Jul 13, 2009 10:58 PM

Reply
12 replies

Jul 14, 2009 5:46 AM in response to calebmouse

I think you could do it with Applescript, with scripts setting the checkboxes instead of using the buttons on your second table. I'm a neophyte with Applescript but it appears that if you clear a cell, put in the value "TRUE" then change the format to a checkbox, you will get a checked checkbox. And you can do the same thing with FALSE to get an unchecked checkbox

tell application "Numbers"
tell document 1 to tell sheet 1 to tell table 1
clear cell "c3"
set value of cell "c3" to "TRUE"
set format of cell "c3" to checkbox
end tell
end tell

Jul 14, 2009 10:06 AM in response to Badunit

Thanks for the assist Badunit!

What I want to know now:

Can I insert an AppleScript button into my Numbers document? I assume I use the inspector to make a button in Numbers to activate an AppleScript, how?

How do I cause the string of code above to modify a range of cells (b3:b6 & b8:b12 ) for instance?

Finally, how do I make that range dependent on values contained in the cells in the (invisible) column C in table 2?


Thank you in advance for your help!

Jul 14, 2009 12:05 PM in response to calebmouse

calebmouse wrote:
Thanks for the assist Badunit!

What I want to know now:

Can I insert an AppleScript button into my Numbers document? I assume I use the inspector to make a button in Numbers to activate an AppleScript, how?


NO
You just may install a script like this one in the Script Menu.

--

--[SCRIPT triggerSeveralBoxes]
(*
Enregistrer le script en tant que Script, Application ou Progiciel : triggerSeveralBoxes.xxx
déplacer le fichier créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:
Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.
cliquer dans la cellule 'Enable Preset Group x'
menu Scripts > Numbers > triggerSeveralBoxes
Le script bascule l'état de la case à cocher adjacente
et fixe l'état des cases du groupe correspondant.
--=====
L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".
+++++++
Save the script as Script, Application or Application Bundle: triggerSeveralBoxes.xxx
Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.
Click in the cell 'Enable Preset Group x'
menu Scripts > Numbers > triggerSeveralBoxes
The script switches the status of the adjacent checkbox
and set accordingly the sttus of boxes of the corresponding group.
--=====
The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
--=====
Yvan KOENIG (Vallauris, FRANCE)
1 juillet 2009
*)
--=====
-- Set these four properties to fit your needs
property docName : "myDoc.numbers"
property sheetName : "Feuille 1"
property masterName : "Tableau 2"
property targetName : "Tableau 1"
on run

set {dName, sName, tName, rName, rowNum1, colNum1, rowNum2, colNum2} to my getSelParams()
if dName is not docName then
if my parleAnglais() then
error "This is not the document “" & docName & "” !"
else
error "Ce n’est pas le document « " & docName & " » !"
end if
end if
if sName is not sheetName then
if my parleAnglais() then
error "This is not the sheet “" & sheetName & "” in the document “" & docName & "” !"
else
error "Ce n’est pas la feuille « " & sheetName & " » du document « " & docName & " » !"
end if
end if
if tName is not masterName then
if my parleAnglais() then
error "This is not the table “" & masterName & "” in the sheet “" & sheetName & "” in the document “" & docName & "” !"
else
error "Ce n’est pas la table « " & masterName & " » de la feuille « " & sheetName & " » du document « " & docName & " » !"
end if
end if
tell application "Numbers" to tell document docName to tell sheet sheetName
if not (exists table targetName) then
if my parleAnglais() then
error "The table “" & targetName & "” in the sheet “" & sheetName & "” in the document “" & docName & "” is unavailable !"
else
error "La table « " & targetName & " » de la feuille « " & sheetName & " » du document « " & docName & " » n’existe pas !"
end if
end if -- not exists …
if colNum1 is not 1 then
if my parleAnglais() then
error "The cursor is out of range !"
else
error "Le curseur est hors zone sensible !"
end if
end if

tell table masterName to tell column (colNum1 + 1)
set vv to not (value of cell rowNum1)
clear cell rowNum1
set value of cell rowNum1 to vv
set format of cell rowNum1 to checkbox
end tell
if rowNum1 = (2 + 1) then -- set values of cells of group 1)
my triggerTarget({1, 4}, vv) (* adjust the list to fit your needs *)
else if rowNum1 = (2 + 2) then -- set values of cells of group 2)
my triggerTarget({2, 3, 5, 7}, vv) (* adjust the list to fit your needs *)
else if rowNum1 = (2 + 3) then -- set values of cells of group 3)
my triggerTarget({6, 9}, vv) (* adjust the list to fit your needs *)
else if rowNum1 = (2 + 4) then -- set values of cells of group 4)
my triggerTarget({8, 10, 11}, vv) (* adjust the list to fit your needs *)
else
if my parleAnglais() then
error "The cursor was out of range !"
else
error "Le curseur est hors zone sensible !"
end if
end if
end tell
end run
--=====
on triggerTarget(group, v)
tell application "Numbers" to tell document docName to tell sheet sheetName to tell table targetName
repeat with g in group
set r to 1 + g
tell column 2
clear cell r
set value of cell r to v
set format of cell r to checkbox
end tell
tell column 3
clear cell r
set value of cell r to v (* maybe you will set it to (not v) *)
set format of cell r to checkbox
end tell
end repeat
end tell
end triggerTarget
--=====
on getSelParams()
local r_Name, t_Name, s_Name, d_Name, col_Num1, row_Num1, col_Num2, row_Num2
set {d_Name, s_Name, t_Name, r_Name} to my getSelection()

if r_Name is missing value then
if my parleAnglais() then
error "No selected cells"
else
error "Il n'y a pas de cellule sélectionnée !"
end if
end if

set two_Names to my decoupe(r_Name, ":")
set {row_Num1, col_Num1} to my decipher(item 1 of two_Names, d_Name, s_Name, t_Name)
if item 2 of two_Names = item 1 of two_Names then
set {row_Num2, col_Num2} to {row_Num1, col_Num1}
else
set {row_Num2, col_Num2} to my decipher(item 2 of two_Names, d_Name, s_Name, t_Name)
end if
return {d_Name, s_Name, t_Name, r_Name, row_Num1, col_Num1, row_Num2, col_Num2}
end getSelParams
--=====
(*
set {rowNumber, columnNumber} to my decipher(cellRef,docName,sheetName,tableName)
apply to named row or named column !
*)
on decipher(n, d, s, t)
tell application "Numbers" to tell document d to tell sheet s to tell table t to return {address of row of cell n, address of column of cell n}
end decipher
--=====
(*
set { d_Name, s_Name, t_Name, r_Name} to my getSelection()
*)
on getSelection()
local _, theRange, theTable, theSheet, theDoc, errMsg, errNum

tell application "Numbers" to tell document 1
repeat with i from 1 to the count of sheets
tell sheet i
set x to the count of tables
if x > 0 then
repeat with y from 1 to x
try
(selection range of table y) as text
on error errMsg number errNum
set {_, theRange, _, theTable, _, theSheet, _, theDoc} to my decoupe(errMsg, quote)
return {theDoc, theSheet, theTable, theRange}
end try
end repeat -- y
end if -- x>0
end tell -- sheet
end repeat -- i
end tell -- document
return {missing value, missing value, missing value, missing value}
end getSelection
--=====
on decoupe(t, d)
local l
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to ""
return l
end decoupe
--=====
on parleAnglais()
local z
try
tell application "Numbers" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
--[/SCRIPT]
--


Yvan KOENIG (from FRANCE mardi 14 juillet 2009 21:05:40)

Jul 14, 2009 5:21 PM in response to calebmouse

If I understand it correctly, using the script provided by Yvan you put your cursor on the cell that has the preset name you wish to use (actually it doesn't care what name is in the cell, it determines it by row number) then you run the script from the script menu. It looks complicated mostly because it checks to ensure you have the correct document, sheet, and table and that the cursor is within the correct range. The cells you want to modify are coded into the script, not taken from your hidden column C in table 2. Applescript can be a little daunting at first if you are not a programmer or haven't done any programming for a while.

Jul 14, 2009 5:45 PM in response to Badunit

I was mostly able to suss out what this did from the script, but while this does answer my question, this exceeds my needs.

I am okay with selecting different scripts from the scripts menu for now, but what I would prefer is for a window to pop up prompting to enable 1-4 presets. Click a radio button (or whatever) and those presets are made from the hidden section of C.

I will continue to plug around with this. It has been over a decade since I tried programming anything so to say I am rusty is a vast overstatement.

Jul 14, 2009 6:03 PM in response to calebmouse

I don't know of a way to make a window pop up via a click in a spreadsheet. I imagine you could write a script that would create a window that asks for user input (i.e., type in a number or make a selection) then runs the correct preset. You would still have to trigger the script from the script menu, though.

You may want to keep some if not all of the checks Yvan put in the script so you don't inadvertently run the script on the wrong document.

Message was edited by: Badunit

Jul 14, 2009 5:57 PM in response to Badunit

That's what I meant. Functionally, you would select the "Presets" script from the menu bar. That would invoke a window with 1-4 radio buttons (or whatever) on it. Clicking those buttons would enable/disable the checkboxes in Table 1: column B based on hidden conditions in Table 2: column C
(or where ever) but selecting different scripts is just fine by me for now.

Jul 14, 2009 7:02 PM in response to calebmouse

Unfortunately a dialog box is limited to three buttons and I don't know enough to create any other kind of input window so this script brings up a dialog window where you type in the preset number.

I hope Yvan will tell me how he posts scripts to these forums without losing the formatting. It is very hard to read when the indents have been removed. I also apologize to him in advance for removing the French from his script.

property docname : "MyDocument.numbers"
property sheetname : "Sheet 1"
property targetName : "Table 1"

on run
tell application "Finder"
set returnvalue to display dialog "Choose Preset (1-4)" default answer "1"
set ChosenPreset to text returned of returnvalue as number
end tell


tell application "Numbers" to tell document docname to tell sheet sheetname
if not (exists table targetName) then
error "The table “" & targetName & "” in the sheet “" & sheetname & "” in the document “" & docname & "” is unavailable !"
end if -- not exists …

if ChosenPreset = 1 then -- set values of cells of group 1)
my triggerTarget({2, 4}, true) (* adjust the list to fit your needs *)
else if ChosenPreset = 2 then -- set values of cells of group 2)
my triggerTarget({2, 3, 5, 7}, true) (* adjust the list to fit your needs *)
else if ChosenPreset = 3 then -- set values of cells of group 3)
my triggerTarget({6, 9}, true) (* adjust the list to fit your needs *)
else if ChosenPreset = 4 then -- set values of cells of group 4)
my triggerTarget({8, 10, 11}, true) (* adjust the list to fit your needs *)
else
error "The Preset was out of Range"
end if
end tell
end run

--=====

on triggerTarget(group, v)
tell application "Numbers" to tell document docname to tell sheet sheetname to tell table targetName
repeat with g in group
tell column 2
clear cell g
set value of cell g to v
set format of cell g to checkbox
end tell
end repeat
end tell
end triggerTarget

Jul 15, 2009 1:27 AM in response to Badunit

(1) I don't understand the problem with an hidden table .

my original script allow us to select the 'title' of the switcher then it does its job.
If complementary parameters must be grabbed from an other area, let me know where they are.
Re reading the original posts, I'm wondering if the hidden column C contains the list of rows of the group.
I will edit my script on this guess.

(2) To keep the formatting, I enclose the script with markers (code) at the beginning ang at the end. CAUTION replace the parenthesis enclosing the word code by curly brackets. I didn't used them here because if I did, they would do their duty and the markers will no longer be visible.

(3) It's true that a standard dialog has only three buttons. To get rid of this limit, I often use the Choose from List function . With it we may choose from a huge list of items.

(4) No problem for your French dropping. Next time I will drop English 😉

(5) There is a typo in your script. Item 2 is available in Group 1 and in Group 2.

Yvan KOENIG (from FRANCE mercredi 15 juillet 2009 10:19:10)

Jul 15, 2009 1:53 AM in response to KOENIG Yvan

Here is the modified version which grabs the groups definitions from the column C.

--

--[SCRIPT triggerSeveralBoxes]
(*
Enregistrer le script en tant que Script, Application ou Progiciel : triggerSeveralBoxes.xxx
déplacer le fichier créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:
Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.
cliquer dans la cellule 'Enable Preset Group x'
menu Scripts > Numbers > triggerSeveralBoxes
Le script bascule l'état de la case à cocher adjacente
et fixe l'état des cases du groupe correspondant.
Les groupes sont définis par le contenu des cellules de la colonne C de la table masterName.
Les numéros d'items sont séparés par le caractère 'souligné' qui n'interfère avec aucun format de données.
--=====
L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".
+++++++
Save the script as Script, Application or Application Bundle: triggerSeveralBoxes.xxx
Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.
Click in the cell 'Enable Preset Group x'
menu Scripts > Numbers > triggerSeveralBoxes
The script switches the status of the adjacent checkbox
and set accordingly the sttus of boxes of the corresponding group.
The groups are defined by the content of cells of column C in the table masterName.
The items numbers are separated by the character undescore so it will not interfere with other formats.
--=====
The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
--=====
Yvan KOENIG (Vallauris, FRANCE)
1 juillet 2009
modified 15 juillet 2009
*)
--=====
-- Set these four properties to fit your needs
property docnames : {"myDoc.numbers", "myDocument.numbers"}
property sheetname : "Feuille 1"
property masterName : "Tableau 2"
property targetName : "Tableau 1"
on run

set {dName, sName, tName, rName, rowNum1, colNum1, rowNum2, colNum2} to my getSelParams()
if dName is not in docnames then
if my parleAnglais() then
error "“" & dName & "” is not a driven document !"
else
error "« " & dName & " » n’est pas un document géré !"
end if
end if
if sName is not sheetname then
if my parleAnglais() then
error "This is not the sheet “" & sheetname & "” in the document “" & dName & "” !"
else
error "Ce n’est pas la feuille « " & sheetname & " » du document « " & dName & " » !"
end if
end if
if tName is not masterName then
if my parleAnglais() then
error "This is not the table “" & masterName & "” in the sheet “" & sheetname & "” in the document “" & dName & "” !"
else
error "Ce n’est pas la table « " & masterName & " » de la feuille « " & sheetname & " » du document « " & dName & " » !"
end if
end if
tell application "Numbers" to tell document dName to tell sheet sheetname
if not (exists table targetName) then
if my parleAnglais() then
error "The table “" & targetName & "” in the sheet “" & sheetname & "” in the document “" & dName & "” is unavailable !"
else
error "La table « " & targetName & " » de la feuille « " & sheetname & " » du document « " & dName & " » n’existe pas !"
end if
end if -- not exists …
if colNum1 is not 1 then
if my parleAnglais() then
error "The cursor is out of range !"
else
error "Le curseur est hors zone sensible !"
end if
end if

tell table masterName to tell column (colNum1 + 1)
set vv to not (value of cell rowNum1)
clear cell rowNum1
set value of cell rowNum1 to vv
set format of cell rowNum1 to checkbox
end tell
tell table masterName to tell column (colNum1 + 2) to set group to value of cell rowNum1
set group to my decoupe(group, "_")
if (rowNum1 > 2) and rowNum1 < 7 then -- set values of cells of group )
my triggerTarget(group, vv, dName)
else
if my parleAnglais() then
error "The cursor was out of range !"
else
error "Le curseur est hors zone sensible !"
end if
end if
end tell
end run
--=====
on triggerTarget(group, v, d)
tell application "Numbers" to tell document d to tell sheet sheetname to tell table targetName
repeat with g in group
set r to 1 + g
tell column 2
clear cell r
set value of cell r to v
set format of cell r to checkbox
end tell
tell column 3
clear cell r
set value of cell r to v (* maybe you will set it to (not v) *)
set format of cell r to checkbox
end tell
end repeat
end tell
end triggerTarget
--=====
on getSelParams()
local r_Name, t_Name, s_Name, d_Name, col_Num1, row_Num1, col_Num2, row_Num2
set {d_Name, s_Name, t_Name, r_Name} to my getSelection()

if r_Name is missing value then
if my parleAnglais() then
error "No selected cells"
else
error "Il n'y a pas de cellule sélectionnée !"
end if
end if

set two_Names to my decoupe(r_Name, ":")
set {row_Num1, col_Num1} to my decipher(item 1 of two_Names, d_Name, s_Name, t_Name)
if item 2 of two_Names = item 1 of two_Names then
set {row_Num2, col_Num2} to {row_Num1, col_Num1}
else
set {row_Num2, col_Num2} to my decipher(item 2 of two_Names, d_Name, s_Name, t_Name)
end if
return {d_Name, s_Name, t_Name, r_Name, row_Num1, col_Num1, row_Num2, col_Num2}
end getSelParams
--=====
(*
set {rowNumber, columnNumber} to my decipher(cellRef,dName,sheetName,tableName)
apply to named row or named column !
*)
on decipher(n, d, s, t)
tell application "Numbers" to tell document d to tell sheet s to tell table t to return {address of row of cell n, address of column of cell n}
end decipher
--=====
(*
set { d_Name, s_Name, t_Name, r_Name} to my getSelection()
*)
on getSelection()
local _, theRange, theTable, theSheet, theDoc, errMsg, errNum

tell application "Numbers" to tell document 1
repeat with i from 1 to the count of sheets
tell sheet i
set x to the count of tables
if x > 0 then
repeat with y from 1 to x
try
(selection range of table y) as text
on error errMsg number errNum
set {_, theRange, _, theTable, _, theSheet, _, theDoc} to my decoupe(errMsg, quote)
return {theDoc, theSheet, theTable, theRange}
end try
end repeat -- y
end if -- x>0
end tell -- sheet
end repeat -- i
end tell -- document
return {missing value, missing value, missing value, missing value}
end getSelection
--=====
on decoupe(t, d)
local l
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to ""
return l
end decoupe
--=====
on parleAnglais()
local z
try
tell application "Numbers" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
--[/SCRIPT]
--


User uploaded file

The script may apply to several documents whose list is defined by the property docNames.

Yvan KOENIG (from FRANCE mercredi 15 juillet 2009 10:53:06)

Jul 15, 2009 3:04 AM in response to KOENIG Yvan

Here is a modified version of Badunit's script.

--

property forYK : true
property docname : ""
property sheetname : ""
property targetName : ""
property masterName : ""
on run
if forYK then
set docname to "MyDoc.numbers"
set sheetname to "Feuille 1"
set targetName to "Tableau 1"
set masterName to "Tableau 2"
else
set docname to "MyDocument.numbers"
set sheetname to "Sheet 1"
set targetName to "Table 1"
set masterName to "Table 2"
end if

set theList to {"Touch Preset 1", "Touch Preset 2", "Touch Preset 3", "Touch Preset 4"}
tell application "Finder"
set returnvalue to choose from list theList default items {item 1 of theList}
if returnvalue is false then error number -128

set ChosenPreset to item -1 of my decoupe(item 1 of returnvalue, space) as number
end tell
log ChosenPreset

tell application "Numbers"
if not (exists document docname) then
error "The document “" & docname & "” is unavailable !"
end if -- not exists …
tell document docname
if not (exists sheet sheetname) then
error "The sheet “" & sheetname & "” in the document “" & docname & "” is unavailable !"
end if -- not exists …
tell sheet sheetname
if not (exists table targetName) then
error "The table “" & targetName & "” in the sheet “" & sheetname & "” in the document “" & docname & "” is unavailable !"
end if -- not exists …
if not (exists table targetName) then
error "The table “" & masterName & "” in the sheet “" & sheetname & "” in the document “" & docname & "” is unavailable !"
end if -- not exists …
set rowNum1 to ChosenPreset + 2
set colNum1 to 1
tell table masterName to tell column (colNum1 + 1)
set vv to not (value of cell rowNum1)
clear cell rowNum1
set value of cell rowNum1 to vv
set format of cell rowNum1 to checkbox
end tell
tell table masterName to tell column (colNum1 + 2) to set group to value of cell rowNum1
set group to my decoupe(group, "_")
if (rowNum1 > 2) and rowNum1 < 7 then -- set values of cells of group )
my triggerTarget(group, vv)
else
error "The Preset was out of Range !"
end if

end tell
end tell
end tell
end run
--=====
on triggerTarget(group, v)
tell application "Numbers" to tell document docname to tell sheet sheetname to tell table targetName
repeat with g in group
set r to 1 + g
tell column 2
clear cell r
set value of cell r to v
set format of cell r to checkbox
end tell
tell column 3
clear cell r
set value of cell r to v
set format of cell r to checkbox
end tell
end repeat
end tell
end triggerTarget
--=====
on decoupe(t, d)
local l
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to ""
return l
end decoupe
--=====
--


Yvan KOENIG (from FRANCE mercredi 15 juillet 2009 12:04:12)

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.

Numbers checkbox conditional question

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