OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

CALC: How to obtain the number of selected cell ranges?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Arnolf Best
Newbie
Newbie


Joined: 19 Jul 2012
Posts: 3

PostPosted: Thu Jul 19, 2012 11:18 am    Post subject: CALC: How to obtain the number of selected cell ranges? Reply with quote

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
View user's profile Send private message
karolus
OOo Advocate
OOo Advocate


Joined: 22 Jun 2011
Posts: 210

PostPosted: Thu Jul 19, 2012 12:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Arnolf Best
Newbie
Newbie


Joined: 19 Jul 2012
Posts: 3

PostPosted: Thu Jul 19, 2012 12:25 pm    Post subject: Reply with quote

Thank you very much!
Very Happy
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Thu Jul 19, 2012 2:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
patel
Power User
Power User


Joined: 14 Apr 2012
Posts: 54
Location: Italy

PostPosted: Fri Jul 20, 2012 4:19 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group