Apple Event: May 7th at 7 am PT

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

Numbers AppleScript assistance needed for copying cell values

Hi All,


I am trying to copy values of cells of 3 rows, into another table that contains 8 rows.

I want to copy them into the first 3 rows, leaving the other rows undisturbed.


My script does the copying fine, but


  1. Should start row 2, column A, but starts in row 1, column F
  2. After pasting into column A,B,C, should go back to column A, and down 1 row, But continues to paste the data into row 4,5,6,7,8 and I cannot figure out how to force the script to go back to column A, after pasting the 3 cells.
  3. Also for some reason I cannot get the actual copying to take place using the sheet names, but only sheet numbers?



-- Adds new rows at the bottom

set antalrowws to row count of table "Mainstatements" of sheet "MainaccountPrep"

set startrows to row count of table "Transactions" of sheet "ProcessPrep"


repeat with r from 1 to antalrowws

add row below last row of table "Transactions" of sheet "ProcessPrep"

end repeat

--Selects all cells and copies them from Mainaccountprep into ProcessPrep

set srcRangeValues to value of cells of cell range of table "Mainstatements" of sheet 8

set destCellList to cells of cell range of table "Transactions" of sheet 7


repeat with i from 1 to length of destCellList

set value of item (i + startrows) of destCellList to item i of srcRangeValues

end repeat





Can anybody help me?


Alternatively if anybody has a script for simply copying the first 3 columns into another table, that would make it all so much simpler ;-)



Best...Jan

Posted on Jan 3, 2022 4:04 AM

Reply
Question marked as Best reply

Posted on Jan 3, 2022 7:33 AM

jklarsen wrote:

if anybody has a script for simply copying the first 3 columns into another table, that would make it all so much simpler ;-)


Does this do what you want? Change "My Source Table", "My Source Sheet", "My Destination Table", "My Destination Sheet" to match the names in your document. Your source and destination sheet would be the same if you have both tables on the same sheet.


The -1 looks strange but it simply means "the last" cell.


tell front document of application "Numbers"
	set sourceTable to table "My Source Table" of sheet "My Source Sheet"
	set destTable to table "My Destination Table" of sheet "My Destination Sheet"
	
	tell sourceTable
		set sourceData to formatted value of cells 1 thru -1 of columns 1 thru 3 whose value is not missing value
	end tell
	
	tell destTable
		repeat with c from 1 to 3
			repeat with r from 1 to length of item 1 of sourceData
				-- the +1 skips one Header Row
				set value of cell (r + 1) of column c to item r of item c of sourceData
			end repeat
		end repeat
	end tell
end tell


SG

Similar questions

3 replies
Question marked as Best reply

Jan 3, 2022 7:33 AM in response to jklarsen

jklarsen wrote:

if anybody has a script for simply copying the first 3 columns into another table, that would make it all so much simpler ;-)


Does this do what you want? Change "My Source Table", "My Source Sheet", "My Destination Table", "My Destination Sheet" to match the names in your document. Your source and destination sheet would be the same if you have both tables on the same sheet.


The -1 looks strange but it simply means "the last" cell.


tell front document of application "Numbers"
	set sourceTable to table "My Source Table" of sheet "My Source Sheet"
	set destTable to table "My Destination Table" of sheet "My Destination Sheet"
	
	tell sourceTable
		set sourceData to formatted value of cells 1 thru -1 of columns 1 thru 3 whose value is not missing value
	end tell
	
	tell destTable
		repeat with c from 1 to 3
			repeat with r from 1 to length of item 1 of sourceData
				-- the +1 skips one Header Row
				set value of cell (r + 1) of column c to item r of item c of sourceData
			end repeat
		end repeat
	end tell
end tell


SG

Numbers AppleScript assistance needed for copying cell values

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