Page 1 of 1

Help with the worlds most basic copy & paste loop

Posted: Mon Mar 20, 2017 10:45 pm
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

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

Posted: Tue Mar 21, 2017 12:13 am
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

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

Posted: Tue Mar 21, 2017 12:19 am
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.

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

Posted: Tue Mar 21, 2017 12:24 am
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!

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

Posted: Tue Mar 21, 2017 1:58 am
by JeJe
Put this before the for loop
dim args1(0) as new com.sun.star.beans.PropertyValue