Ask Your Question
0

Calc Basic - Access filtered data from macro code

asked 2018-10-18 11:57:49 +0200

XanderN gravatar image

updated 2018-10-22 13:02:24 +0200

Today I stood into another problem while helping in the creating of a Calc App powered by Basic macros.

The thing is, I'm using calc as a database and I have a sheet where I save all the input data as if it was a database table, and here lies the problem: I search row by row "manually" to find the row from where I get the rest of the data saved, but it is painfully slow, so I though using Calc Filters to speed up the search to only filtered data. Making the filters themselves is relatively "easy" but accessing to that filtered data on the macro is getting really tedious.

I have been reading the helping document called CalcAsASimpleDatabase and in page 17 it mentions how to copy the filtered data to another part of the document, but it never mentions how to just keep the Cell Range as a variable from which I can then rescue the data I need to show in another place.

Here is a simplified version of the db-like sheet I'm using as table for storing the data: Simplified table with autofilters

And here the filtered data I would like to rescue from it:

Filtered Data I would like to access in basic

That said, I would rescue more than 1 row of data in the real one, but for easiness here is just 1 row.

Thanks in advance for the help, I hope I have been clear, if not please let me know.

==== EDIT ====

First, here's a reinterpretation of the simplified version of what I'm making, which should make it easier to understand what I'm trying to achieve:

Simplified table adjusted to the "Form" Sheet

The code then read the "Form" Year, Month and Zone and uses functions to fill first the users data and days making a fillable calendar of some sort, which then is filled with data that was already saved, if it existed that is. With the info we have in Data, it would look like this:

Form sheet after loading data from Data sheet

As @Mike Kaganski suggested, here's the code that fills the data for each user (the user list is first generated from another sheet using another function) and that I'm pretty sure isn't optimized at all, it uses other functions and the macro should have globals which actually doesn't have, but at least gives a vision of how I'm loading the data right now, fullfilling Mike's suggestion:

Sub fillSavedData(month as string, year as string,zone as string)
dim origin,destiny, southzone as string
dim i,j,k,n,col, row, endrow,int,startrow,startcol, endcol, totrows as integer
'Where we are going to start filling data in the sheet
startcol=2
startrow=7
'Row numbers in Form sheet
i=startrow
do while isLastRegister(0,i,0)=false 'sheet, row, column; return true if the cell has data or false if not
 endrow=endrow+1
 i=i+1
loop
endrow=endrow+startrow
'we go through the Data sheet
k=startrow
southzone=zone
totrows=nrows(2,0,0)
endcol=daysOnMonth(month,year) 'month ...
(more)
edit retag flag offensive close merge delete

Comments

Besides asking for help with a specific problem on your way to the solution you imagined, it would also help to post initial solution that was "painfully slow", to see if someone would come with an advise how to improve that one without using UI filtering (which is simply rows with height=0).

Mike Kaganski gravatar imageMike Kaganski ( 2018-10-18 12:02:11 +0200 )edit

Thanks for the fast response. Well my plan was to use sheet filters described on the document linked above, yet I don't know if they differ or are the same as UI Filters, just used them for a fast and easy way to represent what I want to achieve.

The code itself is in Spanish, and depends on other handmade functions, I will try my best to translate and correctly comment it prior posting it.

Again thanks for your time!

XanderN gravatar imageXanderN ( 2018-10-18 12:21:17 +0200 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2018-10-18 12:46:09 +0200

Lupp gravatar image

updated 2018-10-22 15:46:03 +0200

Kind of advice in advance:
If you are decisive about working with custom code there are three things you should know:
-1- If you visually (UI) select a filtered range, the ThisComponent.CurrentSelection will only contain the subranges not filtered away (also invisible then).
-2- If you create a com.sun.star.sheet.SheetCellRange object (by mySheet.GetCellRangeByName() e.g.) it has a .Rows property giving acces to each row intersecting the range. Each row object has a Boolean property .IsFiltered having the Value True for rows "filtered" away and False otherwise.
-3- Rows hidden without using a filter (by direct user action e.g.) are not set to .IsFiltered=True. They also have their original .Height settings, but are set to IsVisisble=Fale.

===Amendment1===
I don't know a comfortable way to just get unfiltered rows.
There is a CellRange method to get the visible parts: theRange.QueryVisibleCells(). If you can assure that there are no rows hidden though not filtered, you may do something like demonstrated in the attached example.
(Attachment updated 2018-10-19 15:22 CEST)

===Amendment2===
See the new demo announced in my recent comment.

Please report if you find a better way.

edit flag offensive delete link more

Comments

Thanks a lot for the advices! Yeah that aproaches to what I want to achieve, but I guess there is no way to get only visible (IsFiltered=False) rows without checking each row from a selected range isn't it? That's what I'm trying to achieve so I have a CellRange with only 50 rows instead of 5000 rows.

XanderN gravatar imageXanderN ( 2018-10-18 13:25:35 +0200 )edit

Quoting @XanderN: "...from a selected range..."
Here applies what I wrote under -1-: If the selection actually intersects at least one filtered row you don't get a ShetCellRange object as the CurrentSelection, but a SheetCellRanges (Regard the plural!) object. The ranges accessible this way no longer have intersections with filtered rows. You don't need to inspect every single row, but of course, the treatment of an arbitrary number of ranges is somehow more complicated.

Lupp gravatar imageLupp ( 2018-10-18 17:35:55 +0200 )edit

My bad for not specifying how I select the range, I meant to select the range using Sheet(x).getCellRangeByPosition(a, b, c, d) in basic code, instead of UI selecting as macro recorder does

XanderN gravatar imageXanderN ( 2018-10-19 09:26:00 +0200 )edit

Well, such a range is not selected at all.
See also the amendment to my answer.

Lupp gravatar imageLupp ( 2018-10-19 13:55:31 +0200 )edit

The data in the sheet will be only touched by macros, so it would be safe to filter as in your admendment example.

I will try filtering the sheet by macro, then using range.QueryVisibleCells() to reduce the search's range, and deleting the filter before the end so I can create new filters for new searchs and post my findings here.

Thanks a lot for the time and dedication put into giving a factible solution to what I was looking for!

XanderN gravatar imageXanderN ( 2018-10-22 13:18:15 +0200 )edit

If you also do the filtering exclusively by macros I would suggest you don't let the filter hide rows.
Set it up instead to write the resulting data to a dedicated range, best in a different sheet.
If you want to work on the data in addition, you get them by myDataArray = targetRange.GetDataArray
See Sub filterAndCopyResults contained in the new demo.

Lupp gravatar imageLupp ( 2018-10-22 15:43:22 +0200 )edit
0

answered 2018-10-19 18:47:20 +0200

EasyTrieve gravatar image

I think you probably don't want to hear this, but may I be the first to gently suggest that you consider moving to a database.

In a database you can have indexes to search for things. This makes searching blindingly fast.

While it's true that a hammer can solve almost any problem, if you're trying to turn in a screw, a screw driver is really what you should be using.

To help you get started I put this page together. So far it has over 7000 views.

Please consider this direction. I think in the long run you'll be much happier.

edit flag offensive delete link more

Comments

I first considered to make an "app" that made use of Calc and Base, as Spreadsheets were needed for some operations, but after looking into Base I decided the interoperability between Base and Calc wasn't as good as needed, and seeing that native Base DB isn't multi-user, I deprecated the idea, although it would be ideal for cases like the one I'm trying to resolve here. In any case, I'm a total newbie at Base, so correct me if I'm wrong and thanks for the suggestion.

XanderN gravatar imageXanderN ( 2018-10-22 13:09:41 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-10-18 11:57:49 +0200

Seen: 112 times

Last updated: Oct 22 '18