Help with the worlds most basic copy & paste loop

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
JackWills
Posts: 3
Joined: Mon Mar 20, 2017 10:30 pm

Help with the worlds most basic copy & paste loop

Post by JackWills »

Hi world,

I am well and truly new to the wonderful world of OpenOffice Macros and I require some level 1 advice.

I am very simply trying to do this: Copy Cell A2, Paste into B2, Copy results in C2 (Which contains a formula based on B2 as one of the inputs), Paste in D2 as Value.. then loop 5 times.

If I was in VBA it would look like:

Dim n As Integer
For n = 2 to 5

Range("A" & n).Select
Selection.Copy
Range("B" & n).Select
ActiveSheet.Paste
Range("C" & n).Select
Selection.Copy
Range("D" & n).Select
Selection.PasteSepcial Paste:=xlPasteValues

Next n

Can anyone please help translate this into the simplest of terms? Once again I apologies for being such a novice, I am very new to this but understand the time savings this could make!

I have tried a few times but I am not sure ("Column" & n) works in openoffice? And I've read that you shouldn't have an "args" within a loop, which begs the question how does one do it in this circumstance!

Thank you again,

Jack J
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: Help with the worlds most basic copy & paste loop

Post by karolus »

Hallo

Code: Select all

def stupid_copy_paste():
    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.CurrentController.ActiveSheet
    rrange = sheet.getCellRangeByName
    for source, target in [('A2:A5', 'B2:B5'), ('C2:C5', 'D2:D5')]:
        rrange(target).DataArray = rrange(source).DataArray
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JackWills
Posts: 3
Joined: Mon Mar 20, 2017 10:30 pm

Re: Help with the worlds most basic copy & paste loop

Post by JackWills »

Hi Karolus, thanks for the reply!

I am afraid it is not as simple as copying them together.. I have simplified in my question but essentially B2 is an input for an extremely large engine in the background which final output is linked in row C. Therefore I can't simply copy all of A2:A5 into B2:B5 and instead it requires a loop so as to place B2 into the engine, computes the result in the engine and links to C2 then copies as values into D2 and then repeats for row 3.

Hope this makes sense, thanks for the help so far.
Open Office 3 on Windows 10
JackWills
Posts: 3
Joined: Mon Mar 20, 2017 10:30 pm

Re: Help with the worlds most basic copy & paste loop

Post by JackWills »

As an update I have tried experimenting with Record Macro and applied some VBA knowledge.

"Dim n as Integer
For n= 2 to 5

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "A" & n

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

next n"

This is the start of what I am trying to build as it is what selects "A2" (will build on with copy/pastes once I've worked this first part out).

I am getting the following error:

BASIC run-time error.
An exception occurred.
Type: com.sun.star.lang.IllegalArgumentException
Message: sequence element is not assignable by given value!
Open Office 3 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Help with the worlds most basic copy & paste loop

Post by JeJe »

Put this before the for loop
dim args1(0) as new com.sun.star.beans.PropertyValue
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply