Skip to content

Commit

Permalink
[FIX] Find&Replace: always search cell content if searchFormula
Browse files Browse the repository at this point in the history
The current implementation of a search with the option `serachFormula`
will only look into the cell content if the cell is a valid formula cell
and not a cell which results in an error following its evaluation.

e.g.  A1: '=SUM("a")' is invalid thus the related cell is not a formula
cell but an error cell and will never be matched when looking for the
term "sum".

closes #4990

Task: 4175968
X-original-commit: 4b485dd
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
rrahir committed Sep 17, 2024
1 parent 81fe3c0 commit 917a403
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/plugins/ui/find_and_replace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ export class FindAndReplacePlugin extends UIPlugin {
cell &&
this.currentSearchRegex &&
this.currentSearchRegex.test(
this.searchOptions.searchFormulas
? cell.isFormula()
? cell.content
: String(cell.evaluated.value)
: String(cell.evaluated.value)
this.searchOptions.searchFormulas ? cell.content : String(cell.evaluated.value)
)
) {
const position = this.getters.getCellPosition(cell.id);
Expand Down
16 changes: 15 additions & 1 deletion tests/plugins/find_and_replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
unhideRows,
updateFilter,
} from "../test_helpers/commands_helpers";
import { getCellContent, getCellText } from "../test_helpers/getters_helpers";
import { getCellContent, getCellError, getCellText } from "../test_helpers/getters_helpers";

let model: Model;
let searchOptions: SearchOptions;
Expand Down Expand Up @@ -499,6 +499,20 @@ describe("search options", () => {
expect(matchIndex).toBe(null);
});

test("Search in formula searches cell content of a cell in error", () => {
const model = new Model();
setCellContent(model, "A1", "=notASumFunction(2)");
setCellContent(model, "A2", '=SUM("a")');
expect(getCellError(model, "A1")).toBeDefined();
expect(getCellError(model, "A2")).toBeDefined();
searchOptions.searchFormulas = true;
model.dispatch("UPDATE_SEARCH", { toSearch: "sum", searchOptions });
const matches = model.getters.getSearchMatches();
expect(matches).toHaveLength(2);
expect(matches[0]).toStrictEqual({ col: 0, row: 0, selected: true });
expect(matches[1]).toStrictEqual({ col: 0, row: 1, selected: false });
});

test("Combine matching case / matching entire cell / search in formulas", () => {
model.dispatch("UPDATE_SEARCH", { toSearch: "hell", searchOptions });
let matches = model.getters.getSearchMatches();
Expand Down

0 comments on commit 917a403

Please sign in to comment.