| View previous topic :: View next topic |
| Author |
Message |
Arnolf Best Newbie

Joined: 19 Jul 2012 Posts: 3
|
Posted: Thu Jul 19, 2012 11:18 am Post subject: CALC: How to obtain the number of selected cell ranges? |
|
|
Hi,
I want to write an OOo Basic macro that operates on cells selected by the user. It is necessary to check whether the selected cells form a single consistent area or there are many selected cell ranges (a collection).
Could you please give me a tip how to obtain the number of selected cell ranges? It would be also very nice to know how to get access to a particular cell range in the collection.
Moderation probe1: moved to MACROS AND API section, where all macro related questions belong to; edited subject |
|
| Back to top |
|
 |
karolus OOo Advocate

Joined: 22 Jun 2011 Posts: 208
|
Posted: Thu Jul 19, 2012 12:05 pm Post subject: |
|
|
Hallo
| Code: | oselection = thiscomponent.CurrentSelection
if oselection.supportsService("com.sun.star.sheet.SheetCellRanges") then
selections = oselection.createEnumeration()
while selections.hasMoreElements()
singlerange = selections.nextElement()
rem loop over all single ranges
....
|
Karo |
|
| Back to top |
|
 |
Arnolf Best Newbie

Joined: 19 Jul 2012 Posts: 3
|
Posted: Thu Jul 19, 2012 12:25 pm Post subject: |
|
|
Thank you very much!
 |
|
| Back to top |
|
 |
JohnV Administrator

Joined: 07 Mar 2003 Posts: 8984 Location: Lexinton, Kentucky, USA
|
Posted: Thu Jul 19, 2012 2:04 pm Post subject: |
|
|
Karolus and I approached this in very different ways. Here's my version which handles both a single selection (D1:D5 or D1:E5) and multiple selections.
| Code: | Sub MultipleSelections
oDoc = ThisComponent
CS = oDoc.CurrentSelection
On Error GoTo EH
C = CS.Count 'This will throw error if only a single selection.
For x = 0 to C-1
EN = CS.ElementNames(x)
SN = Left(EN,Instr(EN,".")-1
oSheet = oDoc.getSheets.getbyName(SN)
E = oSheet.getCellRangeByName(EN)
REM Do something with the range. I have enumerated the cell content.
For cc = 0 to E.getColumns.Count-1
For rr = 0 to E.getRows.Count-1
oCell = E.getCellByPosition(cc,rr)
print cc,oCell.String
Next rr
Next cc
Next x
END 'If no error
EH: 'We have a single selection
SN = Left(CS.AbsoluteName, Instr(CS.AbsoluteName,".")-1)
SN = Right(SN,Len(SN)-1)
oSheet = oDoc.getSheets.getByName(SN)
E = oSheet.getCellRangeByName(CS.AbsoluteName)
For cc = 0 to E.getColumns.Count-1
For rr = 0 to E.getRows.Count-1
oCell = E.getCellByPosition(cc,rr)
print cc,oCell.String
Next rr
Next cc
End Sub |
|
|
| Back to top |
|
 |
patel Power User

Joined: 14 Apr 2012 Posts: 54 Location: Italy
|
Posted: Fri Jul 20, 2012 4:19 am Post subject: |
|
|
OK JohnV
very simple solution ! _________________ If your problem has been solved please add "[Solved]" to the beginning of your first post title (edit button). |
|
| Back to top |
|
 |
|