CellAddress in Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
jdpipe
Posts: 52
Joined: Wed Jun 04, 2008 8:02 am

CellAddress in Calc

Post by jdpipe »

Hi all

I'm just starting out with some simple OOo BASIC code here, and getting confused about how to do cell addressing in an intuitive way. Can someone please help me fix the following code?

Code: Select all

dim sheet as object
sheet = ThisComponent.Sheets.getByIndex(0)
dim targetpos as new com.sun.star.table.CellAddress
targetpos=sheet.getCellRangeByName("D206").getRangeAddress()
The error is 'Object not accessible. Invalid use of an object'. FWIW I want to use this CellAddress to pass to the 'seekgoal' method.

Cheers
JP
OOo 3.1.1 on Mac OS X 10.5, OOo 3.0.1 on Ubuntu 9.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: CellAddress in Calc

Post by Villeroy »

A com.sun.star.table.CellRangeAddress is another type of struct than com.sun.star.table.CellAddress. A cell has both, a range has a range address only.
For a single cell the following holds true:
CellRangeAddress.StartRow=CellRangeAddress.EndRow=CellAddress.Row
CellRangeAddress.StartColumn=CellRangeAddress.EndColumn=CellAddress.Column

http://api.openoffice.org/docs/common/r ... dress.html
http://api.openoffice.org/docs/common/r ... dress.html

One of my helper functions to get a range (single cell or rectangle) from a given range address within a given document, a sheet or another range.

Code: Select all

'pass a spreadsheet-document, sheet or range.
'return empty if oAddr out of bounds or wrong obj
Function getRangeByAddress(obj, oAddr as com.sun.star.table.CellRangeAddress)
on error goto nullErr:
Dim oSheet
	If obj.supportsService("com.sun.star.sheet.SpreadsheetDocument") then
		REM use the sheet specified by given address
		oSheet = obj.getSheets.getByIndex(oAddr.Sheet)
	else
		REM use given object (range/sheet) as parent range
		oSheet = obj
	endif
	getRangeByAddress = oSheet.getCellRangeByPosition(oAddr.StartColumn,oAddr.StartRow,oAddr.EndColumn,oAddr.EndRow)
exit function
nullErr:
	getRangeByAddress = Null
End Function
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
jdpipe
Posts: 52
Joined: Wed Jun 04, 2008 8:02 am

Re: CellAddress in Calc

Post by jdpipe »

Hi Villeroy

Thanks for that. How about for converting to a CellAddress from a CellRange... how can I do that?
[edit - correction: *to* CellAddress]

Cheers
JP
OOo 3.1.1 on Mac OS X 10.5, OOo 3.0.1 on Ubuntu 9.04
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: CellAddress in Calc

Post by Villeroy »

Code: Select all

oRangeObj.getCellAddress()
and you have it (assumed oRangeObj beeing a single cell)

If you want to be shure to get a CellAddress (just the top-left cell address of a range):

Code: Select all

oCellObj = oRangeObj.getCellByPosition(0,0) 'top-left cell
oCellAddr=oCellObj.getCellAddress()
Or if you want to create a new c.s.s.table.CellAddress from scratch and assign the top-left cell address from a RangeAddress:

Code: Select all

oCA = createUnoStruct("com.sun.star.table.CellAddress")
oCA.Sheet = oRangeAddress.Sheet
oCA.Column = oRangeAddress.StartColumn
oCA.Row = oRangeAddress.StartRow
Or using my helper function:

Code: Select all

oRange = getRangeFromAddress(ThisComponent, oRangeAddress)
oCell = oRange.getCellByPosition(0,0)
oCA = oCell.getCellAddress()
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
Post Reply