Page 1 of 1

[Solved] Undoing Pasted Array of Data Created in Basic

Posted: Mon May 24, 2021 4:03 pm
by sokolowitzky
Hi,
For some various purposes, I create arrays of data in oo basic, like an order of numbers from 0 to 5, using the "for" loop, or some other way and I paste them in the spreadsheet with help of macros.
But, whenever I want to undo this action, I have to undo it, step by step for every single element of array, because, the way I paste this data in spreadsheet is actually one step each time.
Since the openoffice can undo only upto 50 steps, when my array is longer than 50 elements, this action becomes irreversible

I looked for formulas to create imaginary array of data in background and pasting them in one step on the forum, but I am not sure about which method I must look for.

Here is my sample code that I use for creating and pasting an array. Could someone advise me what to do about this?

Edit: I must add one more question here. Let's say I have a text copied in the clipboard from internet which is consisted of 60 sentences and I have chosen 51 of them randomly by help of macros. The case is the same with this one as well, when I wish to paste the text on the spreadsheet, oo basic does this as one step each time. So this means this action becomes technically irreversible.

Code: Select all

sub pastesomedata
dim ocell
dim xx as integer
for xx = 0 to 17
ocell=thiscomponent.currentcontroller.activesheet.getcellbyposition(0, xx)
ocell.value = xx
next
end sub

Re: Pasting Array of Data Created in Background

Posted: Mon May 24, 2021 4:47 pm
by Villeroy

Code: Select all

REM range A1:F4
rg = ThisComponent.Sheets(0).getCellRangeByPosition(0,0,5,3)
Dim da(3) ' rows 0 to 3, each row with 6 values
da(0) = Array(0,1,2,3,4,5)
da(1) = Array(10,11,12,13,14,15)
da(2) = Array(20,21,22,23,24,25)
da(3) = Array(30,31,32,33,34,35)
rg.setDataArray(da())

Re: Pasting Array of Data Created in Background

Posted: Mon May 24, 2021 5:36 pm
by sokolowitzky
Thank you, but when I try to undo this action, the system does not delete the numbers written in the cells.

Re: Pasting Array of Data Created in Background

Posted: Tue May 25, 2021 11:52 am
by JeJe
Why not use a custom property? You can save an array of numbers to a single property by using a non-numeric character as a separator and the split function to retrieve them.

Re: Pasting Array of Data Created in Background

Posted: Tue May 25, 2021 1:18 pm
by sokolowitzky
@Jeje, this sounds like what I want to do actually.
But I don't know how to paste into different cells, lets say "1/2/3/5/5" or "a/b/c/d/e" when they are given in single property.

Re: Pasting Array of Data Created in Background

Posted: Tue May 25, 2021 1:25 pm
by JeJe
items =split( "1/2/3/5/5" ,"/") returns a array of the numbers (as strings). You use the val function to convert each to a number in your loop.

Edit:

XUndoManager allows you undo several actions at once.

https://www.openoffice.org/api/docs/com ... nager.html

Edit2:

I don't know whether you can use the array returned by split with setDataArray. If not you could copy the values over to another array for that.

Re: Pasting Array of Data Created in Background

Posted: Tue May 25, 2021 3:41 pm
by JeJe
Possibly I've misunderstood you with the custom properties suggestion - thinking you wanted to store some information without it being in the document cells.


Using the undomanager you do this, then you can undo all changes as one step.

Code: Select all




sub pastesomedata
dim ocell

thiscomponent.undomanager.enterundocontext("addcells") 'addcells or your chosen name for the operation

for xx = 0 to 17
ocell=thiscomponent.currentcontroller.activesheet.getcellbyposition(0, xx)
ocell.value = xx
next

thiscomponent.undomanager.leaveundocontext
end sub


Re: Pasting Array of Data Created in Background

Posted: Wed May 26, 2021 6:56 am
by sokolowitzky
thank you very much. even though i still wonder how to undo an action with help of undomanager, because when I use "thiscomponent.undomanager.undo()" it gives an error UndoContextNotClosedException.
But I think I'll figure it out soon. thank you for your guidance.

Code: Select all

sub pastesomedata
dim ocell, undomgr
dim xx
thiscomponent.undomanager.enterundocontext("addcells") 'addcells or your chosen name for the operation

for xx = 0 to 17
ocell=thiscomponent.currentcontroller.activesheet.getcellbyposition(0, 5 + xx)
ocell.value = xx
next
thiscomponent.undomanager.leaveundocontext

thiscomponent.getUndoManager().undo()
end sub

Re: Pasting Array of Data Created in Background

Posted: Wed May 26, 2021 11:13 am
by JeJe
It works for me with no error.