AppleScript Excel Numbers Copy rows

Hi,
How to copy rows in AppleScript A2 and D2 and E2 to Variable, Paste in 3 different places for example to Pages. Again copy the A3 and D2 and E3 positions and again ....

Numbers.app is quite scriptable. You can do this sort of thing:

tell application "Numbers"
	tell document 1
		tell active sheet
			tell table 1
				value of cell 2 of column 2
			end tell
		end tell
	end tell
end tell

Yep, Excel is quite scriptable too.

tell application "Microsoft Excel"
	tell workbook 1
		tell sheet 1
			value of cell "A4"
		end tell
	end tell
end tell

This is my macro
Wypełnianie Excel Clib###.kmmacros (58.3 KB)

Is there any other method of copying data from Excel to Safari? Another method than mine. How to copy this data? Only copying (another way, another method).

It is my database in excel:

If it was me, I would try selecting all the cells, copying them, and then processing the resulting text.

You don't need to Select a field and the insert by pasting, you can set the field directly.

Also, since what you are extracting is just plain text, you should use variables instead of named clipboards.

Instead of

I would do:

And then later:

But the primary question to ask is: Is what you have working? If so, then other than to improve for next time, why worry about what is better?

"If it was me, I would try selecting all the cells, copying them, and then processing the resulting text.

You don’t need to Select a field and the insert by pasting, you can set the field directly." - This is the best solution, but I have no idea how to do it :frowning:

Give this version of the macro a try and see if it does what you want. If you need to alter it to suit your needs and aren't sure how to do so, feel free to ask.

Wypełnianie Excel Clib### 2.0.kmmacros (54.3 KB)

1 Like

Thank you very much for the help, it helped a lot :grinning:

1 Like

Can you help me with one more thing? How to write for "Numbers"?

tell application "Microsoft Excel"
   activate
   delay 0.1
   set RowNumber to (get first row index of active cell)
   set ARange to "A" & RowNumber
   set CRange to "C" & RowNumber
   set DRange to "D" & RowNumber
   set ERange to "E" & RowNumber
   set FRange to "F" & RowNumber
   set GRange to "G" & RowNumber
   set HRange to "H" & RowNumber
   set IRange to "I" & RowNumber
   set AContents to value of range ARange
   set CContents to value of range CRange
   set DContents to value of range DRange
   set EContents to value of range ERange
   set FContents to value of range FRange
   set GContents to value of range GRange
   set HContents to value of range HRange
   set IContents to value of range IRange
   select (get offset active cell row offset 1) #This is the line used to automatically select the next row in Excel. Disable it by prefacing it with a hashtag symbol as per the above instructions if you'd rather select the row manually each time.	
end tell

tell application "Keyboard Maestro Engine"
   setvariable "numer_zdjecia" to AContents
   setvariable "nazwa_czesci" to CContents
   setvariable "nazwa_czesci_2" to DContents
   setvariable "cena" to EContents
   setvariable "ilosc_sztuk" to FContents
   setvariable "ilosc_zdjec" to GContents
   setvariable "stan" to HContents
   setvariable "kategoria" to IContents
end tell

I don’t have nearly as much experience scripting Numbers as I do Excel, but this script, while certainly not the most elegant thing in the world, seemed to accomplish the same thing as the Excel version in my testing:

tell application "Numbers"
  tell document 1
    tell active sheet
      tell table 1
        set RowNumber to (get address of row of selection range) as integer
        set ACell to "A" & RowNumber
        set CCell to "C" & RowNumber
        set DCell to "D" & RowNumber
        set ECell to "E" & RowNumber
        set FCell to "F" & RowNumber
        set GCell to "G" & RowNumber
        set HCell to "H" & RowNumber
        set ICell to "I" & RowNumber
        set AContents to value of cell ACell
        set CContents to value of cell CCell
        set DContents to value of cell DCell
        set EContents to value of cell ECell
        set FContents to value of cell FCell
        set GContents to value of cell GCell
        set HContents to value of cell HCell
        set IContents to value of cell ICell
        set RowNumber to RowNumber + 1
        set selection range to range ("A" & RowNumber & ":A" & RowNumber)
      end tell
    end tell
  end tell
end tell

Try replacing the first section of the script with this (making sure to leave the part that begins tell application "Keyboard Maestro Engine" alone) and see if it does the trick.

(Incidentally, I’m quite sure you don’t need the “activate” or “delay” commands when using these scripts, since unlike standard copying, they don’t require Excel or Numbers to be the frontmost application to work)

Since I just ran into this when googling how to copy a row in Excel with Applescript, and then eventually figured out the solution, I like to share it here for "eternity".

The following duplicates the first sheet's row 3 into row 4. This operation also properly updates any formulas that reference neighboring cells, which is what I needed.

Tested with Excel 16.16.25, in Script Debugger

tell application id "com.microsoft.Excel" -- Microsoft Excel
	tell sheet 1
		set src to row 3
		set dst to row 4
	end tell
	copy range src destination dst
	return "done" -- without this, Excel would crash when Script Debugger tries to display the copied result.
end tell
1 Like

Hey @Thomas_Tempelmann,

Welcome!

And thanks.

-Chris