[Solved] Undoing Pasted Array of Data Created in Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

[Solved] Undoing Pasted Array of Data Created in Basic

Post 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
Last edited by sokolowitzky on Fri May 28, 2021 7:13 am, edited 3 times in total.
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pasting Array of Data Created in Background

Post 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())
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Pasting Array of Data Created in Background

Post by sokolowitzky »

Thank you, but when I try to undo this action, the system does not delete the numbers written in the cells.
Win10-OpenOffice 4.1/LibreOffice 7.4
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Pasting Array of Data Created in Background

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Pasting Array of Data Created in Background

Post 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.
Win10-OpenOffice 4.1/LibreOffice 7.4
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Pasting Array of Data Created in Background

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Pasting Array of Data Created in Background

Post 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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Pasting Array of Data Created in Background

Post 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
Win10-OpenOffice 4.1/LibreOffice 7.4
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Pasting Array of Data Created in Background

Post by JeJe »

It works for me with no error.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply