Skip to content

Commit e7ae2f0

Browse files
committed
[FIX] Sort: Allow to sort array formula that do not spread
Some array formulas can lead to a single cell result. This can occur quite easily when manipulating array functions that end up aggregated in a single result. An easy example, ```javascript =min(if(A1:B4>1, 2)) ``` leads to a single cell result but should be sortable. closes #7189 Task: 5033192 X-original-commit: f0d974c Signed-off-by: Adrien Minne (adrm) <adrm@odoo.com> Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent 57bdca8 commit e7ae2f0

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/plugins/ui_feature/sort.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { isInside, overlap, positions, range, zoneToDimension } from "../../helpers/index";
1+
import {
2+
deepEquals,
3+
isInside,
4+
overlap,
5+
positions,
6+
range,
7+
zoneToDimension,
8+
} from "../../helpers/index";
29
import { sortCells } from "../../helpers/sort";
310
import {
411
CellPosition,
@@ -79,9 +86,10 @@ export class SortPlugin extends UIPlugin {
7986
}
8087

8188
private checkArrayFormulaInSortZone({ sheetId, zone }: SortCommand): CommandResult {
82-
const arrayFormulaInZone = positions(zone).some(({ col, row }) =>
83-
this.getters.getArrayFormulaSpreadingOn({ sheetId, col, row })
84-
);
89+
const arrayFormulaInZone = positions(zone).some(({ col, row }) => {
90+
const originPosition = this.getters.getArrayFormulaSpreadingOn({ sheetId, col, row });
91+
return originPosition && !deepEquals(originPosition, { sheetId, col, row });
92+
});
8593
return arrayFormulaInZone ? CommandResult.SortZoneWithArrayFormulas : CommandResult.Success;
8694
}
8795

tests/helpers/ui_helpers.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ describe("UI Helpers", () => {
446446
});
447447
});
448448

449-
test("Cannot sort on zone with array formulas", () => {
449+
test("Cannot sort on zone with array formulas that spread", () => {
450450
const raiseError = jest.fn();
451451
model = createModelFromGrid({ A1: "9", A2: "8", A3: "=CHOOSECOLS(A1:A2, 1)" });
452452
const env = makeTestEnv({ model, raiseError });
@@ -455,6 +455,20 @@ describe("UI Helpers", () => {
455455
expect(raiseError).toHaveBeenCalledWith("Cannot sort a zone with array formulas.");
456456
});
457457

458+
test("Can sort on zone with array formulas that do not spread", () => {
459+
const raiseError = jest.fn();
460+
model = createModelFromGrid({ A1: "9", A2: "8", B1: "1", C1: "=MMULT(A1:A2, A1:B1)" });
461+
const env = makeTestEnv({ model, raiseError });
462+
463+
interactiveSortSelection(env, sheetId, toCartesian("C1"), toZone("C1:D2"), "asc");
464+
expect(raiseError).toHaveBeenCalledTimes(1);
465+
expect(raiseError).toHaveBeenCalledWith("Cannot sort a zone with array formulas.");
466+
467+
setCellContent(model, "C1", "=MMULT( A1:B1, A1:A2)");
468+
interactiveSortSelection(env, sheetId, toCartesian("C1"), toZone("C1:D2"), "asc");
469+
expect(raiseError).toHaveBeenCalledTimes(1);
470+
});
471+
458472
describe("Sort Merges", () => {
459473
const raiseError = jest.fn();
460474
const sheetId: UID = "sheet5";

0 commit comments

Comments
 (0)