[Solved] Use of Calc "Defined Name" in Basic

Discuss the spreadsheet application
Post Reply
Herb40
Posts: 134
Joined: Thu May 08, 2014 3:35 am

[Solved] Use of Calc "Defined Name" in Basic

Post by Herb40 »

There does not appear to be any integration of the Calc "Defined Name" capability with OO Basic; that is, there appears to be no way of accessing a Calc Defined Name value in a Basic macro. Is there some capability that I am just overlooking?

Thanks for the replies. Yes, I was overlooking the capability.
Last edited by Herb40 on Fri Mar 06, 2015 8:42 pm, edited 1 time in total.
OpenOffice 4.1.3 on Windows 10
User avatar
Lupp
Volunteer
Posts: 3693
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Use of Calc "Defined Name" in Basic

Post by Lupp »

You will have to use the document's NamedRanges property as in the following gravely reduced example:

Code: Select all

oDoc = ThisComponent
oRanges = oDoc.NamedRanges
oNamedRange = oRanges.getByName("TestName")
a = oNamedRange.getContent()
I never actually used the named ranges from a macro. Read Andrew Pitonyaks texts on the macro topic (the "book" and the "macro document", both available in odt and in pdf from: http://www.pitonyak.org/oo.php) and search for "NamedRanges" therein if still interested. You may face some problems, however, I think.
Last edited by Lupp on Fri Mar 06, 2015 6:54 pm, edited 1 time in total.
On Windows 10: LibreOffice 25.2.4 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
FJCC
Moderator
Posts: 9548
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Use of Calc "Defined Name" in Basic

Post by FJCC »

Things like this might also be useful

Code: Select all

  oNamedRanges = ThisComponent.NamedRanges
  oObj1 = oNamedRanges.getByName("TheCell")
  oReferredCells = oObj1.getReferredCells()
  

Code: Select all

  oSheets = ThisComponent.getSheets()
  oObj2 = oSheets.getByName("Sheet1")
  oCellRangeByName = oObj2.getCellRangeByName("TheCell")
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Use of Calc "Defined Name" in Basic

Post by Villeroy »

This is how you get the named cell range in most cases without knowing the sheet name:

Code: Select all

Function getNamedCellRange(doc,strName)
xName = doc.NamedRanges.getByName(strName)
getNamedCellRange = xName.getReferredCells()
End Function
The function returns the named cell range even when you move that range, rename or move its containing sheet.
The function returns Null if the range refers to something that is not a absolutely addressed range with 5 $-signs as in $SheetName.$A$1:$X$999 or 3 $-signs for a reference to a single cell as in $SheetName.$A$1.
Any other type of reference with relative elements or without sheet name is ambiguous.
Names may also include constant values, functions and other names.
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
Herb40
Posts: 134
Joined: Thu May 08, 2014 3:35 am

Re: [Solved] Use of Calc "Defined Name" in Basic

Post by Herb40 »

As a relatively new Calc user who uses Defined Names strictly for single cells, I never thought of them as being NamedRanges, so that is what made the material in Andy's book difficult to find. For those like me who use NamedRanges in such a limited way, here is what it takes to use them for single cells:

Suppose I have Defined a Name for a cell and called it "Mode". To get or set the value of cell Mode by using its name, I would need something like:

oDoc = ThisComponent
ModeCell = oDoc.NamedRanges.getByName("Mode").getReferredCells().getCellByPosition(0,0)
s = ModeCell.getString() ' or
v = ModeCell.getValue() ' or
f = ModeCell.getFormula() ' or
ModeCell.setString("Howdy") ' or
ModeCell.setValue(v) ' or
ModeCell.setFormula("=SUM(A1:A10)")

You could use the variable name Mode instead of ModeCell if you wished, but is important to remind yourself that what you have is a cell whose contents must be accessed or changed as shown here.
OpenOffice 4.1.3 on Windows 10
Post Reply