Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Question:

Question: Delete some cells numbers with Applescript

Hi!


In Numbers I create three tables (TableOne, TableTwo, TableThree). Each table has 16 row and 3 colomns.


How can I delete the follow cells with Applescript:

TableOne: B2, C2, E1, E3, H2, J1

TableTwo: A1, A2, A3, B1, B2, J2, L1, M2

TableThree: D1, D3, F3, H1, M1, M2


Thanks!

iMac, macOS Sierra (10.12.6)

Posted on

Reply
Question marked as Solved
Answer:
Answer:

Hi Karelias,


I do not understand your question.

Karelias wrote:

In Numbers I create three tables (TableOne, TableTwo, TableThree). Each table has 16 row and 3 colomns.

Like this?

User uploaded file

Columns are A, B, C

How can I delete the follow cells with Applescript:

TableOne: B2, C2, E1, E3, H2, J1

TableTwo: A1, A2, A3, B1, B2, J2, L1, M2

TableThree: D1, D3, F3, H1, M1, M2

Columns D, E, F etc. do not exist.

Please reply with more information so that we can help you.


Regards,

Ian.

Posted on

Question marked as Helpful

Dec 19, 2017 2:43 AM in response to Karelias In response to Karelias

Answering Wayne's question about setting a cell to blank (which preserves formatting) rather than 'deleting' it (which removes formatting and value), if you just want to clear the current value in the cell the script could be as below.


SG


set tblCells to {{"TableOne", {"B2", "C2", "E1", "E3", "H2", "J1"}}, {"TableTwo", {"A1", "A2", "A3", "B1", "B2", "J2", "L1", "M2"}}, {"TableThree", {"D1", "F3", "H1", "M1", "M2"}}}

repeat with i in tblCells

repeat with c in i's item 2's items


