Bug 148679 - Database range (and its column labels / totals settings) is not considered when expanding the range for sorting
Summary: Database range (and its column labels / totals settings) is not considered wh...
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Calc (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL: https://ask.libreoffice.org/t/how-do-...
Whiteboard:
Keywords: difficultyBeginner, easyHack, skillCpp
Depends on:
Blocks: Calc-DataRange Dev-related
  Show dependency treegraph
 
Reported: 2022-04-20 07:09 UTC by Mike Kaganski
Modified: 2024-06-08 12:47 UTC (History)
6 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mike Kaganski 2022-04-20 07:09:33 UTC
1. In a spreadsheet, create a rectangular data block with all cells having only text (no numbers).
2. Define a database range for this block (select it, Data->Define Range), and for the newly created range, make sure that "Contains column labels" is checked under Options.
3. Unselect the range; click on any cell inside the range, and click on Sort Ascending or Sort Descending toolbar buttons.

The range gest automatically selected including all adjacent non-empty cells; and it is sorted as a whole, including the first row, despite the data range for this selection defined the first row to be labels.

1. When the selected area is the same as some database range, sorting must not sort first row when it's defined as column labels;
2. When the selected area is the same as some database range, sorting must not sort last row when it's defined as totals;
3. When expanding selection, database range in the place of selection should be considered (in case when e.g. database range is smaller/larger than contiguous block).
Comment 1 Mike Kaganski 2022-04-20 07:22:02 UTC
Code pointers:

1. Database range is available through ScDocument::GetDBAtCursor / ScDocument::GetDBAtArea.

2. Checking if the range has a column header is done in ScTable::HasColHeader (sc/source/core/data/table3.cxx).

3. Expansion of the area happens in ScTable::GetDataArea (sc/source/core/data/table1.cxx).
Comment 2 Vladimir Sokolinskiy 2022-04-23 09:55:19 UTC
The effect indicated by Mikе occurs after reopening the file in the case when the entire database range is immediately selected.
Comment 3 Zain Iftikhar 2022-05-04 19:30:13 UTC
(In reply to Mike Kaganski from comment #0)
> 1. In a spreadsheet, create a rectangular data block with all cells having
> only text (no numbers).
> 2. Define a database range for this block (select it, Data->Define Range),
> and for the newly created range, make sure that "Contains column labels" is
> checked under Options.
> 3. Unselect the range; click on any cell inside the range, and click on Sort
> Ascending or Sort Descending toolbar buttons.
> 
> The range gest automatically selected including all adjacent non-empty
> cells; and it is sorted as a whole, including the first row, despite the
> data range for this selection defined the first row to be labels.
> 
> 1. When the selected area is the same as some database range, sorting must
> not sort first row when it's defined as column labels;
> 2. When the selected area is the same as some database range, sorting must
> not sort last row when it's defined as totals;
> 3. When expanding selection, database range in the place of selection should
> be considered (in case when e.g. database range is smaller/larger than
> contiguous block).

Forgive me if this is a stupid question.
Clarification is needed for point 3 i.e expanding selection
As I understand from this blog https://help.libreoffice.org/6.2/en-US/text/scalc/guide/database_sort.html?&DbPAR=WRITER&System=UNIX 
i.e 
If you select a range of cells, only these cells will get sorted then why is point 3 saying that database range in the place of selection should be considered.
Comment 4 Mike Kaganski 2022-05-04 21:02:13 UTC
(In reply to Zain Iftikhar from comment #3)
> Clarification is needed for point 3 i.e expanding selection
> As I understand from this blog
> https://help.libreoffice.org/6.2/en-US/text/scalc/guide/database_sort.
> html?&DbPAR=WRITER&System=UNIX 
> i.e 
> If you select a range of cells, only these cells will get sorted then why is
> point 3 saying that database range in the place of selection should be
> considered.

Does this work for sort ascending/descending toolbar buttons as expected - especially in a scenario when the range is not equal to adjacent range?
Comment 5 Zain Iftikhar 2022-05-05 06:53:21 UTC
(In reply to Mike Kaganski from comment #4)
> (In reply to Zain Iftikhar from comment #3)
> > Clarification is needed for point 3 i.e expanding selection
> > As I understand from this blog
> > https://help.libreoffice.org/6.2/en-US/text/scalc/guide/database_sort.
> > html?&DbPAR=WRITER&System=UNIX 
> > i.e 
> > If you select a range of cells, only these cells will get sorted then why is
> > point 3 saying that database range in the place of selection should be
> > considered.
> 
> Does this work for sort ascending/descending toolbar buttons as expected -
> especially in a scenario when the range is not equal to adjacent range?

I have reproduced a behavior
I have defined a Database range and there are some adjacent non-empty cells outside the database range.
Now if I select cells inside the database range and try to sort them there will be a popover stating "Sort Range"(with three options "Extend Selection", "Current Selection" and "Cancel") and If I choose Extend Selection now instead of sorting just Database range it also considers adjacent non-empty cells outside the database range.

Now the question is "Behavior should be sorting just the Database range when selecting Extend Selection option Or something else"  Pardon me If I'm not understanding point 3 properly?
Comment 6 Mike Kaganski 2022-05-05 06:57:15 UTC
(In reply to Zain Iftikhar from comment #5)

Please don't quote everything - this clutters the discussion. Only quote what is immediately relevant to your phrase. Often it's enough to simply answer without quoting, or to use "(In reply to ... from comment ...)" string, or just one sentence that you answer to.

> Now the question is "Behavior should be sorting just the Database range when
> selecting Extend Selection option Or something else"  Pardon me If I'm not
> understanding point 3 properly?

First of all, I likely needed to make #3 a separate issue. So please disregard #3 for the sake of this issue.
Comment 7 Andreas Säger 2022-07-29 08:28:48 UTC
Suggestion:

Get sort range
1) Test if the sort range is a named database range (menu:Data>Define).
2a) If so, expand a single cell selection to that range. 
2b)) If not, expand a single cell selection to current region.
In case of a range selection that is not identical with the db-range nor with the current region, prompt "The cells next to the selection contain data. do your really want ..." or respectively "The selected range intersects with a defined database range. do you really want ...")

Separate header row
3a) If we deal with a defined database range, obey the configuration declared for that range.
3b) If not, treat any first row with consecutive string values as a header row.
Side note: There is a relatively new option "Contains totals row" only for DatabaseRange. It is not defined for a sort descriptor.

3) Get sort columns
Expand selection to what has been found in steps 1) and 2).
3a) If called from Data>Sort command: Show sort dialog with what has been found in step 3).
3b) If called from quick sort button: Sort by the column where the active cell is.
Comment 8 Andreas Säger 2024-06-08 12:47:16 UTC
As a temporary work-around, I uploaded https://extensions.libreoffice.org/en/extensions/show/99205 which seems to work pretty well. 
It detects any database range at the active cell, takes over most settings from that database range and its sort descriptor. It sorts the database range ascending or descending by the field at the active cell. It disregards options "copy sort results" and "sort columns". It just sorts a database-like table in place by the active cell's field analog to the built-in quick sort buttons ".uno:SortAscending" and ".uno:SortDescending, which are dispatched when there is no database range at the active cell.