Ben Holness
Ben Holness

Reputation: 2717

How do I put text into the cell to the left of the currently selected cell with a libreoffice macro?

I am trying to write a macro that takes a string from the currently selected cell(s) and puts a modified version of that string into the cell one to the left of the selected cell(s).

My Google-fu so far has gotten me as far as getting the contents of the selected cell and manipulate the string so it's ready to go into the cell one to the left as follows:

oCell = ThisComponent.getCurrentSelection()
dim d1 as string
dim d2 as string
dim d3 as string
dim d4 as string
dim d5 as string

d1=oCell.getString()
d2=mid(d1,1,2)
d3=mid(d1,4,2)
d4=mid(d1,7,4)
d5=d4+d3+d2

But I can't work out how to put d5 into the cell one to the left of oCell

I haven't even started to look at how to make this work for a range of selected cells yet, I figured I'd get it working for one cell first :)

Upvotes: 1

Views: 815

Answers (1)

Lyrl
Lyrl

Reputation: 935

A good resource for OpenOffice APIs is the book OpenOffice Macros Explained by Andrew Pitonyak, available as a free download from his website.

Another resource is the Xray tool, available from Bernard Marcelly's website. Below is a picture of the Xray tool inspecting the oCell object, showing it has a method .getCellAddress that returns a struct with properties Column, Row, and Sheet.

Xray of oCell

Knowing this, you can see that code something like this would get you the cell to the left (of course you'll need to add an error check that you're not already on the leftmost column):

oAddress = oCell.getCellAddress()
oLeftCell = ThisComponent.Sheets(oAddress.Sheet).getCellByPosition(oAddress.Column-1,oAddress.Row)
oLeftCell.setString(d5)

Upvotes: 2

Related Questions