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

A more elegant way to lookup data from a table needed?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
dominicedmonds
General User
General User


Joined: 19 Dec 2006
Posts: 7

PostPosted: Thu Sep 25, 2008 2:38 pm    Post subject: A more elegant way to lookup data from a table needed? Reply with quote

I have written a macro in OOo Basic that concatenates strings from a column of data in a table in one Calc worksheet (email addresses in detailsArray) and places the concatenated result into another column of data in another table (rolegroupArray) on another Calc worksheet.

I am uncertain as to the efficiency and syntactical correctness and would appreciate some guidance on the way I am using, for example, DataArray, getbyname, getcellbyposition. I think that I want something like VLOOKUP() and INDEX() to help me here. Am I getting to the data in a sensible fashion or are there easier and much more concise ways to achieve the same result? The following code works but it feels a little clunky and inefficient ... what do you think?

Code:
REM  *****  BASIC  *****

option base 0

sub init_emails

' routine to initialise the email address of each rolegroup

   Dim rolegroupArray As Object, rolegroupitem As Object
   Dim detailsArray as Object, detailitem As Object
   Dim rolegroupCells As Object
   
   Dim i as Integer, j As Integer

   Dim detailrolegroup as String, detailemails as String, rolegroupname as String

   rolegroupArray = ThisComponent.NamedRanges.getbyname("RoleGroup").ReferredCells
   detailsArray = ThisComponent.NamedRanges.getbyname("Details").ReferredCells

   j = 0 ' counter index into rolegroup array
   ' clear out the old email addresses
   do
      j = j + 1 ' NB: the first row of the array contains column names
      rolegroupArray.getCellByPosition(2, j).String = ""
   loop until ( j = rolegroupArray.Rows.Count - 1 )

   i = 0 ' counter index into details array
   do
      i = i + 1 ' NB: the first row of the array contains column names

      detailitem = detailsArray.DataArray(i)
      detailrolegroup = detailitem(4) ' rolegroup for detailitem
      detailemails = detailitem(32) ' combined emails for detailitem

      if detailemails <> "" then
         j = 0 ' counter index into rolegroup array
         do
            j = j + 1 ' NB: the first row of the array contains column names
            rolegroupitem = rolegroupArray.DataArray(j)
            rolegroupname = rolegroupitem(1) ' rolegroup for rolegroupitem
            if rolegroupname = detailrolegroup then
               rolegroupCells = rolegroupArray.getCellByPosition(2, j)
               sep = ""
               s = rolegroupCells.String
               if s <> "" then ' separate each email address with a comma
                  sep = ", "
               end if
               if Instr(s, detailemails) = 0 then ' check that the email is not duplicated
                  rolegroupCells.String = s & sep & detailemails
               end if
               exit do
            end if
         loop until ( j = rolegroupArray.Rows.Count - 1 ) ' until we have examined eacg record in the rolegroup array
         ' shouldn't ever get here: it means that there is an undefined role group
      end if

   loop until ( i = detailsArray.Rows.Count - 1) ' until we have processed all records in the details array
   
end sub


Many thanks for your patience and/or interest.
_________________
Forwards! In every direction!
Back to top
View user's profile Send private message MSN Messenger
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