Skip to content

Commit a38adee

Browse files
committed
autofilter: try to show same row range after filter application
problem: when autofilter is applied sheet reset to view the first row(i.e row 0) very inconvenient with big sheets to go and find the row you were looking at now after this patch when autofilter is applied we try to show row which is in current view and will also be visible after applying the filter. In case if no row will be visible after filter from current view we show the next visible row example 1: our current view is showing row 550 to 580 after autofilter row 570 is first visible row in this range so the view will reset to row 570 as first row in the view example 1: our current view is showing row 550 to 580 after autofilter no row is visible in this range row 603 is first visible row afte this range so the view will reset to row 603 as first row in the view Signed-off-by: Pranam Lashkari <lpranam@collabora.com> Change-Id: I02a7315e7a42ef1f0a6f5dc4ab4be05b8aa6f5a8
1 parent fa03a2e commit a38adee

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

browser/src/layer/tile/CalcTileLayer.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,17 @@ window.L.CalcTileLayer = window.L.CanvasTileLayer.extend({
802802

803803
this._replayPrintTwipsMsgs(differentSheet);
804804

805-
this.sheetGeometry.setViewArea(this._pixelsToTwips(this._map._getTopLeftPoint()),
806-
this._pixelsToTwips(this._map.getSize()));
805+
if (this.sheetGeometry.autoFilterChanged) {
806+
this.sheetGeometry.autoFilterChanged = false;
807+
let firstVisibleRow = this.sheetGeometry.getFirstNewVisibleRow();
808+
app.activeDocument.activeLayout.scrollTo(
809+
this._map._getTopLeftPoint().x,
810+
this.sheetGeometry.getRowsGeometry().getElementData(firstVisibleRow).startpos);
811+
} else {
812+
this.sheetGeometry.setViewArea(
813+
this._pixelsToTwips(this._map._getTopLeftPoint()),
814+
this._pixelsToTwips(this._map.getSize()));
815+
}
807816

808817
this._addRemoveGroupSections();
809818

@@ -931,6 +940,10 @@ window.L.CalcTileLayer = window.L.CanvasTileLayer.extend({
931940
else if (e.commandName === 'AutoFilterInfo') {
932941
app.calc.autoFilterCell = { 'row': e.state.row, 'column': e.state.column };
933942
}
943+
else if (e.commandName === 'AutoFilterChange')
944+
{
945+
this.sheetGeometry.autoFilterChanged = true;
946+
}
934947
else if (e.commandName === 'PivotTableFilterInfo') {
935948
app.calc.pivotTableFilterCell = { 'row': e.state.row, 'column': e.state.column };
936949
}

browser/src/layer/tile/SheetGeometry.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,51 @@ export class SheetGeometry {
209209
return true;
210210
}
211211

212+
/**
213+
* Find which will be new first visible row after updating the sheet geometry
214+
*/
215+
public getFirstNewVisibleRow(): number {
216+
// Find first the visible rows in current view (before new sheet geometry is applied)
217+
const target: number = this.getViewRowRange().start;
218+
const invisibleRows: BoolSpanList = this.getRowsGeometry().getInvisibleSpanList(); //(hidden rows list in new geometry)
219+
220+
if (target === 0)
221+
return 0;
222+
223+
let start: number = 0;
224+
let end: number = invisibleRows._spanlist.length - 1;
225+
let ans: number = -1;
226+
227+
// Just one span means every row is visible
228+
if (start === 0 && end === 0)
229+
return target;
230+
231+
// Modified binary search to find next visible of after the target row
232+
while (start <= end) {
233+
const mid = Math.floor((start + end) / 2);
234+
if (invisibleRows._spanlist[mid] <= target) {
235+
start = mid + 1;
236+
} else {
237+
ans = mid;
238+
end = mid - 1;
239+
}
240+
}
241+
242+
// InvisibleRows is list of ranges in form of
243+
// Like: 5 8 10 20... -> Means that visible up to 5 (including), hidden up to 8 (including), visible up to 10 (including), hidden up to 20 (including)...
244+
if (ans !== -1) {
245+
if (ans === 0)
246+
return 0;
247+
// If its hidden index then +1 will be visible
248+
if (ans % 2 === 0 && (ans + 1 < invisibleRows._spanlist.length)) {
249+
return invisibleRows._spanlist[ans + 1];
250+
} else {
251+
return invisibleRows._spanlist[ans];
252+
}
253+
}
254+
return 0;
255+
}
256+
212257
public getPart(): number {
213258
return this._part;
214259
}
@@ -507,6 +552,7 @@ export class SheetDimension {
507552
private _filtered: BoolSpanList;
508553
private _outlines: DimensionOutlines;
509554
private _visibleSizes: SpanList;
555+
private _invisibleSpanList: BoolSpanList;
510556

511557
private _maxIndex: number;
512558
private _tileSizeTwips: number;
@@ -531,6 +577,8 @@ export class SheetDimension {
531577
this._visibleSizes = undefined;
532578
}
533579

580+
public getInvisibleSpanList(): BoolSpanList { return this._invisibleSpanList; }
581+
534582
public update(jsonObject: SheetDimensionCoreData): boolean {
535583

536584
if (typeof jsonObject !== 'object') {
@@ -595,8 +643,8 @@ export class SheetDimension {
595643

596644
private _updateVisible() {
597645

598-
var invisibleSpanList = this._hidden.union(this._filtered); // this._hidden is not modified.
599-
this._visibleSizes = this._sizes.applyZeroValues(invisibleSpanList); // this._sizes is not modified.
646+
this._invisibleSpanList = this._hidden.union(this._filtered); // this._hidden is not modified.
647+
this._visibleSizes = this._sizes.applyZeroValues(this._invisibleSpanList); // this._sizes is not modified.
600648
this._updatePositions();
601649
}
602650

0 commit comments

Comments
 (0)