deleteCellContents(i'sitem 1 as text, c as text)

end repeat

end repeat

to deleteCellContents(tblName, cellName)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'scellcellName'svalue to missing value

end tell

end deleteCellContents

Question marked as Helpful

Dec 19, 2017 2:43 AM in response to Karelias In response to Karelias

You can try something like the script below. It assumes the tables contain the cells you list. Change the table names and cells in the first line as needed. Be sure to keep the { and } and the ".


SG


set tblCells to {{"TableOne", {"B2", "C2", "E1", "E3", "H2", "J1"}}, {"TableTwo", {"A1", "A2", "A3", "B1", "B2", "J2", "L1", "M2"}}, {"TableThree", {"D1", "F3", "H1", "M1", "M2"}}}


repeat with i in tblCells

repeat with c in i's item 2's items


deleteCellContents(i'sitem 1 as text, c as text)

end repeat

end repeat


to deleteCellContents(tblName, cellName)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'sselection range to t'scellcellName


activate

tell application "System Events" to key code 51 using command down

end tell

end deleteCellContents

There’s more to the conversation

Read all replies
Question marked as Solved

Dec 16, 2017 12:54 AM in response to Karelias In response to Karelias

Hi Karelias,


I do not understand your question.

Karelias wrote:

In Numbers I create three tables (TableOne, TableTwo, TableThree). Each table has 16 row and 3 colomns.

Like this?

User uploaded file

Columns are A, B, C

How can I delete the follow cells with Applescript:

TableOne: B2, C2, E1, E3, H2, J1

TableTwo: A1, A2, A3, B1, B2, J2, L1, M2

TableThree: D1, D3, F3, H1, M1, M2

Columns D, E, F etc. do not exist.

Please reply with more information so that we can help you.


Regards,

Ian.

Dec 16, 2017 12:54 AM

Reply Helpful
Question marked as Helpful

Dec 19, 2017 2:43 AM in response to Karelias In response to Karelias

You can try something like the script below. It assumes the tables contain the cells you list. Change the table names and cells in the first line as needed. Be sure to keep the { and } and the ".


SG


set tblCells to {{"TableOne", {"B2", "C2", "E1", "E3", "H2", "J1"}}, {"TableTwo", {"A1", "A2", "A3", "B1", "B2", "J2", "L1", "M2"}}, {"TableThree", {"D1", "F3", "H1", "M1", "M2"}}}


repeat with i in tblCells

repeat with c in i's item 2's items


deleteCellContents(i'sitem 1 as text, c as text)

end repeat

end repeat


to deleteCellContents(tblName, cellName)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'sselection range to t'scellcellName


activate

tell application "System Events" to key code 51 using command down

end tell

end deleteCellContents

Dec 19, 2017 2:43 AM

Reply Helpful (1)

Dec 16, 2017 7:17 AM in response to SGIII In response to SGIII

SGIII,


I tried this an could never actually set the value of the cell. I find your knowledge of AppleScript very helpful. Generally, to me, Applescripts misses the clarity of other languages, in that, to extend from one thing to the next, it is far less than intuitive.


I do not understand how to address a specific cell. you cleared the value by "typing" the delete key. can you also demonstrate setting the value of the cell to empty string?

Dec 16, 2017 7:17 AM

Reply Helpful
Question marked as Helpful

Dec 19, 2017 2:43 AM in response to Karelias In response to Karelias

Answering Wayne's question about setting a cell to blank (which preserves formatting) rather than 'deleting' it (which removes formatting and value), if you just want to clear the current value in the cell the script could be as below.


SG


set tblCells to {{"TableOne", {"B2", "C2", "E1", "E3", "H2", "J1"}}, {"TableTwo", {"A1", "A2", "A3", "B1", "B2", "J2", "L1", "M2"}}, {"TableThree", {"D1", "F3", "H1", "M1", "M2"}}}

repeat with i in tblCells

repeat with c in i's item 2's items


deleteCellContents(i'sitem 1 as text, c as text)

end repeat

end repeat

to deleteCellContents(tblName, cellName)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'scellcellName'svalue to missing value

end tell

end deleteCellContents

Dec 19, 2017 2:43 AM

Reply Helpful (2)

Dec 18, 2017 9:44 AM in response to Karelias In response to Karelias

Thanks for all! Sorry for my mistake!


I've tried the scripts. In my file, both the scripts preserve the formatting.


If the cell contains a popup menu, both the scrips delete the list of this menu. In this case is it possible to position the cell in "Start with Blank" (which preserves the list)?


Thanks!!!

Dec 18, 2017 9:44 AM

Reply Helpful

Dec 19, 2017 7:43 AM in response to Karelias In response to Karelias

Karelias wrote:


is it possible to position the cell in "Start with Blank" (which preserves the list)?


I haven't found a way to directly control Pop-Pp Menu in AppleScript. But it is possible to set up a Pop-Up Menu in a one-cell table, set to the blank in the Start with Blank and have AppleScript copy-paste that into cells where you want to reset the menu at blank. Here is a simple example that resets the Pop-Up Menu in cells I3 and J3 of a table named 'TableOne'. The one-cell table containing the Pop-Up Menu is named 'MenuCell'.


SG


set menuCell to {"MenuCell", "A1"}

set tblCells to {{"TableOne", {"I3", "J3"}}}

repeat with i in tblCells

repeat with c in i's item 2's items

copyPaste("MenuCell", "A1", "c")

copyPaste(i's item 1 as text, c as text, "v")

end repeat

end repeat

to copyPaste(tblName, cellName, typekey)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'sselection range to t'scellcellName


activate

tell application "System Events" to keystroke typekey using {command down}

end tell

end copyPaste

Dec 19, 2017 7:43 AM

Reply Helpful (1)

Dec 20, 2017 5:46 AM in response to SGIII In response to SGIII

Very Good! Thank you SG!!!


if I wanted to combine the follow two scripts (adding a new cell in the table MenuCell)?

**** 1 ****

set menuCell to {"MenuCell", "A1"}

set tblCells to {{"TableOne", {"I3", "J3"}}}

repeat with i in tblCells

repeat with c in i's item 2's items

copyPaste("MenuCell", "A1", "c")

copyPaste(i's item 1 as text, c as text, "v")

end repeat

end repeat

tocopyPaste(tblName, cellName, typekey)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'sselection rangetot'scellcellName


activate

tell application "System Events" to keystroke typekey using {command down}

end tell

endcopyPaste


**** 2 ****

set menuCell to {"MenuCell", "A2"}

set tblCells to {{"TableTwo", {"I3", "J3"}}}

repeat with i in tblCells

repeat with c in i's item 2's items

copyPaste("MenuCell", "A1", "c")

copyPaste(i's item 1 as text, c as text, "v")

end repeat

end repeat

tocopyPaste(tblName, cellName, typekey)

tell application "Numbers"

set t to front document's active sheet's table tblName

set t'sselection rangetot'scellcellName


activate

tell application "System Events" to keystroke typekey using {command down}

end tell

endcopyPaste

Dec 20, 2017 5:46 AM

Reply Helpful

Dec 20, 2017 10:26 AM in response to SGIII In response to SGIII

I have to set some cells with different menu.


The thing is that I could create many AppleScript for each cell that I need to copy.

But I would like to do everything with one click, basically with one AppleScript.


For this reason I was thinking of creating just one AppleScritp that can copy the cell A1 of the "MenuCell" table and paste it into some cells, then copy cell A2 and paste it into other different cells.


Thanks!!!

Dec 20, 2017 10:26 AM

Reply Helpful
User profile for user: Karelias

Question: Delete some cells numbers with Applescript