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

applescript to copy and paste values

Hi everyone,


I am trying to bring an excel sheet over to numbers and I need to replicate paste special.


Can anyone help me with an apple script that copies a range of cells - say a range H3:H16 and pastes to range B3:B16



Any help is appreciated.



Andrew

iMac, OS X Mountain Lion (10.8.2), null

Posted on Jul 27, 2015 3:14 AM

Reply
5 replies

Jul 27, 2015 9:01 AM in response to RedEight

RedEight wrote:



Can anyone help me with an apple script that copies a range of cells - say a range H3:H16 and pastes to range B3:B16



Hi Andrew,


Though the sequence described above is the easiest, and generally you shouldn't need AppleScript, here is a script that addresses your specific question.


SG



set tblName to "Table 1"

set srceRng to "H3:H16"

set destRng to "A3:A16"


tell application "Numbers"

tell document 1 to tell active sheet

tell table tblName

set destCol to range destRng's column 1's address

repeat with c in range srceRng's cells

set v to c's value

set r to c's row's address

set row r's cell destCol's value to v

end repeat

end tell

end tell

end tell

Jul 28, 2015 4:00 AM in response to SGIII

SGIII, thank you, this works perfectly.


It's been 30 years since I used the excel macro language and I have been lost trying to teach myself how to do this.


Is there any way to add the value in H3 to the value in B3 and then make that B3 - effectively replicating the Paste Special Add Values in excel?


Currently i have an additional column in my sheet to achieve this. i.e. B3 =100 G3 = 50 H3 = B3+G3 - This script then copies H3 over B3



Thanks again for your assistance

Jul 28, 2015 7:18 AM in response to RedEight

RedEight wrote:


Is there any way to add the value in H3 to the value in B3 and then make that B3



One way to do that is to modify the script slightly, as below.


SG


set tblName to "Table 1"

set srceRng to "H3:H16"

set destRng to "A3:A16"


tell application "Numbers"

tell document 1 to tell active sheet

tell table tblName

set destCol to range destRng's column 1's address

repeat with c in range srceRng's cells

set v to c's value

set r to c's row's address

tell row r's cell destCol

set its value to (its value) + v

end tell

end repeat

end tell

end tell

end tell

Jul 28, 2015 8:46 AM in response to RedEight

And here's a generalized script, closer to Excel's Paste Special > Add.


  1. Select source cells.
  2. Command-c to copy values to clipboard.
  3. Select destination cells (must be the same number of cells as source cells).
  4. Click triangle 'run' button.


SG


set pasteItems to (the clipboard)'s paragraphs's items 2 thru -3

--> puts cell values copied to clipboard into an AppleScript list


tell application "Numbers"

tell document 1 to tell active sheet

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

set selRng to t'sselection range

if (count pasteItems) is not (count selRng's cells) then return

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

tell selRng's cell i

set its value to (its value) + (pasteItems's item i)

end tell

end repeat

end tell

end tell

applescript to copy and paste values

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