[Solved] Looping through filtered results

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
x2rider
Posts: 20
Joined: Wed May 26, 2021 1:55 pm

[Solved] Looping through filtered results

Post by x2rider »

After creating a filter..

Dim oFields(#) As New com.sun.star.sheet.TableFilterField
oFilterDesc = rangeSht1.createFilterDescriptor(True)
'... add filter criteria(s) into oFields...
oFilterDesc.setFilterFields(oFields())
oFilterDesc.ContainsHeader = False
oFilterDesc.UseRegularExpressions = True
rangeSht1.filter(oFilterDesc)

What's the best way to go through the results?

If I try to user rangeSht1.Rows, I get ALL rows, not just the ones left over from filtering.
DataArray also contains ALL of the data before filtering.

So far, I've found RowDescriptions has what I need, but is there something else, or is this the way to do it?
Or am I going about this all wrong to get the data I need?

for each item in rangeSht1.RowDescriptions
rowInfo = Split(item, " ")
rowIndex = clng(rowInfo(1)) - 1

itemValue = wsSheet.getCellByPosition(colIndex, rowIndex).value
...
next
Last edited by MrProgrammer on Fri May 27, 2022 9:15 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
OpenOffice 4.x on Win10 / OpenOffice 4.x on MacOS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Looping through filtered results

Post by Villeroy »

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
x2rider
Posts: 20
Joined: Wed May 26, 2021 1:55 pm

Re: Looping through filtered results

Post by x2rider »

Is there not an easier way of getting the rows, or the data?
Why is there not a queryVisibleRows() ?

What I'm seeing so far, is that I get back an array of cells.

I can create an enumerator, but it goes down the first column, then goes down the next column, and so forth. This is pretty much useless.

The XRanges here does have a data property, but every element in that array is a double. Fields that are numerical are fine, but the string values are all some number (memory reference?) How do you extract the string values?

XRanges also contains a RangeAddresses array, but that requires a bit of logic as some ranges have a single entry, some have multiple, so I'd have to look at each range, use the start/end row of each one.
OpenOffice 4.x on Win10 / OpenOffice 4.x on MacOS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Looping through filtered results

Post by Villeroy »

Get array of visible row indices from XCellRanges:

Code: Select all

Function getVisibleRows(rgs)
a() = rgs.getRangeAddresses()
Dim r()
sr = -1
for each adr in a()
	if sr  > adr.StartRow then exit for
	for i = adr.StartRow to adr.EndRow
		bas_PushArray(r(), i)
	next i
	sr = adr.StartRow
next adr
getVisibleRows = r()
End Function
My helper function PushArray for stupid Basic goes like this:

Code: Select all

REM very simple routine appending some element to an array which can be undimensioned (LBound > UBound)
Sub bas_PushArray(xArray(),vNextElement)
Dim iUB%,iLB%
	iLB = lBound(xArray())
	iUB = uBound(xArray())
	If iLB > iUB then
		iUB = iLB
		redim xArray(iLB To iUB)
	else
		iUB = iUB +1
		redim preserve xArray(iLB To iUB)
	endif
	xArray(iUB) = vNextElement
End Sub
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
FJCC
Moderator
Posts: 9277
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Looping through filtered results

Post by FJCC »

My first thought would be to use the getByIndex() method of the collection of visible cells and use the getDataArray method of each contiguous range. But it is not clear what you want to accomplish. Can you explain more about what you need to do with the data?
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.
x2rider
Posts: 20
Joined: Wed May 26, 2021 1:55 pm

Re: Looping through filtered results

Post by x2rider »

FJCC wrote:My first thought would be to use the getByIndex() method of the collection of visible cells and use the getDataArray method of each contiguous range. But it is not clear what you want to accomplish. Can you explain more about what you need to do with the data?
This spreadsheet is used by a group of people with about 13,000 members, but it's an old organization, so about a few hundred are active at any given time I would say.

Each member has a program they run locally to track contacts they make over the radio. At the end of each active day, they export out their daily contacts, and import them into this spreadsheet. This worksheet contains all of the contacts, and from there, about 50+ scripts can be run against this data for that person to qualify for various awards.

For each award, I need to filter the data based on some criteria, then work against the results like a database. The macro then fills out various forms within the spreadsheet, and once award criteria is met, they can then apply for that particular award. The creator of the spreadsheet was a seasoned Fortran programmer, but yet didn't utilize any of the spreadsheet functions, so most macros are looping over the entire dataset and only acting on data it finds that it needs. I'm updating the spreadsheet to utilize the spreadsheet functions to speed things up. I've already improved the data loader tremendously, but each award script is now being looked at. Once someone gets over a few thousand contacts, it can really slow down the routines, so I'm identifying the routines taking a lot of time and trying to speed them up.

There is an Excel and OpenOffice version of the spreadsheet (PC and Mac as well), so as I make improvements in Excel, I then try to recreate this in the OO version.
This was much easier to do in excel.
OpenOffice 4.x on Win10 / OpenOffice 4.x on MacOS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Looping through filtered results

Post by Villeroy »

Please, remove OpenOffice and do that in Excel!
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
x2rider
Posts: 20
Joined: Wed May 26, 2021 1:55 pm

Re: Looping through filtered results

Post by x2rider »

Here's the thing. Not everyone can afford Excel, therefore OpenOffice is the free alternative for the members.
OpenOffice calc can do it... and IS doing it (granted it takes longer than Excel), but I think once the routines are refined, it'll be just fine.
OpenOffice 4.x on Win10 / OpenOffice 4.x on MacOS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Looping through filtered results

Post by Villeroy »

Many millions of other users ran into the VBA trap. I stop writing macros for anybody on this forum. I'm tired, fed up, disappointed.
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
x2rider
Posts: 20
Joined: Wed May 26, 2021 1:55 pm

Re: Looping through filtered results

Post by x2rider »

Villeroy wrote:Many millions of other users ran into the VBA trap. I stop writing macros for anybody on this forum. I'm tired, fed up, disappointed.
Too bad you feel that way. I haven't tried your script yet, but I do appreciate you throwing that out there, it looks like what I need, you've been very helpful despite the negative rambling ;)
OpenOffice 4.x on Win10 / OpenOffice 4.x on MacOS
Post Reply